java - 如何使 Java 和 Objective-C (iPhone) 之间的 AES 加密相同?

标签 java iphone objective-c encryption aes

我在 Objective-c 中加密一个字符串,并在 Java 中使用 AES 加密相同的字符串,我看到了一些奇怪的问题。结果的第一部分匹配到某个点,但随后又有所不同,因此当我将结果从 Java 解码到 iPhone 上时,它无法解密。

我使用的源字符串是“现在,这都是什么废话。你知道吗?” 使用“1234567890123456”键

要加密的 Objective-c 代码如下: 注意:它是一个 NSData 类别,因此假设在 NSData 对象上调用该方法,因此“self”包含要加密的字节数据。

   - (NSData *)AESEncryptWithKey:(NSString *)key {
 char keyPtr[kCCKeySizeAES128+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 = [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 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;
}

而java加密代码是……

public byte[] encryptData(byte[] data, String key) {
    byte[] encrypted = null;

    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    byte[] keyBytes = key.getBytes();

    SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");

    try {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);

        encrypted = new byte[cipher.getOutputSize(data.length)];
        int ctLength = cipher.update(data, 0, data.length, encrypted, 0);
        ctLength += cipher.doFinal(encrypted, ctLength);
    } catch (Exception e) {
        logger.log(Level.SEVERE, e.getMessage());
    } finally {
        return encrypted;
    }
}

objective-c 代码的十六进制输出为 -

7a68ea36 8288c73d f7c45d8d 22432577 9693920a 4fae38b2 2e4bdcef 9aeb8afe 69394f3e 1eb62fa7 74da2b5c 8d7b3c89 a295d306 f1f90349 6899ac34 63a6efa0

java 输出是 -

7a68ea36 8288c73d f7c45d8d 22432577 e66b32f9 772b6679 d7c0cb69 037b8740 883f8211 748229f4 723984beb 50b5aea1 f17594c9 fad2d05e e0926805 572156d

如您所见,一切都很好-

7a68ea36 8288c73d f7c45d8d 22432577

我猜我有一些不同的设置,但不知道是什么,我尝试在 java 端在 ECB 和 CBC 之间进行更改,但没有效果。

谁能帮忙!?请……

最佳答案

既然 CCCrypt 采用 IV,它不使用链接 block 密码方法(例如 CBC)吗?这与您所看到的一致:第一个 block 是相同的,但在第二个 block 中,Java 版本应用原始 key 进行加密,但 OSX 版本似乎使用了其他东西。

编辑:

here 我看到了一个例子。似乎您需要将 kCCOptionECBMode 传递给 CCCrypt:

ccStatus = CCCrypt(encryptOrDecrypt,
        kCCAlgorithm3DES,
        kCCOptionECBMode, <-- this could help
        vkey, //"123456789012345678901234", //key
        kCCKeySize3DES,
        nil, //"init Vec", //iv,
        vplainText, //"Your Name", //plainText,
        plainTextBufferSize,
        (void *)bufferPtr,
        bufferPtrSize,
        &movedBytes);

编辑 2:

我使用了一些命令行来查看哪个是正确的。我想我可以贡献它:

$ echo "Now then and what is this nonsense all about. Do you know?" | openssl enc -aes-128-ecb -K $(echo 1234567890123456 | xxd -p) -iv 0 | xxd 
0000000: 7a68 ea36 8288 c73d f7c4 5d8d 2243 2577  zh.6...=..]."C%w
0000010: e66b 32f9 772b 6679 d7c0 cb69 037b 8740  .k2.w+fy...i.{.@
0000020: 883f 8211 7482 29f4 7239 84be b50b 5aea  .?..t.).r9....Z.
0000030: eaa7 519b 65e8 fa26 a1bb de52 083b 478f  ..Q.e..&...R.;G.

关于java - 如何使 Java 和 Objective-C (iPhone) 之间的 AES 加密相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2280375/

相关文章:

objective-c - NSString:isEqual 与 isEqualToString

java - Spring MVC 文件下载 IOUtils.copy 有效,但 FileCopyUtils 无效

ios - 具有 cocoa touch 的自定义 UI 元素

java - Spring 网络流 : how is a request handled?

ios - 升级我的 Mac 后无法再构建 iOS 模拟器

ios - 交互式推送通知 - 隐藏/显示按钮

iphone - 如果 NSDictionary 适用于成对值,那么什么适用于三重值?

ios - UITableView 在移出屏幕之前不会完全填充单元格

java - log4j 时区

java - 从未知路径读取文件