2017-03-01 46 views
0

我想用给定的公钥加密使用swift钠的值。 但是,加密值与服务器端生成的值不同。 我不确定这段代码是否正确无误。 这些步骤与在java中完成的步骤类似。Swift钠 - 匿名加密(密封盒)

假设公钥采用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)) 

请告诉我知道什么是错的迅速相当于编码。 非常感谢。

回答