【问题标题】:Using Kalium on Server and libsodium.js on client在服务器上使用 Kalium,在客户端使用 libsodium.js
【发布时间】:2016-10-11 12:51:59
【问题描述】:

我是尝试 NaCl 包装的新手。我在我的服务器上使用 Kalium,在我的客户端上使用 libsodium.js,它们都在工作,但是当我尝试使用经过身份验证的加密在两端之间进行通信时,密文验证失败。 客户端上的加密由:

var nonce=sodium.crypto_generichash(sodium.crypto_box_NONCEBYTES, dataObj.extensionId);
var message="test";
var encryptedString = sodium.crypto_box_easy(message, nonce, serverPublicKeyBytes, clientPrivateKey);

nonce、serverPublicKeyBytes 和 clientPrivateKey 作为 Base64 字符串传输到服务器。

在服务器中,数据被解密使用:

public byte[] decrypt( byte[] publicKey, byte[] privateKey, byte[] nonce,
            byte[] message) throws Exception {
        Box box = new Box(publicKey, privateKey);
        byte[] output= box.decrypt(nonce, message);

        return output;
    }

在服务器中,包装器使用 java byte[],而在服务器上,javascript 使用 UInt8Array[],有人可以帮助我启用客户端-服务器通信。

提前致谢

【问题讨论】:

    标签: javascript java encryption libsodium


    【解决方案1】:

    经过反复试验,我发现 kalium 中的默认 Box 类不适用于 sodium.crypto_box_easy 的 javascript 版本 ...

    将 kalium 项目克隆到您的环境中,并将以下代码添加到 NaCl 类中:

    public int crypto_box_easy(@Out byte[] ciphertext, @In byte[] plaintext, @In @u_int64_t int plaintextLength,
                    @In byte[] nonce, @In byte[] receiverPublicKey, @In byte[] senderPrivateKey);
    
            public int crypto_box_open_easy(@Out byte[] plaintext, @In byte[] ciphertext,
                    @In @u_int64_t int ciphertextLength, @In byte[] nonce, @In byte[] senderPublicKey,
                    @In byte[] receiverPrivateKey);
    

    然后在 Box 类中调用此代码或创建您自己的类:

    isValid(sodium().crypto_box_easy(ciphertext, plaintext, plaintext.length, nonce, receiverPublicKey,
                    senderPrivateKey), "Encryption failed");
    isValid(sodium().crypto_box_open_easy(plaintext, ciphertext, ciphertext.length, nonce, senderPublicKey,
                receiverPrivateKey), "Decryption failed. Ciphertext failed verification.");
    

    上述代码中的密文(crypto_box_easy)是一个新的byte[]对象,长度= crypto_box_curve25519xsalsa20poly1305_MACBYTES(16) + plaintext长度。 上述代码中的明文(crypto_box_open_easy)是一个新的byte[]对象,其长度为原始明文的长度

    【讨论】:

      猜你喜欢
      • 2016-10-04
      • 1970-01-01
      • 2017-06-14
      • 1970-01-01
      • 2013-04-27
      • 2013-03-08
      • 1970-01-01
      • 1970-01-01
      • 2015-07-22
      相关资源
      最近更新 更多