Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to encrypt a file in a CLI and decrypt it with iOS?
    primarykey
    data
    text
    <p>I have some problems decrypting a file encrypted with openssl in a bash. Below is what I did step by step. I can't figure out where does it go wrong.</p> <p>The original file (ended with newline) :</p> <pre><code>123456 abcdef ghijkl </code></pre> <p>Generate 32 bytes long random password :</p> <pre><code>$ openssl rand -hex 32 fec8950708098e9075e8b4df9a969aa7963c4d820158e965c7848dbfc8ca73ed </code></pre> <p>Encrypt the file :</p> <pre><code>$ openssl aes-128-ecb -in original.txt -out encrypted.txt </code></pre> <p>About the encrypted file :</p> <pre><code>$ file encrypted.txt encrypted.txt: Non-ISO extended-ASCII text, with CR line terminators, with overstriking $ cat encrypted.txt Salted__??\z?F?z????4G}Q? Y?{ӌ???????b*?? </code></pre> <p>Code to call the decrypt method :</p> <pre><code>NSData *myDataDec = [self aesDecrypt:@"fec8950708098e9075e8b4df9a969aa7963c4d820158e965c7848dbfc8ca73ed" data:myData]; NSLog(@"decrypted: %@", [[NSString alloc] initWithData:myDataDec encoding:NSASCIIStringEncoding]); </code></pre> <p>Method to decrypt :</p> <pre><code>- (NSData *)aesDecrypt:(NSString *)key data:(NSData *)data { // 'key' should be 32 bytes for AES256, will be null-padded otherwise char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused) bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding) // fetch key data [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; NSUInteger dataLength = [data length]; //See the doc: For block ciphers, the output size will always be less than or equal to the input size plus the size of one block. //That's why we need to add the size of one block here size_t bufferSize = dataLength + kCCBlockSizeAES128; void *buffer = malloc(bufferSize); size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCKeySizeAES256, NULL /* initialization vector (optional) */, [data bytes], dataLength, /* input */ buffer, bufferSize, /* output */ &amp;numBytesEncrypted); NSLog(@"cryptStatus: %d", cryptStatus); if (cryptStatus == kCCSuccess) { NSLog(@"aes success"); //the returned NSData takes ownership of the buffer and will free it on deallocation return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; } NSLog(@"aes error"); free(buffer); //free the buffer; return nil; } </code></pre> <p>Logs :</p> <pre><code>2012-09-01 15:08:51.331 My Project[75582:f803] cryptStatus: -4304 2012-09-01 15:08:51.332 My Project[75582:f803] aes error 2012-09-01 15:08:51.332 My Project[75582:f803] decrypted: </code></pre> <p>kCCDecodeError details :</p> <pre><code>kCCDecodeError - Input data did not decode or decrypt properly. </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload