AES128decryption failed

When I decrypt, there will be a decryption exception due to multiple version judgments.

but,Remove the version judgment and normal results will be displayed.

(Encrypted data & key & iv Encrypted data all identical)

Encrypted Data from [NSUserDefaults standardUserDefaults]




================================================================

Func:


Step1:


NSData* theData = [[[NSData alloc] initWithBase64EncodedString:userID options:NSDataBase64DecodingIgnoreUnknownCharacters]AES128DecryptWithKey: KEY withIV: IV];



Step2:

- (NSData *) AES128DecryptWithKey: (NSString *) _sKey withIV: (unsigned int) _iv {

char keyPtr[kCCKeySizeAES128 + 1];

bzero(keyPtr, sizeof(keyPtr));

[_sKey getCString: keyPtr maxLength: sizeof(keyPtr) encoding: NSUTF8StringEncoding];


NSUInteger dataLength = [self length];

NSData *nsData;

size_t bufferSize = dataLength + kCCBlockSizeAES128;

void *buffer = malloc(bufferSize);

void *iv = malloc(16);

memset((void *)iv, _iv, 16);


size_t numBytesDecrypted = 0;

CCCryptorStatus cryptStatus = CCCrypt( kCCDecrypt,

kCCAlgorithmAES128,

kCCOptionPKCS7Padding,

keyPtr,

kCCKeySizeAES128,

iv ,

[self bytes], dataLength,

buffer, bufferSize,

&numBytesDecrypted);


if (cryptStatus == kCCSuccess)

nsData = [NSData dataWithBytes: buffer length: numBytesDecrypted];


free(iv);

free(buffer);


return nsData;

}

================================================================


Xcode Ver : 11.3
Test device : SimPhone 11~13 & Iphone 6(Ver.11)、IphoneXs(Ver.13)

Replies

I’m sorry to say that this code has numerous problems, but at the code level and at the cryptographic level (using a C string as-is as a cryptographic key is very poor form). I can help you fix the code problems but I encourage you to discuss the cryptographic aspects of this with a security professional.

Anyway, on the code front, I recommend that you look at the CryptoCompatibility sample code, which shows how to do AES encryption and decryption in a way that’s compatible with other major cryptographic libraries. If you can’t work out from that, please post a code snippet and a test vector, that is, the inputs to the function and the expected outputs.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

The difference between the two versions is to install swift related resource library.


I'm not sure if this will affect the decryption.


Or do you need more information to confirm what the problem?


Annex is code snippet and a test vector and inputs to the function and the expected outputs.


Annex.Link https://www.icloud.com/iclouddrive/06b9jW1qlQLj1i2ui9OnhLc5Q#LogScreenshot

Thanks.

I’m sorry but there’s not much I can do with screen shots. Ideally I’d like code snippets, including a test vector, that I can copy into a small test project here.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"