简体   繁体   中英

AES cypher in GCM mode with null byte IV in java

I have to encrypt a json payload using AES cypher in GCM mode with null byte IV.

When i tried using the secret key and the json payload, i am getting a different result than expected. It only mismatches with AES GCM MAC part.

These are the parameters and my source code:

  • GCM_TAG_LENGTH= 16,

  • Symmetric key In Hex: 083080D3D0C521C02CD3AE2134363D09EA50DFF914677FAB9E22F18F9C28A3B9

  • jsonPayload:{"Parameter1":"Value1","Parameter2":"Value2","Parameter3":"Value3"}

  • OutPut (Expected): EF6BA2FFA05B6985FE129E3CB6845C4EA1E94AE98D31A538A4E24906FB720D764D640894CD9 DE7CEC00114396651A1CCAEDCF480C57A959E925C04492B9CF85FC711FAB3CBED10DC2BA99A2B B063CEFF8DE1

    Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
    SecretKeySpec keySpec = new SecretKeySpec(secretkey, "AES");
    GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(Integer.valueOf(WalletStaticTypes.GCM_TAG_LENGTH.getType()) * 8,newbyte[12]);
    cipher.init(Cipher.ENCRYPT_MODE, keySpec,gcmParameterSpec);
    byte[] encryptedPayLoad = cipher.doFinal(jsonPayload.getBytes("UTF-8"));
    Hex.encodeHexString(encryptedPayLoad,false);

I noticed that this is about the Apple Pay integration, I managed to achieve the same results from the test documentation using

GCMParameterSpec(128, ByteArray(12))

val cipher = Cipher.getInstance("AES/GCM/NoPadding")
    cipher.init(Cipher.ENCRYPT_MODE, finalKey, GCMParameterSpec(128, ByteArray(12)))
// AES Key 
083080D3D0C521C02CD3AE2134363D09EA50DFF914677FAB9E22F18F9C28A3B9
// JSON Payload (67 bytes), UTF-8 encoded: {"Parameter1":"Value1","Parameter2":"Value2","Parameter3":"Value3"}

Output:
E3EF6BA2FFA05B6985FE129E3CB6845C4EA1E94AE98D31A538A4E24906FB720D764D640894CD9DE7CEC00114396651A1CCAEDCF480C57A959E925C04492B9CF85FC711FAB3CBED10DC2BA99A2BB063CEFF8DE1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM