主要解决python 加密,java 解密 ,或者 java 加密,python 解密
python demo--cryptography 版本
# 导入库
pip install cryptography
import base64
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
aes_key = '1d35eefc2b8207d615028d056ce5296c'
associatedData = "12345"
nonce = "1234567812345678"
# 加密
def st_aes_en_func():
de_data = '1234561234565'
aesgcm = AESGCM(aes_key.encode())
ct = aesgcm.encrypt(nonce.encode(), de_data.encode(), associatedData.encode())
return base64.b64encode(ct).decode('utf-8')
# 解密
def st_aes_de_func():
en_data = 'X3lkQaqdASpIF0+XsOjwUhfCZdvXh3RQFXsH8o8='
en_data = base64.b64decode(en_data.encode('utf-8'))
aesgcm = AESGCM(aes_key.encode())
ct = aesgcm.decrypt(nonce.encode(), en_data, associatedData.encode())
return ct.decode()
java 版本
package tools;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class AesUtil {
static final int KEY_LENGTH_BYTE = 32;
static final int TAG_LENGTH_BIT = 128;
private final byte[] aesKey;
public AesUtil(byte[] key) {
if (key.length != KEY_LENGTH_BYTE) {
throw new IllegalArgumentException("无效的ApiV3Key,长度必须为32个字节");
}
this.aesKey = key;
}
public String decryptToString(byte[] associatedData, byte[] nonce, String ciphertext)
throws GeneralSecurityException, IOException {
try {
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
SecretKeySpec key = new SecretKeySpec(aesKey, "AES");
GCMParameterSpec spec = new GCMParameterSpec(TAG_LENGTH_BIT, nonce);
cipher.init(Cipher.DECRYPT_MODE, key, spec);
cipher.updateAAD(associatedData);
return new String(cipher.doFinal(Base64.getDecoder().decode(ciphertext)), "utf-8");
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
throw new IllegalStateException(e);
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
throw new IllegalArgumentException(e);
}
}
public String encryptToString(byte[] associatedData, byte[] nonce, String ciphertext)
throws GeneralSecurityException, IOException{
try {
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
SecretKeySpec key = new SecretKeySpec(aesKey, "AES");
GCMParameterSpec spec = new GCMParameterSpec(TAG_LENGTH_BIT, nonce);
cipher.init(Cipher.ENCRYPT_MODE, key, spec);
cipher.updateAAD(associatedData);
// return new String(cipher.doFinal(Base64.getDecoder().decode(ciphertext)), "utf-8");
return new String(Base64.getEncoder().encode(cipher.doFinal(ciphertext.getBytes())), "utf-8");
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
throw new IllegalStateException(e);
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
throw new IllegalArgumentException(e);
}
}
public static void main(String[] args) throws Exception {
String key = "1d35eefc2b8207d615028d056ce5296c";
String associatedData = "12345";
String nonce = "1234567812345678";
AesUtil aesUtils = new AesUtil(key.getBytes());
String enData = aesUtils.encryptToString(associatedData.getBytes(), nonce.getBytes(), "1234561234565");
System.out.println("加密后密文:" + enData);
String deData = aesUtils.decryptToString(associatedData.getBytes(), nonce.getBytes(), enData);
System.out.println("解密后明文:" + deData);
}
}