CryptLib解密返回错误的值

时间:2017-05-30 09:32:14

标签: swift encryption swift3 cryptography

我在我的swift应用程序中使用Cross-Platform-AES进行AES加密/解密。我的加密工作正常。但是当我尝试解密服务返回值时,它并没有给我正确的结果。这是我如何进行解密。

public func decryptStrings(text:String)->String{

    let hashKey=cryptoLib.sha256(key, length: 31)
    let decryptedData = cryptoLib.decrypt(text.data(using: String.Encoding.utf8), key: hashKey, iv: iv)
    let decryptedString=decryptedData?.base64EncodedString()
    print("decryptedString \(decryptedString! as String)")
    return decryptedString!

}

请解释我如何以正确的方式解密这个问题。 感谢

加密

public func base64Convertion (secretcode:String)->String
{


    let hashKey=cryptoLib.sha256(key, length: 31)
    let encryptedData=cryptoLib.encrypt(secretcode.data(using: String.Encoding.utf8), key: hashKey, iv: iv)
    let encryptedString=encryptedData?.base64EncodedString()
    print("encryptedString \(encryptedString! as String)")


    return encryptedString!

}

1 个答案:

答案 0 :(得分:0)

我通过以这种方式更改解密方法来解决问题

    public func decryptStrings(text:String)->String{

    let hashKey=cryptoLib.sha256(key, length: 31)
    let decodedData = Data(base64Encoded: text, options: Data.Base64DecodingOptions())
    let decryptedData = cryptoLib.decrypt(decodedData, key: hashKey, iv: iv)
    let decryptedString=String(data: decryptedData!,encoding:String.Encoding.utf8)
    print("decryptedString \(decryptedString!)")
    return decryptedString!

}