简体   繁体   English

在节点 js 中通过 AES/GCM/NoPadding 算法使用密钥和 iv 加密有效负载并在 java 中解密

[英]Encrypt payload using a key and iv by AES/GCM/NoPadding algorithm in node js and decrypt in java

I have encrypt the file using node.js and decrypt in JAVA.我已经使用 node.js 加密文件并在 JAVA 中解密。 Decryption is done in JAVA using "AES/GCM/Nopadding" algorithm and it is third party app hence I cannot see the JAVA code.解密是在 JAVA 中使用“AES/GCM/Nopadding”算法完成的,它是第三方应用程序,因此我看不到 JAVA 代码。 I am encrypting the payload in node.js using "aes-128-gcm" algorithm.我正在使用“aes-128-gcm”算法加密 node.js 中的有效负载。 for this, I am try mimicking a working java encryption code为此,我尝试模仿一个有效的 Java 加密代码

I have tried with crypto and node-forge.我尝试过加密和节点锻造。 iam getting the output but am getting an error "Bad encryption - payload is not encrypted properly" when submitting payload.我正在获取输出,但在提交有效负载时收到错误“加密错误 - 有效负载未正确加密”。

pleas help me to find what I did wrong in this code.请帮我找出我在这段代码中做错了什么。

working code in java java中的工作代码

public void encrypt(@NonNull final byte[] payload, @NonNull final byte[] key) throws GeneralSecurityException
{
    SecretKeySpec codingKey = new SecretKeySpec(key, AES);
    Cipher cipher = AEC_GCM_THREAD_CIPHER.get();
    byte[] iv = new byte[cipher.getBlockSize()];
    RANDOM.nextBytes(iv);

    cipher.init(Cipher.ENCRYPT_MODE, codingKey, new IvParameterSpec(iv));
    final byte[] encryptedPayload = cipher.doFinal(payload);
    byte[] encryptMerchantKey = encryptMerchantKey(key);

    String payloadFinal = encodeToUrlString(encryptedPayload);    // final payload
    String ivFinal =  encodeToUrlString(iv);                  // final iv
    String keyFinal =  encodeToUrlString(encryptMerchantKey);  // final key

    System.out.println("Payload");
    System.out.println(payloadFinal);
    System.out.println("iv");
    System.out.println(ivFinal);
    System.out.println("key");
    System.out.println(keyFinal);
}

code iam tried in node js我在节点 js 中尝试过的代码

function encrypt(payload) {

    let key = forge.random.getBytesSync(16);
    let iv = forge.random.getBytesSync(16);

    let cipher = forge.cipher.createCipher("AES-GCM", key);
    cipher.start({ iv: iv});
    cipher.update(forge.util.createBuffer(payload));
    cipher.finish();

    let encrypted = forge.util.encode64(cipher.output.getBytes());
    let tag = forge.util.encode64(cipher.mode.tag.getBytes());
    let iv64 = forge.util.encode64(iv);

    let encryptedPayload = encrypted+tag;

    //RSA Encryption
    encryptedkey = RSAencrypt(forge.util.encode64(key));

     return {
     "payload" : base64url.fromBase64(encryptedPayload) ,
     "iv" : base64url.fromBase64(iv64).length,
     "key" : base64url.fromBase64(encryptedkey)
     };
}

Rsa description is working fine abling to decrypt the key. Rsa 描述工作正常,能够解密密钥。 some problem with aes encryption. aes 加密的一些问题。 as see the code, I added auth tag and encrypted data together but no use.如代码所示,我将 auth 标签和加密数据一起添加但没有用。

I have complete example of encryption and decryption in angular and java you can take this example as it is and make changes according to you.我有完整的 angular 和 java 加密和解密示例,您可以按原样使用此示例并根据您进行更改。
Install node-forge with command "npm install node-forge".使用命令“npm install node-forge”安装 node-forge。

encrypt(msg, pass) {
    const key = CryptoJS.lib.WordArray.random(8).toString();
    const iv =  CryptoJS.lib.WordArray.random(8).toString();

    // encrypt some bytes using GCM mode
    const cipher = forge.cipher.createCipher('AES-GCM', key);
    cipher.start({
        iv: iv, 
         additionalData: 'nvn', // optional
         tagLength: 128 // optional, defaults to 128 bits
    }); 
    cipher.update(forge.util.createBuffer(msg));
    cipher.finish();
    const encrypted = cipher.output;
    const encodedB64 = forge.util.encode64(encrypted.data);
    const tag = cipher.mode.tag; 
    const tagB64 = forge.util.encode64(tag.data);
    // outputs encrypted hex

    const trasmitmsg = key+iv+tagB64+encodedB64;
    return trasmitmsg
}

I have used CryptoJS to generate random string because random of node-forge giving nontransferable strings.我使用 CryptoJS 生成随机字符串,因为节点伪造的随机性给出了不可转移的字符串。

java code to decrypt this trasmitmsg is解密这个trasmitmsg的java代码是

public String getDecrypt(String transmsg) throws Exception {
    String keyString = transmsg.substring(0, 16);
    String ivString = transmsg.substring(16, 32);
    String additionalString = transmsg.substring(32, 56);
    String cipherString = transmsg.substring(56);

    byte[] keyBytes = keyString.getBytes();
    SecretKey key = new SecretKeySpec(keyBytes, "AES");
    byte[] ivBytes = ivString.getBytes();

    byte[] one = Base64.getDecoder().decode(cipherString);
    byte[] two = Base64.getDecoder().decode(additionalString);
    byte[] cipherText = ArrayUtils.addAll(one, two);
    return decrypt(cipherText, key, ivBytes);
}

public static String decrypt(byte[] cipherText, SecretKey key, byte[] IV) throws Exception {
    // Get Cipher Instance
    Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");

    // Create SecretKeySpec
    SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");

    // Create GCMParameterSpec
    GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH , IV);

    // Initialize Cipher for DECRYPT_MODE
    cipher.init(Cipher.DECRYPT_MODE, keySpec, gcmParameterSpec);

    cipher.updateAAD("nvn".getBytes());
    byte[] decryptedText = cipher.doFinal(cipherText);

    return new String(decryptedText);
}

Cheers!!!干杯!!!

The problem was with forge buffer it need to convert to node buffer this code is working now.问题在于伪造缓冲区需要转换为节点缓冲区,此代码现在正在运行。 thanks, @Maarten Bodewes for the advice.谢谢,@Maarten Bodewes 的建议。

function encrypt(payload) {

    //initialize forge random buffer
    var key = forge.random.getBytesSync(16);
    var iv = forge.random.getBytesSync(16);

    let cipher = forge.cipher.createCipher("AES-GCM", key);
    cipher.start({iv : iv});
    cipher.update(forge.util.createBuffer(payload));
    cipher.finish();

    let encrypted = cipher.output.data;
    let tag = cipher.mode.tag.data;
    let encryptedLoad = encrypted+tag;

    // node buffer and forge buffer differ, so the forge buffer must be converted to node Buffer            
    iv = Buffer.from(iv, "binary");
    encryptedLoad = Buffer.from(encryptedLoad, "binary");

    //Calling RSA Encryption
    encryptedKey = RSAencrypt(key);

    return {
     "payload" : base64url(encryptedLoad) ,
     "iv" : base64url(iv),
     "key" : base64url.fromBase64(encryptedKey)
     };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用“ AES / GCM / NoPadding”在节点中加密文件并在Java中解密。 基本上在node.js中相当于AES / GCM / NoPadding - Encrypt a file in node and decrypt in java using “AES/GCM/NoPadding”. Basically AES/GCM/NoPadding equivalent in node.js AES/GCM/NoPadding 从节点加密并在 java 上解密,抛出 AEADBadTagException:标签不匹配 - AES/GCM/NoPadding encrypt from node and decrypt on java, throw AEADBadTagException: Tag mismatch java AES加密然后使用相同的密钥和IV在PHP中解密 - java AES encryption then decrypt in PHP using same Key and IV 用JavaScript加密AES-GCM,用Java解密 - Encrypt AES-GCM in JavaScript, decrypt in Java 将 Java AES/GCM/NoPadding 算法转换为 C# - Convert Java AES/GCM/NoPadding algorithm to C# 如何在Java中使用未知的KEY和IV解密AES? - How to decrypt AES with an unknown KEY and IV in Java? AES/CFB/NOPADDING 加密解密不起作用 - AES/CFB/NOPADDING Encrypt to Decrypt not working 如何使用phpseclib加密和使用AES算法使用Java解密 - How to encrypt with phpseclib and decrypt with java using AES algorithm Java AES/GCM/NoPadding 加密在 doFinal 后不会增加 IV 的计数器 - Java AES/GCM/NoPadding encryption does not increment the counter of the IV after doFinal 在java中加密文件并使用密钥aes在openssl中解密 - Encrypt file in java and decrypt in openssl with key aes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM