Objective-c AES encryption doesn't look like java AES encryption

夙愿已清 提交于 2019-12-21 05:44:31

问题


Well im trying to encrypt an string in objective c extending NSData with this method:


 @implementation NSData (AES128)

  • (NSData *)AES128Encrypt { char keyPtr[kCCKeySizeAES128] = {'\xe1','\xaa','\x9c','\x61','\x46','\x74','\x44','\x56','\xf0','\xe5','\x47','\x46','\x86','\xdc','\x95','\x77'};

    NSUInteger dataLength = [self length];

    size_t bufferSize = dataLength + kCCBlockSizeAES128; void *buffer = malloc(bufferSize);

    size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,kCCAlgorithmAES128,kCCOptionPKCS7Padding,keyPtr,kCCKeySizeAES128,NULL /* initialization vector (optional) /,[self bytes], dataLength, / input /buffer, bufferSize, / output */ &numBytesEncrypted); if (cryptStatus == kCCSuccess) { //the returned NSData takes ownership of the buffer and will free it on deallocation return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; }

    free(buffer); //free the buffer; return nil; }

  • (NSData *)AES128Decrypt { char keyPtr[kCCKeySizeAES128] = {'\xe1','\xaa','\x9c','\x61','\x46','\x74','\x44','\x56','\xf0','\xe5','\x47','\x46','\x86','\xdc','\x95','\x77'};

    NSUInteger dataLength = [self 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 numBytesDecrypted = 0; CCCryptorStatus cryptStatus=CCCrypt(kCCDecrypt,kCCAlgorithmAES128,kCCOptionPKCS7Padding,keyPtr, kCCKeySizeAES128,NULL /* initialization vector (optional) /,[self bytes], dataLength, / input /buffer, bufferSize, / output */&numBytesDecrypted);

    if (cryptStatus == kCCSuccess) { //the returned NSData takes ownership of the buffer and will free it on deallocation return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted]; }

    free(buffer); //free the buffer; return nil; }

@end

then i call it here:


NSString *strData = @"My string";

NSData *objNSData = [NSData dataWithData:[strData dataUsingEncoding: NSUTF8StringEncoding]];

NSLog(@"encrypted: %@",[objNSData description]);

If I just use it in objective c, it works fine. But when i try to send it to a java server it doesn't work.

My cipher data seems like this:

86fcf0fa9e3dff93dc8918ffd02ee203 12de0bf8c8ba300456293c4240296c0d

and if I try to cipher it in java using also AES with the same key, i get this:

86fcf0fa9e3dff93dc8918ffd02ee203 8388f173da143c6aeeb90e554259c83c

its weird because the first half its the same.

Someone knows why this can be happening? thanks.


回答1:


I've never done any Objective-C programming, but I'm almost positive that you're using AES in different modes in your code. You need to make sure these are consistent. The default is probably Cipher Block Chaining (CBC) mode. Make sure you set this option in your Java code.

By the way, CBC mode should have a randomized Initialization Vector (IV) rather than NULL (which I assume uses all zeros). This too would need to be consistent across both.

I'm obliged to give standard disclaimer with cryptography that it's usually much safer to use a higher level protocol that handles this stuff for you like SSL/TLS for data in transit and something like Keyczar for data at rest. Getting crypto right is really hard and a tiny error (like picking a bad mode) can totally destroy the security of the system.



来源:https://stackoverflow.com/questions/3811708/objective-c-aes-encryption-doesnt-look-like-java-aes-encryption

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!