Objective-c AES加密不像java AES加密 [英] Objective-c AES encryption doesn't look like java AES encryption

查看:220
本文介绍了Objective-c AES加密不像java AES加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图加密一个使用此方法扩展NSData的目标c中的字符串:

  
@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 / *初始化向量(可选) /,[self bytes],dataLength,/ input / buffer ,bufferSize,/ output * /
    & numBytesEncrypted);
    if(cryptStatus == kCCSuccess){
    //返回的NSData获取缓冲区的所有权,并在释放后释放它
    return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
    }



    free(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];



    //查看文档:对于块密码,输出大小始终小于
    //等于输入大小加上一个块的大小。
    //这就是为什么我们需要在这里添加一个块的大小
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void * buffer = malloc(bufferSize);



    size_t numBytesDecrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,kCCAlgorithmAES128,kCCOptionPKCS7Padding,keyPtr,kCCKeySizeAES128,NULL / *初始化向量(可选) /,[self bytes],dataLength,/ input / buffer ,bufferSize,/ output * /& numBytesDecrypted);



    if(cryptStatus == kCCSuccess){
    //返回的NSData拥有所有权的缓冲区,并释放它在释放
    返回[NSData dataWithBytesNoCopy:缓冲区长度:numBytesDecrypted];
    }



    free(buffer); //释放缓冲区;
    return nil;
    }




@end



然后我在这里调用:

  
NSString * strData = @我的字符串;



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



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



如果我只是在客观c中使用它,它的工作正常。
但是,当我尝试发送到一个java服务器它不工作。



我的密码数据看起来像这样:



86fcf0fa9e3dff93dc8918ffd02ee203 12de0bf8c8ba300456293c4240296c0d



如果我尝试使用AES使用相同的密码在java中加密,我得到这个:



86fcf0fa9e3dff93dc8918ffd02ee203 8388f173da143c6aeeb90e554259c83c



它的奇怪,因为上半部分一样。



有人知道为什么会发生这种情况?
谢谢。

解决方案

我从来没有做过任何Objective-C编程,但我几乎是肯定的,在代码中使用不同的模式中使用AES。你需要确保这些是一致的。默认值可能是密码块链接(CBC)模式。确保您在Java代码中设置此选项。



顺便说一句,CBC模式应该有一个随机的初始化向量(IV)而不是NULL(我假定使用全零)。这也需要在两者之间保持一致。



我不得不使用加密技术提供标准免责声明,因为使用处理此类内容的更高级协议通常更为安全您可以像运输中的数据一样使用SSL / TLS,也可以像 Keyczar 一样休息。获取加密权利是非常困难的,一个微小的错误(如挑选不好的模式)可以完全破坏系统的安全性。


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.

解决方案

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.

这篇关于Objective-c AES加密不像java AES加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆