首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tweetnacl.js与TweetNaclFast (java)混合用于非对称加密

tweetnacl.js与TweetNaclFast (java)混合用于非对称加密
EN

Stack Overflow用户
提问于 2020-09-03 16:26:13
回答 2查看 595关注 0票数 1

我们的项目是使用带有nacl.box和临时密钥的非对称加密:

代码语言:javascript
复制
    encrypt(pubKey, msg) {
        if (typeof msg !== 'string') {
            msg = JSON.stringify(msg)
        }
        let ephemKeys = nacl.box.keyPair()
        let msgArr = nacl.util.decodeUTF8(msg)
        let nonce = nacl.randomBytes(nacl.box.nonceLength)
        p(`naclRsa.pubKey=${this.pubKey}`)
        let encrypted = nacl.box(
            msgArr,
            nonce,
            nacl.util.decodeBase64(pubKey),
            ephemKeys.secretKey
        )
        let nonce64 = nacl.util.encodeBase64(nonce)
        let pubKey64 = nacl.util.encodeBase64(ephemKeys.publicKey)
        let encrypted64 = nacl.util.encodeBase64(encrypted)
        return {nonce: nonce64, ephemPubKey: pubKey64, encrypted: encrypted64}
    }

我们目前有node.js应用程序,然后对这些消息进行解密。我们希望在某些特性中使用jvm语言。tweet-nacljvm上似乎没有老牌球员的丰富,但似乎如此。

及其建议的执行情况

°tweetnacl-fast https://github.com/InstantWebP2P/tweetnacl-java/blob/master/src/main/java/com/iwebpp/crypto/TweetNaclFast.java

很受欢迎。

目前还不清楚在这个库中,与使用短暂密钥的asymmetric加密类似的是什么。它支持吗?请注意,如果javakotlintweetnacl-java中不受支持,那么我将对其开放。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-09-04 15:52:23

是的港口。因此,预期两者提供相同的功能。至少对于posted方法是这样的,可以用TweetNaclFast在Java端实现,如下所示:

代码语言:javascript
复制
import java.nio.charset.StandardCharsets;
import java.util.Base64;

import com.iwebpp.crypto.TweetNaclFast;
import com.iwebpp.crypto.TweetNaclFast.Box;
import com.iwebpp.crypto.TweetNaclFast.Box.KeyPair;

...

private static EncryptedData encrypt(byte[] pubKey, String msg) {
    KeyPair ephemKeys = Box.keyPair();
    byte[] msgArr = msg.getBytes(StandardCharsets.UTF_8);
    byte[] nonce = TweetNaclFast.randombytes(Box.nonceLength);
    
    Box box = new Box(pubKey, ephemKeys.getSecretKey());
    byte[] encrypted = box.box(msgArr, nonce);
    
    String nonce64 = Base64.getEncoder().encodeToString(nonce);
    String ephemPubKey64 = Base64.getEncoder().encodeToString(ephemKeys.getPublicKey());
    String encrypted64 = Base64.getEncoder().encodeToString(encrypted);
    return new EncryptedData(nonce64, ephemPubKey64, encrypted64);
}

...

class EncryptedData {
    public EncryptedData(String nonce, String ephemPubKey, String encrypted) {
        this.nonce = nonce;
        this.ephemPubKey = ephemPubKey;
        this.encrypted = encrypted;
    }
    public String nonce;
    public String ephemPubKey;
    public String encrypted;
}

为了证明双方是兼容的,在以下示例中,将在Java端加密明文,并在JavaScript端解密:

  • --首先,JavaScript端需要一对密钥,它的公钥(publicKeyJS)被传递到JavaScript端。JavaScript侧的密钥对可以按以下方式生成:

设keysJS = nacl.box.keyPair();secretKeyJS = keysJS.secretKey;publicKeyJS = keysJS.publicKey;console.log(秘密密钥:+ nacl.util.encodeBase64(secretKeyJS));console.log(公钥:+ nacl.util.encodeBase64(publicKeyJS));

具有以下示例输出:

密钥: BDXNKDHeq0vILm8oawAGAQtdIsgwethzBTBqmsWI+R8=:YTxAFmYGm4yV2OP94E4pcD6LSsN4gcSBBAlU105l7hw=公钥

  • Java端的加密使用上面发布的encrypt方法(和publicKeyJS):

Base64.getDecoder().decode("BDXNKDHeq0vILm8oawAGAQtdIsgwethzBTBqmsWI+R8=");publicKeyJS = EncryptedData encryptedFromJava = encrypt(publicKeyJS,“我有一种感觉我们已经不在堪萨斯了……”);System.out.println("Nonce:“+ encryptedFromJava.nonce);System.out.println(”临时公钥:“+ encryptedFromJava.ephemPubKey);System.out.println(”密文:“+ encryptedFromJava.encrypted);

具有以下示例输出:

即时公钥:Mde+9 metwF1jIEij5rZDHjStR/pd4BN9p5JbZleSg=密文: hHo7caCxTU+hghcFZFv+djAkSlWKnC12xj82V2R/Iz9GdOMoTzjoCDcz9m/KbRN6i5dkYi3+Gf0YTtKlZQWFooo=

JS端的解密给出了原始明文(使用secretKeyJS): )

让FcdzXfYwSbI0nq2WXsLe9aAh94vXSoWd= "FcdzXfYwSbI0nq2WXsLe9aAh94vXSoWd";让ephemPubKey = console.log(nacl.util.encodeUTF8(decryptedFromJS));加密=console.log(nacl.util.encodeUTF8(decryptedFromJS));secretKeyJS = nacl.util.decodeBase64("YTxAFmYGm4yV2OP94E4pcD6LSsN4gcSBBAlU105l7hw=");让decryptedFromJS =解密(secretKeyJS,{ nonce : nonce,ephemPubKey: ephemPubKey,encrypted: encrypted});console.log(nacl.util.encodeUTF8(decryptedFromJS));//我有一种感觉,我们已经不在堪萨斯了……函数解密( secretKey,密文){让解密= nacl.box.open( nacl.util.decodeBase64(ciphertext.encrypted),nacl.util.decodeBase64(ciphertext.nonce),nacl.util.decodeBase64 secretKey );返回解密;}

票数 1
EN

Stack Overflow用户

发布于 2021-08-23 16:34:57

我的tweetnacl-java (Kudos to @topaco)的完整代码

我生成了两个随机密钥对,并将它们的密钥保存在application.properties文件中,这样,我将始终拥有相同的pub&sec和nonce。Base64.getEncoder().encodeToString(ephemeralKeyPair.getSecretKey()); baseKeyPair= Box.keyPair();String baseKeyPairSecretKey = Base64.getEncoder().encodeToString(nonce); KeyPair ephemeralKeyPair= Box.keyPair();String ephemeralKeyPairSecretKey =Base64.getEncoder().encodeToString(ephemeralKeyPair.getSecretKey()); byte[] nonce = TweetNaclFast.randombytes(Box.nonceLength);String Box.nonceLength=Base64.getEncoder().encodeToString(nonce);

代码语言:javascript
复制
 private final AppConfig config; //you can autowire the config class 

 private TweetNaclFast.Box.KeyPair getBaseKeyPair() {
        byte[] secretKey = Base64.getDecoder().decode(config.getTweetNACLConfig().getBaseSecretKey());
        return TweetNaclFast.Box.keyPair_fromSecretKey(mySecretKey);
    }


    private TweetNaclFast.Box.KeyPair getEphemeralKeyPair() {
        byte[] secretKey = Base64.getDecoder().decode(config.getTweetNACLConfig().getEphemeralSecretKey());
        return TweetNaclFast.Box.keyPair_fromSecretKey(mySecretKey);
    }


    private byte[] getNonce() {
        return Base64.getDecoder().decode(config.getTweetNACLConfig().getNonce().getBytes(StandardCharsets.UTF_8));
    }

    public String encrypt(String msg) {
        TweetNaclFast.Box.KeyPair baseKeyPair = getBaseKeyPair();
        TweetNaclFast.Box.KeyPair ephemeralKeyPair = getEphemeralKeyPair();
        byte[] msgArr = msg.getBytes(StandardCharsets.UTF_8);
        byte[] nonce = getNonce();
        TweetNaclFast.Box box = new TweetNaclFast.Box(baseKeyPair.getPublicKey(), ephemeralKeyPair.getSecretKey());
        byte[] encryptedData = box.box(msgArr, nonce);
        return Base64.getEncoder().encodeToString(encryptData);
    }


    public String decrypt(String encryptedData) {
        TweetNaclFast.Box.KeyPair baseKeyPair = getBaseKeyPair();
        TweetNaclFast.Box.KeyPair ephemeralKeyPair = getEphemeralKeyPair();
        byte[] nonce = getNonce();
        TweetNaclFast.Box box = new TweetNaclFast.Box(ephemeralKeyPair.getPublicKey(), baseKeyPair.getSecretKey());
        byte[] boxToOpen = Base64.getDecoder().decode(encryptedData);
        byte[] decryptedData = box.open(boxToOpen, nonce);
        return new String(decryptedData, StandardCharsets.UTF_8);
    }

> Please, note these two lines
> TweetNaclFast.Box box = new TweetNaclFast.Box(baseKeyPair.getPublicKey(), ephemeralKeyPair.getSecretKey());
> TweetNaclFast.Box box = new TweetNaclFast.Box(ephemeralKeyPair.getPublicKey(), baseKeyPair.getSecretKey());

return encryptAndDecryptData.encrypt("Friday"); // JHo/tk/Jpp2rpxpzIIgBhVhK/CBZLg==
return encryptAndDecryptData.decrypt("JHo/tk/Jpp2rpxpzIIgBhVhK/CBZLg==") //Friday
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63727899

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档