How can I make my AES encryption identical between Java and Objective-C (iPhone)?

后端 未结 4 1234
清酒与你
清酒与你 2020-12-02 11:50

I am encrypting a string in objective-c and also encrypting the same string in Java using AES and am seeing some strange issues. The first part of the result matches up to a

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-02 12:20

    Since the CCCrypt takes an IV, does it not use a chaining block cipher method (such as CBC)? This would be consistant with what you see: the first block is identical, but in the second block the Java version applies the original key to encrypt, but the OSX version seems to use something else.

    EDIT:

    From here I saw an example. Seems like you need to pass the kCCOptionECBMode to 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);
    

    EDIT 2:

    I played around with some command line to see which one was right. I thought I could contribute it:

    $ 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.
    

提交回复
热议问题