ios - RNCryptor 无法在 iOS 中解密

标签 ios swift encryption swift3 rncryptor

我在我的应用程序中使用RNCryptor。我使用密码为纯文本生成了一个加密字符串,当我尝试使用密码解密加密字符串时,它没有给我原始的纯文本,而是给了我一些随机字符串。

我想我在字符串编码/解码中遗漏了一些东西,但我不确定我在这里遗漏了什么。

您能指导我解决这个问题吗?

代码

func encrypt(plainText : String, password: String) -> String {
    let data: Data = plainText.data(using: .utf8)!
    let encryptedData = RNCryptor.encrypt(data: data, withPassword: password)
    let encryptedString : String = encryptedData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength76Characters)
    return encryptedString
}

func decrypt(encryptedText : String, password: String) -> String {
    do  {

        let data: Data = Data.init(base64Encoded: encryptedText, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)!

        let decryptedData = try RNCryptor.decrypt(data: data, withPassword: password)
        let decryptedString : String = decryptedData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength76Characters)
        return decryptedString

    }
    catch {
        return "FAILED"
    }
}

 let plainText = "123456789"
 let password = "ABCDEFGHIJ"

 let encryptedText =  self.encrypt(plainText: plainText, password: password)
 let decryptedText = self.decrypt(encryptedText: encryptedText, password: password)

 print("ENCRYPTED TEXT : \(encryptedText)")
 print("DECRYPTED TEXT : \(decryptedText)")

ENCRYPTED TEXT : AwEsB6wlUSIJ31TAbaeAjVXP272zW89aa2rR9v6zYWwKUf6Hs5GSHekMKQT+n0vw6jMtjsQVhtzO 8AcqGpTLrQ9YR0PUS07P+8HboCp6Ge8UxQ==

DECRYPTED TEXT : MTIzNDU2Nzg5

最佳答案

只需尝试如下所示,它现在对我有用。

func encrypt(plainText : String, password: String) -> String {
        let data: Data = plainText.data(using: .utf8)!
        let encryptedData = RNCryptor.encrypt(data: data, withPassword: password)
        let encryptedString : String = encryptedData.base64EncodedString() // getting base64encoded string of encrypted data.
        return encryptedString
}

func decrypt(encryptedText : String, password: String) -> String {
        do  {
            let data: Data = Data(base64Encoded: encryptedText)! // Just get data from encrypted base64Encoded string.
            let decryptedData = try RNCryptor.decrypt(data: data, withPassword: password)
            let decryptedString = String(data: decryptedData, encoding: .utf8) // Getting original string, using same .utf8 encoding option,which we used for encryption.
            return decryptedString ?? ""
        }
        catch {
            return "FAILED"
        }
}

谢谢。

关于ios - RNCryptor 无法在 iOS 中解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48188827/

相关文章:

ios - Firebase 多位置更新最佳实践(包括示例)ios

ios - 以编程方式绘制的圆圈不会出现在 SwiftUI 中

azure - 能否仅将 Azure AMS V3 用于 DRM key 传送?

java - JCEKS keyStore 在 JavaTM Cryptography Extension 中使用的加密..?

ios - 如何使用 WKWebView 正确实现 didFailProvisionalNavigation?

ios - glBlendColor 不适用于 iOS

swift - swift 中的箭头运算符

ios - 换行时出现弹跳

python - 加密 : AssertionError ("PID check failed. RNG must be re-initialized after fork(). Hint: Try Random.atfork()")

ios - objective-c 方法的 xcode 预期选择器和 Missing @end 错误