ios - 使用 CryptoSwift 在 iOS 中加密/解密字符串
<p><p>我正在尝试加密一个字符串,然后使用 <a href="https://github.com/krzyzanowskim/CryptoSwift" rel="noreferrer noopener nofollow">CryptoSwift</a> 将其解密回来.代码如下:</p>
<pre><code> let key: =
let iv: =
let message = "Hello there!".dataUsingEncoding(NSUTF8StringEncoding)!.arrayOfBytes()
do {
let encryptedBytes = try ChaCha20(key: key, iv: iv)!.encrypt(message)
let decryptedBytes = try ChaCha20(key: key, iv: iv)!.decrypt(encryptedBytes)
let data = NSData.withBytes(decryptedBytes)
let decrypted = String(data: data, encoding: NSUTF8StringEncoding)
print(decrypted) // this should print "Hello there!", but it doesn't
}
catch { ... }
</code></pre>
<p>我认为这应该打印出原始字符串 <code>"Hello there!"</code>。但是我得到了这个:<code>(<48656c6c 6f207468 65726521>, 4)</code></p>
<p>任何想法我做错了什么?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我想通了。更改了这一行:</p>
<pre><code>let decrypted = String(data: data, encoding: NSUTF8StringEncoding)
</code></pre>
<p>到:</p>
<pre><code>let decrypted = String(bytes: decryptedBytes, encoding: NSUTF8StringEncoding)
</code></pre>
<p>它现在正在工作。</p></p>
<p style="font-size: 20px;">关于ios - 使用 CryptoSwift 在 iOS 中加密/解密字符串,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/31837847/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/31837847/
</a>
</p>
页:
[1]