Pycryptodome AES_GCM加密到Java解密

问题描述

我一直在尝试用Python进行AES_GCM加密,而在Java中进行解密,以下是我要完成的相关代码片段,我还检查了以下问题:Pycrypto AES GCM encryption and Java decryption对我的问题。我怀疑我为Python设置的IV / nonce错误,将不胜感激。

Python加密代码

from Crypto.Cipher import AES
from base64 import b64encode
someKey = 'Sixteen byte key'
cipher = AES.new(someKey,AES.MODE_GCM,nonce='0000000000000000',mac_len=16)
ciphertext,tag = cipher.encrypt_and_digest(data)
ciphertext = ciphertext + tag
print b64encode(ciphertext)

Java解密代码

private static byte[] initializationVector = new byte[16];


Cipher cipher = Cipher.getInstance("AES/GCM/nopadding"); 
 
SecretKeySpec keySpec = new SecretKeySpec(someKey,"AES"); 

GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(128,initializationVector);  
     
cipher.init(Cipher.DECRYPT_MODE,keySpec,gcmParameterSpec);

byte[] bytesstring = Base64.getDecoder().decode(theString);  
 
return new String(cipher.doFinal(bytesstring),"UTF-8");

但是我无法用Java解密它,出现以下错误

javax.crypto.AEADBadTagException: Tag mismatch!
at java.base/com.sun.crypto.provider.galoisCounterMode.decryptFinal(galoisCounterMode.java:623)
at java.base/com.sun.crypto.provider.CipherCore.finalnopadding(CipherCore.java:1116)
at java.base/com.sun.crypto.provider.CipherCore.fillOutputBuffer(CipherCore.java:1053)
at java.base/com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:853)
at java.base/com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
at java.base/javax.crypto.Cipher.doFinal(Cipher.java:2202)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)