在Java中加密和在Javascript中解密

时间:2013-09-12 18:46:04

标签: java javascript encryption cryptography

我想用Java加密字符串并解密它Javascript。我在javascript中尝试Crypto,但未正确解密。在Javascript中解密的最简单方法是什么?

我使用了以下链接中的加密代码:

http://bryox.blogspot.in/2011/12/encrypt-and-decrypt-string-by-java.html

1 个答案:

答案 0 :(得分:0)

  

用JavaScript解密的最简单方法是什么?

不安全。

或者,如果您正在寻找Java和Javascript中安全加密/解密的最简单方法,则可能需要看看libsodium,它具有{ {3}}和Java

使用LazySodium在Java中加密

LazySodiumJava lazySodium = new LazySodiumJava(new SodiumJava());
SecretBox.Lazy secretBoxLazy = (SecretBox.Lazy) lazySodium;

Key key = lazySodium.cryptoSecretBoxKeygen();

String msg = "This message needs top security";
byte[] nonce = lazySodium.nonce(SecretBox.NONCEBYTES);
lazySodium.cryptoSecretBoxEasy(msg, nonce, key);

使用Sodium-Plus在JavaScript中解密

const {SodiumPlus, CryptographyKey} = require('sodium-plus');
let sodium;

async function decryptMessage(ciphertextHex, nonceHex, keyHex) {
    if (!sodium) sodium = await SodiumPlus.auto();

    let ciphertext = Buffer.from(ciphertextHex, 'hex');
    let nonce = Buffer.from(nonceHex, 'hex');
    let key = CryptographyKey.from(keyHex, 'hex');
    return sodium.crypto_secretbox_open(ciphertext, nonce, key);
}

decryptMessage(ciphertext, nonce, key).then((plaintext) => {
    console.log(plaintext.toString());
});