重点提要:crypto-js 应对key进行 md5 散列
AES加密 规定了密钥长度,crypto通过md5的方式使key设为了定长,所以crypto-js(包括java)为了与crypto结果保持一致,应当对key进行md5散列,且散列结果不为字符串,而是数组(即WordArry,类比Java中的byte[],Node.js中的Buffer),以下附上crypto-js、crypto、java三端AES-ECB-128加解密代码:
import CryptoJS from 'crypto-js'; // 先全局安装:npm install crypto-js --save-dev
//aes ecb 128 加密
export const aesEncrypt = (content: string, secret: string) => {
const srcs = CryptoJS.enc.Utf8.parse(content);
const encrypted = CryptoJS.AES.encrypt(srcs, CryptoJS.MD5(secret), {
iv: [],
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.toString();
}
//aes ecb 128 解密
export const aesDecrypt = (content: string, secret: string) => {
//content为密文,无需做处理即可
const decrypt = CryptoJS.AES.decrypt(content, CryptoJS.MD5(secret) , {
iv: [],
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
return decryptedStr.toString();
}
const crypto = require('crypto');
const AES_ALGORITHM = "aes-ebc-128";
const UTF8 = "utf-8";
const BASE64 = "base64";
//aes ecb 128 加密
export const aseEncrypt = (content: string, secret: string) => {
const cipherChunks = [];
const cipher = crypto.createCipher(AES_ALGORITHM, secret);
cipherChunks.push(cipher.update(content, UTF8, BASE64));
cipherChunks.push(cipher.final(BASE64));
return cipherChunks.join('');
}
//aes ecb 128 解密
export const aseDecrypt = (content: string, secret: string) => {
const decipherChunks = [];
const decipher = crypto.createDecipher(AES_ALGORITHM, secret);
decipherChunks.push(decipher.update(content, BASE64, UTF8));
decipherChunks.push(decipher.final(UTF8));
return decipherChunks.join('');
}
//参考引入包
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
//以下内容请放到类中
public final static String AES_ALGORITHM = "AES/ECB/PKCS5Padding";
public final static String AES = "AES";
public final static String MD5 = "MD5";
public final static String UTF8 = "utf-8";
//aes ecb 128 加密
public static String aesEncrypt(String content, String secret) {
Cipher cipher = null;
byte[] cipherBytes = null;
String result = null;
try {
MessageDigest md = MessageDigest.getInstance(MD5);
SecretKey secretKey = new SecretKeySpec(md.digest(secret.getBytes(UTF8)), AES);
cipher = Cipher.getInstance(AES_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
cipherBytes = cipher.doFinal(content.getBytes(UTF8));
//Android端使用下一行
//result = new String(Base64.encode(cipherBytes, Base64.DEFAULT), UTF8);
//Java使用下一行
result = new String(Base64.getEncoder().encode(cipherBytes));
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
//aes ecb 128 解密
public static String aesDecrypt(String data, String key) {
Cipher cipher = null;
byte[] dataBytes = Base64.getDecoder().decode(data);
byte[] plainBytes = null;
String result = null;
try {
MessageDigest md = MessageDigest.getInstance(MD5);
SecretKey secretKey = new SecretKeySpec(md.digest(key.getBytes()), AES);
cipher = Cipher.getInstance(AES_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
plainBytes = cipher.doFinal(dataBytes);
result = new String(plainBytes, UTF8);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}