无法使用CryptoSwift

时间:2015-10-26 09:58:11

标签: ios swift encryption aes cryptoswift

用于加密的字符串" secret"

加密后#34; 64c2VjcmV0"

这是正常运行的代码

    let inputNSData: NSData = input.dataUsingEncoding(NSUTF8StringEncoding)!
    let inputBytes: [UInt8] = inputNSData.arrayOfBytes()
    let key: [UInt8] = self.generateArray("secret0key000000") //16
    let iv: [UInt8] = self.generateArray("0000000000000000")  //16
    do {
        let encrypted: [UInt8] = try AES(key: key, iv: iv, blockMode: .CBC).encrypt(inputBytes, padding: PKCS7())    
        let decrypted: [UInt8] = try AES(key: key, iv: iv, blockMode: .CBC).decrypt(encrypted, padding: PKCS7())   
        let decryptNsData: NSData = NSData(bytes: decrypted, length: decrypted.count)     
        let c = decryptNsData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
        let decryptedString: String = NSString(data: decryptNsData, encoding: NSUTF8StringEncoding) as! String
        print("String after decryption\t\(decryptedString)")
    } catch {
        // some error
    }

但我无法使用相同的密钥和iv解密 我遇到了致命错误:在解包加密字符串的可选值时意外地发现了nil

    let key: [UInt8] = self.generateArray("secret0key000000") //16
    let iv: [UInt8] = self.generateArray("0000000000000000")  //16
    let input: String = "64c2VjcmV0"

    var encryptedStrData = NSData(base64EncodedString: input, options: NSDataBase64DecodingOptions())!
    let inputBytes: [UInt8] = encryptedStrData.arrayOfBytes()
    print("String in uint8\(inputBytes)")
    //var keyData = keyStr.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
    //var ivData:NSData = ivStr.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
    do{
    let decryptedTryData = try AES(key: key, iv: iv, blockMode: .CBC).decrypt(inputBytes)
        print(decryptedTryData)
    }
    catch{

    }

我遇到致命错误:在解包加密字符串的可选值时意外发现nil

3 个答案:

答案 0 :(得分:4)

在没有必要时使用Base64,只有Base64编码非字符串数据。

这是第一个测试代码:

let inputBytes: [UInt8] = Array("secret".utf8)
let key:        [UInt8] = Array("secret0key000000".utf8) //16
let iv:         [UInt8] = Array("0000000000000000".utf8)  //16

var encryptedBase64 = ""
do {
    let encrypted: [UInt8] = try AES(key: key, iv: iv, blockMode: .CBC).encrypt(inputBytes, padding: PKCS7())
    let encryptedNSData = NSData(bytes: encrypted, length: encrypted.count)
    encryptedBase64 = encryptedNSData.base64EncodedStringWithOptions([])

    let decrypted: [UInt8] = try AES(key: key, iv: iv, blockMode: .CBC).decrypt(encrypted, padding: PKCS7())
    let result = String(bytes: decrypted, encoding: NSUTF8StringEncoding)!
    print("result\t\(result )")
} catch {
    // some error
}
print("encryptedBase64: \(encryptedBase64)")

输出:

  

结果:秘密
  encryptedBase64:0OCxa0yJszq9MvkrWjn3wg ==

let encryptedData = NSData(base64EncodedString: encryptedBase64, options:[])!
print("decodedData: \(encryptedData)")
let encrypted = Array(UnsafeBufferPointer(start: UnsafePointer<UInt8>(encryptedData.bytes), count: encryptedData.length))

do {
    let decryptedData = try AES(key: key, iv: iv, blockMode: .CBC).decrypt(encrypted)
    let decryptedString = String(bytes: decryptedData, encoding: NSUTF8StringEncoding)!
    print("decryptedString: \(decryptedString)")

}
catch{
    // some error
}

输出:

  

decryptedString:secret

注意:

不要使用CryptoSwift,它不使用内置加密硬件,并且比Apple Security.framework Common Crypto <强> 400到1000倍。它也没有经过严格的审查和使用非认证的加密代码。

不要直接使用字符串作为密钥,它不安全。而是使用PBKDK2(基于密码的密钥派生函数)从字符串中派生密钥。

答案 1 :(得分:0)

Base64字符串必须可以被4整除。你的字符串不是。 您可以使用https://www.base64decode.org等网站来测试字符串。

答案 2 :(得分:0)

对于Swift3(避免UnsafePointer错误)这对我来说是第二部分(解码base64变量):

let encryptedData = NSData(base64Encoded: encryptedBase64, options:[])!
print("decodedData: \(encryptedData)")

let count = encryptedData.length / MemoryLayout<UInt8>.size

// create an array of Uint8
var encrypted = [UInt8](repeating: 0, count: count)
// copy bytes into array
encryptedData.getBytes(&encrypted, length:count * MemoryLayout<UInt8>.size)

do {
    let decryptedData = try AES(key: key, iv: iv, blockMode: .CBC).decrypt(encrypted)
    let decryptedString = String(bytes: decryptedData, encoding: String.Encoding.utf8)!
    print("decryptedString: \(decryptedString)")

}
catch{
    // some error
}