java - Objective-c AES 加密看起来不像 java AES 加密

标签 java iphone objective-c encryption aes

好吧,我正在尝试使用以下方法加密扩展 NSData 的 objective-c 中的字符串:


 @implementation NSData (AES128)<p></p>

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

<p>NSUInteger dataLength = [self length];</p>

<p>size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);</p>

<p>size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,kCCAlgorithmAES128,kCCOptionPKCS7Padding,keyPtr,kCCKeySizeAES128,NULL /* initialization vector (optional) <em>/,[self bytes], dataLength, /</em> input <em>/buffer, bufferSize, /</em> 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];
}</p>

<p>free(buffer); //free the buffer;
return nil;
}</p></li>
<li><p>(NSData *)AES128Decrypt {
char keyPtr[kCCKeySizeAES128] = {'\xe1','\xaa','\x9c','\x61','\x46','\x74','\x44','\x56','\xf0','\xe5','\x47','\x46','\x86','\xdc','\x95','\x77'};</p>

<p>NSUInteger dataLength = [self length];</p>

<p>//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);</p>

<p>size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus=CCCrypt(kCCDecrypt,kCCAlgorithmAES128,kCCOptionPKCS7Padding,keyPtr, kCCKeySizeAES128,NULL /* initialization vector (optional) <em>/,[self bytes], dataLength, /</em> input <em>/buffer, bufferSize, /</em> output */&numBytesDecrypted);</p>

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

<p>free(buffer); //free the buffer;
return nil;
}</p></li>
</ul>

<p>@end
 </p>

然后我在这里称它为:


NSString *strData = @"My string";<p></p>

<p>NSData *objNSData = [NSData dataWithData:[strData dataUsingEncoding: NSUTF8StringEncoding]];</p>

<p>NSLog(@"encrypted: %@",[objNSData description]);
 </p>

如果我只是在 objective-c 中使用它,它工作正常。 但是当我尝试将它发送到 Java 服务器时,它不起作用。

我的密码数据是这样的:

86fcf0fa9e3dff93dc8918ffd02ee203 12de0bf8c8ba300456293c4240296c0d

如果我尝试在 java 中使用具有相同 key 的 AES 对其进行加密,我会得到:

86fcf0fa9e3dff93dc8918ffd02ee203 8388f173da143c6aeeb90e554259c83c

这很奇怪,因为前半部分是一样的。

有人知道为什么会这样吗? 谢谢。

最佳答案

我从未进行过任何 Objective-C 编程,但我几乎可以肯定您在不同的环境中使用 AES modes在你的代码中。您需要确保这些是一致的。默认值可能是 Cipher Block Chaining (CBC)模式。确保在 Java 代码中设置此选项。

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

我有义务就密码学提供标准免责声明,使用更高级别的协议(protocol)通常会更安全,为您处理这些内容,例如用于传输数据的 SSL/TLS 和类似 Keyczar 的协议(protocol)。用于静态数据。获得正确的加密真的很难,一个微小的错误(比如选择一个错误的模式)可能会完全破坏系统的安全性。

关于java - Objective-c AES 加密看起来不像 java AES 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3811708/

相关文章:

java - Android 3 Scrollviews 用于比较

java - 算法 - 确定矩阵中是否存在具有相同值的特定大小的对角线

iphone - NSURLConnection 没有响应

iphone - 在iOS中将GMT转换为IST

ios - 将 UISearchBar 取消按钮更改为图标

java - C# 结构内存优化?

java - CyanogenMod 和 OpenGL ES 纹理加载

iphone - iPhone上的自动数据备份

iphone - 在 NSURLConnection 下载期间更新 iPhone UIProgressView

objective-c - 在 ios 中生成网站屏幕截图