Node代码
let crypto = require('crypto');
//偏移量 16位
const iv = "0123456789ABCDEF";
//密钥
const key = "0123456789ABCDEF";
//加密
function encodeAes(word) {
if (!word) {
return ''
}
if (typeof word != 'string') {
word = JSON.stringify(word)
}
const md5 = crypto.createHash('md5');
const result = md5.update(key).digest();
const cipher = crypto.createCipheriv('aes-128-gcm', result, iv);
const encrypted = cipher.update(word, 'utf8');
const finalstr = cipher.final();
const tag = cipher.getAuthTag();
const res = Buffer.concat([encrypted, finalstr, tag]);
return res.toString('base64');
}
//解密
function decodeAes(word) {
if (!word) {
return ''
}
const md5 = crypto.createHash('md5');
const result = md5.update(key).digest();
const decipher = crypto.createDecipheriv('aes-128-gcm', result, iv);
const b = Buffer.from(word, 'base64')
decipher.setAuthTag(b.subarray(b.length - 16));
const str = decipher.update(Buffer.from(b.subarray(0, b.length - 16), 'hex'));
const fin = decipher.final();
const decryptedStr = new TextDecoder('utf8').decode(Buffer.concat([str, fin]))
try {
return JSON.parse(decryptedStr);
} catch (e) {
return decryptedStr
}
}
let encodeStr = encodeAes('hello word');
console.log('加密后:' + encodeStr);
let decodeStr = decodeAes(encodeStr);
console.log('解密后:' + decodeStr);
Java代码
import java.security.MessageDigest;
import java.security.Security;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
/**
* @Description
* @Date 13:43 2020/12/8
**/
public class AesGcmUtil {
/**
* @Description 16位的密钥
* @Date 13:43 2020/12/8
**/
public static final String KEY = "0123456789ABCDEF";
private static final String IV = "0123456789ABCDEF";
private static final String ALGORITHMSTR = "AES/GCM/NoPadding";
private static final String DEFAULT_CODING = "utf-8";
/**
* 如果报错java.security.NoSuchProviderException: no such provider: BC,那么需要加上这一段,同时需要bcprov-jdk15on.jar
*/
static {
Security.addProvider(new BouncyCastleProvider());
}
/**
* @Description 加密
* @Date 14:25 2020/12/7
**/
public static String aesEncrypt(String content) throws Exception {
byte[] input = content.getBytes(DEFAULT_CODING);
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(KEY.getBytes(DEFAULT_CODING));
SecretKeySpec skc = new SecretKeySpec(thedigest, "AES");
IvParameterSpec ivspec = new IvParameterSpec(IV.getBytes(DEFAULT_CODING));
Cipher cipher = Cipher.getInstance(ALGORITHMSTR, "BC");
cipher.init(Cipher.ENCRYPT_MODE, skc, ivspec);
byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
ctLength += cipher.doFinal(cipherText, ctLength);
return Base64.getEncoder().encodeToString(cipherText);
}
/**
* @Description 解密
* @Date 14:26 2020/12/7
**/
public static String aesDecrypt(String tmp) throws Exception {
byte[] keyb = KEY.getBytes(DEFAULT_CODING);
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(keyb);
SecretKeySpec skey = new SecretKeySpec(thedigest, "AES");
IvParameterSpec ivspec = new IvParameterSpec(IV.getBytes(DEFAULT_CODING));
Cipher dcipher = Cipher.getInstance(ALGORITHMSTR, "BC");
dcipher.init(Cipher.DECRYPT_MODE, skey, ivspec);
byte[] clearbyte = dcipher.doFinal(Base64.getDecoder().decode(tmp));
return new String(clearbyte, DEFAULT_CODING);
}
public static void main(String[] args) throws Exception {
String origin = "这是Java端的加密解密";
String encryptstr = aesEncrypt(origin);
System.out.println(encryptstr);
String decryptstr = aesDecrypt(encryptstr);
System.out.println(decryptstr);
}
}
就
先
说
到
这
\color{#008B8B}{ 就先说到这}
就先说到这
在
下
A
p
o
l
l
o
\color{#008B8B}{在下Apollo}
在下Apollo
一
个
爱
分
享
J
a
v
a
、
生
活
的
小
人
物
,
\color{#008B8B}{一个爱分享Java、生活的小人物,}
一个爱分享Java、生活的小人物,
咱
们
来
日
方
长
,
有
缘
江
湖
再
见
,
告
辞
!
\color{#008B8B}{咱们来日方长,有缘江湖再见,告辞!}
咱们来日方长,有缘江湖再见,告辞!