JS實現(xiàn)AES-GCM加密
首先我們先引用asmcrypto.js來實現(xiàn)JS端的加密。
? ? const asmcrypto = require('asmcrypto.js')
有了asmcrypto 對象我們就可以調(diào)用它的加密方法
? ? const encText = asmcrypto.AES_GCM.encrypt(text, key, nonce, aad)
?text:需要加密的明文
?key:秘鑰
?nonce:初始向量
?add:消息認證
其中add可以省略。
這四個參數(shù)的數(shù)據(jù)類型都要是Uint8Array。
? ? ? static encrypt(cleartext: Uint8Array, key: Uint8Array, nonce: Uint8Array, adata?: Uint8Array, tagsize?: number): Uint8Array;
第一步:將text裝換成BASE64編碼
? ? ? const text_btoa = window.btoa(“Hello Word”)
?第二步:將BASE64字符串轉(zhuǎn)換成Uint8Array
? ? ? const text = asmcrypto.base64_to_bytes(text_btoa)
?第三步:產(chǎn)生一組長度16的秘鑰
? ? ? const keyArr = [23, 113, 57, 6, 35, -69, -60, 98, 84, -17, -125, -49, 18, 6, -92, 32]
? ? ? 注:這組秘鑰與java端相對應(yīng),秘鑰可以使用相同的進行加密,初始向量最好不要使用相同的。
第四步:將秘鑰轉(zhuǎn)換成Uint8Array
? ? ? const key = new Uint8Array(keyArr)
?第五步:生成一組隨機數(shù)并轉(zhuǎn)換成Uint8Array
? ? ? ? const arrayRound = getArrayRound()
? ? ? ? const nonce = new Uint8Array(arrayRound)
生成隨機數(shù)代碼:
? ? export function getArrayRound() {
? ? ? ? let arr = [];
? ? ? ? for (let i = 0; i < 12; i++) {
? ? ? ? ? ? const randomNum6 = Math.round(Math.random() * 128)
? ? ? ? ? ? arr.push(randomNum6)
? ? ? ? }
? ? ? ? return arr
? ? }
第六步:生成消息認證并轉(zhuǎn)換成Uint8Array
? ? ? ? const adata = [87, 104, 97, 116, 101, 118, 101, 114, 32, 73, 32, 108, 105, 107, 101]
? ? ? ? const aad = new Uint8Array(adata)
與java端相對應(yīng)不是隨意出現(xiàn)
?第七步:將加密后的密文(Uint8Array)轉(zhuǎn)換成BASE64字符串
? ? ? ? const encText = asmcrypto.AES_GCM.encrypt(text, key, nonce, aad)
? ? ? ? const cipherText = asmcrypto.bytes_to_base64(encText)
到此,使用asmcrypto.js實現(xiàn)AES-GCM加密就已經(jīng)結(jié)束,但要實現(xiàn)跨語言java解密還遠遠不夠。
補充js端解密:
? ? ? ? const decText = asmcrypto.AES_GCM.decrypt(encText , key, nonce, aad)
相同的key,初始向量(iv/nonce)消息認證。
?java實現(xiàn)AES-GCM解密
第一步:生成秘鑰
? ? ? KeyGenerator generator = KeyGenerator.getInstance(“AES”);
? ? ? SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
? ? ? secureRandom.setSeed(“123456”.getBytes(StandardCharsets.UTF_8));
? ? ? generator.init(secureRandom);
? ? ? key = generator.generateKey();
其中“123456”是種子數(shù),相同的種子會生成相同的隨機數(shù)。調(diào)用方法
? ? key.getEncoded()
可知key的byte[]與js中的秘鑰一致。
?第二步:生成初始向量
? ? final byte[] nonce = new byte[12];
? ? secureRandom.nextBytes(nonce);
? ? spec = new GCMParameterSpec(16 * 8, nonce);
這里的nonce與js相對應(yīng)
?第三步:生成消息認證
? ? byte[] aad = "Hello word".getBytes(StandardCharsets.UTF_8);
? ? cipher.updateAAD(aad);
與js端對應(yīng)。
?第四步:解密
? ? byte[] encryptStrBytes = cipher.doFinal(strBytes);
? ? return new String(encryptStrBytes, StandardCharsets.UTF_8);
完整Java代碼示例
? ? public static String getDecryptStringTest(String cipherText, String cipherNonce) {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? KeyGenerator generator = KeyGenerator.getInstance(“AES”);
? ? ? ? ? ? ? ? SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
? ? ? ? ? ? ? ? secureRandom.setSeed(“123456”.getBytes(StandardCharsets.UTF_8));
? ? ? ? ? ? ? ? generator.init(secureRandom);
? ? ? ? ? ? ? ? key = generator.generateKey();
? ? ? ? ? ? ? ? byte[] strBytes = Base64.getDecoder().decode(cipherText);
? ? ? ? ? ? ? ? //iv
? ? ? ? ? ? ? ? byte[] nonce = Base64.getDecoder().decode(cipherNonce);
? ? ? ? ? ? ? ? GCMParameterSpec decryptSpec = new GCMParameterSpec(16 * 8, nonce);
? ? ? ? ? ? ? ? Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
? ? ? ? ? ? ? ? cipher.init(Cipher.DECRYPT_MODE, key, decryptSpec);
? ? ? ? ? ? ? ? byte[] aad = "Hello Word".getBytes(StandardCharsets.UTF_8);
? ? ? ? ? ? ? ? cipher.updateAAD(aad);
? ? ? ? ? ? ? ? byte[] encryptStrBytes = cipher.doFinal(strBytes);
? ? ? ? ? ? ? ? return new String(encryptStrBytes, StandardCharsets.UTF_8);
? ? ? ? ? ? } catch (Exception e) {
? ? ? ? ? ? ? ? throw new EDSRuntimeException(e);
? ? ? ? ? ? }
?完整JS代碼示例
? ? //AES-128-GCM-ENCRYPT
? ? export function encrypt(word) {
? ? ? ? //Base64 encrypted plaintext
? ? ? ? const text_btoa = window.btoa(word)
? ? ? ? //Replace Base64 string with Uint8Array
? ? ? ? const text = asmcrypto.base64_to_bytes(text_btoa)
? ? ? ? //secret key corresponding to the Java version
? ? ? ? const keyArr = [73, 173, 77, 6, 375, -679, -670, 978, 84, -177, -1725, -479, 178, 6, -972, 372]
? ? ? ? const key = new Uint8Array(keyArr)
? ? ? ? //nonce
? ? ? ? const arrayRound = getArrayRound()
? ? ? ? const nonce = new Uint8Array(arrayRound)
? ? ? ? //adata
? ? ? ? const adata = [877, 1704, 977, 1176, 1071, 1178, 1071, 1174, 327, 773, 372, 1078, 1075, 1077, 1071]
? ? ? ? const aad = new Uint8Array(adata)
? ? ? ? //encrypt
? ? ? ? const encText = asmcrypto.AES_GCM.encrypt(text, key, nonce, aad)
? ? ? ? const cipherNonce = asmcrypto.bytes_to_base64(nonce)
? ? ? ? const cipherText = asmcrypto.bytes_to_base64(encText)
? ? ? ? const pwd = {
? ? ? ? ? ? cipherText: cipherText,
? ? ? ? ? ? cipherNonce: cipherNonce
? ? ? ? }
? ? ? ? return window.btoa(JSON.stringify(pwd))
? ? }
注:數(shù)據(jù)有出入請以實際為準。