AES128 Encryption/Decryption Swift3 Support

Asked 2 years ago, Updated 2 years ago, 100 views

The following encryption and decryption methods are compatible with swift3.
It now crashes when converting the return value to String.

swift 2.2

 func AES 128 Crypt(data:NSData, keyData:NSData, operation:Int) ->NSData?{

    letkeyBytes=UnsafePointer<UInt8> (keyData.bytes)

    let dataLength=Int(data.length)
    let dataBytes = UnsafePointer<UInt8> (data.bytes)

    let cryptoData: NSMutableData! = NSMutableData(length: Int(dataLength) + kCCBlockSizeAES128)
    let cryptoPointer = UnsafeMutablePointer <UInt8> (cryptData.mutableBytes)
    let cryptoLength=size_t(cryptData.length)

    let keyLength = size_t (kCCKeySizeAES128)
    let allgoritm —CCAlgorithm=UInt32 (kCCAlgorithmAES128)
    lets options: CCOoptions=UInt32 (kCCOoptionECBMode+kCCOoptionPKCS7 Padding)

    var numBytesEncrypted:size_t=0

    let cryptoStatus = CCCrypt(CCOoperation(operation), algorithm,
                              options,
                              keyBytes, keyLength,
                              nil,
                              dataBytes, dataLength,
                              cryptoPointer, cryptoLength,
                              &numBytesEnencrypted)

    if UInt32(cryptStatus) == UInt32(kCCScess){

        cryptoData.length=Int(numBytesEnencrypted)

    } else{
        print("\"(#line)Error:\"(cryptStatus)")
    }

    return cryptoData
}

After Swift3

 func AES 128 Crypt(data:data,keyData:Data,operation:Int) ->Data?{

    letkeyBytes=(keyData as NSData).bytes.bindMemory(to:UInt8.self, capacity:keyData.count)

    let dataLength=Int(data.count)
    let dataBytes=(data as NSData).bytes.bindMemory(to:UInt8.self, capacity:data.count)

    let cryptoData: NSMutableData! = NSMutableData(length: Int(dataLength) + kCCBlockSizeAES128)
    // Change the following
    let cryptoInt=Int(bitPattern:cryptData.mutableBytes)
    guard let cryptoPointer = UnsafeMutablePointer <UInt8> (bitPattern:cryptInt) else {
        print("\"(#line)Error:cryptPointer is nil")
        return nil
    }
    // End of change
    let cryptoLength=size_t(cryptData.length)

    let keyLength = size_t (kCCKeySizeAES128)
    let allgoritm —CCAlgorithm=UInt32 (kCCAlgorithmAES128)
    lets options: CCOoptions=UInt32 (kCCOoptionECBMode+kCCOoptionPKCS7 Padding)

    var numBytesEncrypted:size_t=0

    let cryptoStatus = CCCrypt(CCOoperation(operation), algorithm,
                              options,
                              keyBytes, keyLength,
                              nil,
                              dataBytes, dataLength,
                              cryptoPointer, cryptoLength,
                              &numBytesEnencrypted)

    if UInt32(cryptStatus) == UInt32(kCCScess){

        cryptoData.length=Int(numBytesEnencrypted)

    } else{
        print("\"(#line)Error:\"(cryptStatus)")
    }

    return cryptoData as Data?
}

Where the error occurred

let encryptStr=String (data:encryptData, encoding: String.Encoding.utf8)!

Error Message

Thread1: EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, subcode=0xe7ffdefe)

Where the error indicates

->0x7ef9e0<+124>:trap

The reason for the error is that the return value of String(data:encryptData, encoding:String.Encoding.utf8) is nil, but I unwrapped it.
I don't know why encryptData contains the return value, but cannot convert it.

Also, I didn't notice it when I was checking the operation of the simulator, so
I think it will only happen on the actual machine.

In this case, I thought the encryption process was not correct.
I was told that there were other factors when I was looking at the Internet, so I asked if I was doing something wrong.
Is there a mistake in handling swift3?

swift ios swift3

2022-09-30 21:17

1 Answers

"If you think too hard about the ""preferably sampleable data, keyData"" part in the comment, I will correct the ""unrecommended writing"" and show you the original ""swift2.2"" code rewritten on Swift3."

"As it said, ""It should only occur on real machines,"" I tried both Debug and Release configurations, but the code worked fine."

 func AES 128 Crypt(data:data,keyData:Data,operation:Int) ->Data{

    let dataLength=data.count

    varcryptData=Data(count:dataLength+kCCBlockSizeAES128)
    let cryptoLength = cryptoData.count

    let keyLength = kCCKeySizeAES128
    let allgoritm = CCAlgorithm (kCCAlgorithmAES128)
    let options = CCOoptions (kCCOoptionECBMode + kCCOoptionPKCS7 Padding)

    var numBytesEnencrypted: Int=0

    let cryptoStatus=keyData.withUnsafeBytes {keyBytes in
        data.withUnsafeBytes {dataBytes in
            cryptoData.withUnsafeMutableBytes {cryptPointer in
                CCCrypt (CCOoperation (operation), algorithm,
                        options,
                        keyBytes, keyLength,
                        nil,
                        dataBytes, dataLength,
                        cryptoPointer, cryptoLength,
                        &numBytesEnencrypted)
            }
        }
    }

    if cryptoStatus==CCcryptorStatus(kCCScess){

        cryptoData.count = numBytesEnencrypted

    } else{
        print("\"(#line)Error:\"(cryptStatus)")
    }

    return cryptoData
}

If the same situation occurs with this code, it is likely that there is a problem with how to use String.init(data:encoding:) (the various datas), and if there is no problem replacing this code, there is a problem with using NSData.

If you have any new information, I will update it, so please try it and let me know.


2022-09-30 21:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.