【问题标题】:Swift Sodium - Anonymous Encryption (Sealed Boxes)Swift Sodium - 匿名加密(密封盒)
【发布时间】:2017-03-01 04:57:11
【问题描述】:

我正在尝试使用带有给定公钥的快速钠来加密值。 但是,加密值与服务器端生成的值不同。 我不确定这段编码是否正确。 步骤与java中的类似。

假设公钥是base64字符串格式。

Java:

String pubKey = "w6mjd11n9w9ncKfcuR888Ygi02ou+46ocIajlcUEmQ="; 
String secret = "hithere"
byte[] pubKeyBytes = Base64.decode(pubKey,0);
SealedBox sealedDeal = new SealedBox(pubKeyBytes);
byte[] c = sealedDeal.encrypt(secret.getBytes());
String ciphertext = Base64.encodeToString(c, 0);  

斯威夫特:

let pubKey = "w6mjd11n9w9ncKfcuR888Ygi02ou+46ocIajlcUEmQ="
let dataDecoded:NSData = NSData(base64Encoded: pubKey, options: NSData.Base64DecodingOptions(rawValue: 0))!
let secret = "hithere".toData()!
let c : Data = sodium.box.seal(message: secret, recipientPublicKey: dataDecoded as Box.PublicKey)!
let ciphertext = c.base64EncodedString(options: .init(rawValue: 0))

请告诉我 swift 等效编码有什么问题。 非常感谢。

【问题讨论】:

    标签: swift libsodium


    【解决方案1】:

    加密的值应该是不同的,因此从等效明文产生的密文是无法区分的(请参阅Ciphertext indistinguishability)。

    【讨论】:

      【解决方案2】:

      sodium.box.seal 每次加密消息时都会在内部生成新的nonce,@Max 是对的,这是正常行为

      您可以使用Detached mode 给出相同的随机数,但这是一个非常糟糕的主意

      在你的例子中你使用了Anonymous Encryption我建议你看看Authenticated Encryption

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-09-18
        • 2017-08-31
        • 2017-12-18
        • 2014-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多