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

}

注:数据有出入请以实际为准。

第一次写文章,希望能帮到有缘人。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值