16进制
key=BMKp20Omcv9u7TqGAhydCw63
需要加密的字符串是:hello unionpay
java输出结果是:6c10f079319826fd0337a9ddfd531795
java代码:
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
/**
* @ Description : AES加解密工具类
* @ Author :
* @ CreateDate : 2019/10/25 11:48
* @ Version : 1.0
*/
public class AesUtil {
private static final String CHARSET = "UTF-8";
private static final String KEY_ALGORITHM = "AES";
/**默认的加密算法 aes-192-cbc*/
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
//private static final String IV_PARAMETER_SPEC_STRING = "0000000000000000";
//对应nodejs的buffer.alloc(16,0)
private static final byte[] IV_PARAMETER_SPEC_BYTE = new byte[]{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00};
private static final Integer RADIX = 16;
public static void main(String[] args) {
String testStr ="";
String aesKey = "";/密钥
try {
String aesEncrypt = aesEncrypt(aesKey, testStr);
System.out.println("========success:===488944dc7dca74786f21fe9b98420038==================");
System.out.println(aesEncrypt);
String aesEncryptStr = "bb269a7ad5d995e103aa98a1b831938279b975878df0ee747e4affd5e278f766ea68da78e078e863416d6a8c12ef5000afffaf1f95053c2684fb173472441046";
String aesDecrypt = aesDecrypt(aesKey, aesEncryptStr);
System.out.println(aesDecrypt);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* AES 加密操作
*
* @param aesKey 密钥
* @param data 待加密内容
* @return 加密数据
*/
public static String aesEncrypt(String aesKey, String data) throws Exception {
return byte2HexString(encrypt(aesKey.getBytes(CHARSET), data.getBytes(CHARSET)));
}
/**
* AES 解密操作
*
* @param aesKey 密钥
* @param data 待解密内容
* @return 解密数据
*/
public static final String aesDecrypt(String aesKey, String data) throws Exception {
return new String(decrypt(aesKey.getBytes(CHARSET), hexString2Byte(data)), CHARSET);
}
private static byte[] decrypt(byte[] key, byte[] data) throws Exception {
SecretKeySpec secretKeySpec = new SecretKeySpec(key, KEY_ALGORITHM);
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER_SPEC_BYTE);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, iv);
return cipher.doFinal(data);
}
private static byte[] hexString2Byte(String hexString) {
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] b = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
b[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return b;
}
private static String byte2HexString(byte[] src) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xff;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
sb.append("0");
}
sb.append(hv);
}
return sb.toString();
}
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
private static byte[] encrypt(byte[] key, byte[] data) throws Exception {
SecretKeySpec secretKeySpec = new SecretKeySpec(key, KEY_ALGORITHM);
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER_SPEC_BYTE);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, iv);
return cipher.doFinal(data);
}
}
python代码应该怎么写