java gcm_Java實現AES-GCM解密,JS實現AES-GCM加密。

JS實現AES-GCM加密

首先我們先引用asmcrypto.js來實現JS端的加密。

const asmcrypto = require('asmcrypto.js')

有了asmcrypto 物件我們就可以呼叫它的加密方法

const encText = asmcrypto.AES_GCM.encrypt(text, key, nonce, aad)

text:需要加密的明文

key:秘鑰

nonce:初始向量

add:訊息認證

其中add可以省略。

這四個引數的資料型態都要是Uint8Array。

static encrypt(cleartext: Uint8Array, key: Uint8Array, nonce: Uint8Array, adata?: Uint8Array, tagsize?: number): Uint8Array;

第一步:將text裝換成BASE64編碼

const text_btoa = window.btoa(「Hello Word」)

第二步:將BASE64字串轉換成Uint8Array

const text = asmcrypto.base64_to_bytes(text_btoa)

第三步:產生一組長度16的秘鑰

const keyArr = [23, 113, 57, 6, 35, -69, -60, 98, 84, -17, -125, -49, 18, 6, -92, 32]

註:這組秘鑰與java端相對應,秘鑰可以使用相同的進行加密,初始向量最好不要使用相同的。

第四步:將秘鑰轉換成Uint8Array

const key = new Uint8Array(keyArr)

第五步:生成一組亂數並轉換成Uint8Array

const arrayRound = getArrayRound()

const nonce = new Uint8Array(arrayRound)

生成亂數程式碼:

export function getArrayRound() {

let arr = [];

for (let i = 0; i < 12; i++) {

const randomNum6 = Math.round(Math.random() * 128)

arr.push(randomNum6)

}

return arr

}

第六步:生成訊息認證並轉換成Uint8Array

const adata = [87, 104, 97, 116, 101, 118, 101, 114, 32, 73, 32, 108, 105, 107, 101]

const aad = new Uint8Array(adata)

與java端相對應不是隨意出現

第七步:將加密後的密文(Uint8Array)轉換成BASE64字串

const encText = asmcrypto.AES_GCM.encrypt(text, key, nonce, aad)

const cipherText = asmcrypto.bytes_to_base64(encText)

到此,使用asmcrypto.js實現AES-GCM加密就已經結束,但要實現跨語言java解密還遠遠不夠。

補充js端解密:

const decText = asmcrypto.AES_GCM.decrypt(encText , key, nonce, aad)

相同的key,初始向量(iv/nonce)訊息認證。

java實現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」是種子數,相同的種子會生成相同的亂數。呼叫方法

key.getEncoded()

可知key的byte[]與js中的秘鑰一致。

第二步:生成初始向量

final byte[] nonce = new byte[12];

secureRandom.nextBytes(nonce);

spec = new GCMParameterSpec(16 * 8, nonce);

這裡的nonce與js相對應

第三步:生成訊息認證

byte[] aad = "Hello word".getBytes(StandardCharsets.UTF_8);

cipher.updateAAD(aad);

與js端對應。

第四步:解密

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))

}

註:資料有出入請以實際為準。

第一次寫文章,希望能幫到有緣人。