CCCrypt返回nil的最大时间。加密有什么问题

时间:2019-06-16 10:27:53

标签: encryption swift4 aes cryptoswift

我试图编写基于CCCrypt的加密(AESCBC128)函数,而CCCrypt正在生成随机值。

例如,当我将016768821221传递给函数时,对于相同的iv和Key,它将返回“ 0oTPFcKNWABTpBGgLlzsjw ==“。

这是iv:“ khabbababab”,密钥是:“ khabbababab”

为什么返回nil。加密是正确的,但有时会返回nil

extension String {

func aesEncrypt(key:String, iv:String, options:Int = kCCOptionPKCS7Padding) -> String? {
    if let keyData = key.data(using: String.Encoding.utf8),
        let data = self.data(using: String.Encoding.utf8),
        let cryptData    = NSMutableData(length: Int((data.count)) + kCCBlockSizeAES128) {
        let keyLength              = kCCKeySizeAES128
        let operation: CCOperation = UInt32(kCCEncrypt)
        let algoritm:  CCAlgorithm = CCOptions(kCCAlgorithmAES128)
        let options:   CCOptions   = UInt32(options)
        var numBytesEncrypted :size_t = 0
        let cryptStatus = CCCrypt(operation,
                                  algoritm,
                                  CCOptions(options),
                                  (keyData as NSData).bytes, keyLength,
                                  iv,
                                  (data as NSData).bytes, data.count,
                                  cryptData.mutableBytes, cryptData.length,
                                  &numBytesEncrypted)
        print(String(data: data, encoding: .utf8) as Any)
        if UInt32(cryptStatus) == UInt32(kCCSuccess) {
            cryptData.length = Int(numBytesEncrypted)
            let base64cryptString = cryptData.base64EncodedString(options: .lineLength64Characters)
            print("succccc")
            return base64cryptString
        }
        else {
            print("nill nill llllllllll")
            return nil
        }
    }
    return nil
}

func aesDecrypt(key:String, iv:String, options:Int = kCCOptionPKCS7Padding) -> String? {
    if let keyData = key.data(using: String.Encoding.utf8),
        let data = NSData(base64Encoded: self, options: .ignoreUnknownCharacters),
        let cryptData    = NSMutableData(length: Int((data.length)) + kCCBlockSizeAES128) {

        let keyLength              = size_t(kCCKeySizeAES128)
        let operation: CCOperation = UInt32(kCCDecrypt)
        let algoritm:  CCAlgorithm = UInt32(kCCAlgorithmAES128)
        let options:   CCOptions   = UInt32(options)

        var numBytesEncrypted :size_t = 0

        let cryptStatus = CCCrypt(operation,
                                  algoritm,
                                  options,
                                  (keyData as NSData).bytes, keyLength,
                                  iv,
                                  data.bytes, data.length,
                                  cryptData.mutableBytes, cryptData.length,
                                  &numBytesEncrypted)

        if UInt32(cryptStatus) == UInt32(kCCSuccess) {
            cryptData.length = Int(numBytesEncrypted)
            let unencryptedMessage = String(data: cryptData as Data, encoding:String.Encoding.utf8)
            return unencryptedMessage
        }
        else {
            return nil
        }
    }
    return nil
}

1 个答案:

答案 0 :(得分:0)

您的aesEncrypt始终将keylength的16传递给CCCrypt,而无需检查密钥实际上是否至少有16个字节的数据。