AES encryption makes different result in iOS and Android

前端 未结 2 1484
北海茫月
北海茫月 2021-02-04 15:16

Trying to encrypt sample data using AES128 algorithm with CBC and PKCS7 padding in Android and iOS, but results are different :(

Android code:

private s         


        
2条回答
  •  醉梦人生
    2021-02-04 15:44

    The Android code uses explicitly CBC mode. But the iOS code does not specify this. At least I don't see it there.

    Also when you use CBC mode, you must also specify Initialization Vector:

    byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; // use different random value
    AlgorithmParameterSpec algorithmSpec = new IvParameterSpec(iv);
    Cipher ecipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
    ecipher.init(Cipher.ENCRYPT_MODE, skeySpec, algorithmSpec);
    

    You should use the same initialization vector on iOS and also specify you are using CBC mode.

提交回复
热议问题