swiftencryptionaescryptoswift

Decrypt AES in ECB mode


I'm trying to decrypt a simple frame :

uint8_t chipertext[] = {0x72, 0x82, 0xC9, 0xAA, 0x1F, 0xE1, 0x84, 0x97, 0x06, 0x65, 0x58, 0x5D, 0x06, 0x7B, 0xD4, 0xB4};

which was encrypted by the following key :

uint8_t aes_key[]    = {0x0A, 0x1A, 0x2A, 0x3A, 0x0B, 0x1B, 0x2B, 0x3B, 0x0C, 0x1C, 0x2C, 0x3C, 0x0D, 0x1D, 0x2D, 0x3D};

I know that the result will be (plaintext) :

uint8_t plaintext[]  = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};

Below my swift code :

Conversion Crypted message to String:

let hexString = "0x72,0x82,0xC9,0xAA,0x1F,0xE1,0x84,0x97,0x06,0x65,0x58,0x5D,0x06,0x7B,0xD4,0xB4"
// Remove all of the "0x"
let cleanString = hexString.replacingOccurrences(of: "0x", with: "")
// Create an array of hex strings
let hexStrings = cleanString.components(separatedBy: ",")
// Convert the array of hex strings into bytes (UInt8)
let bytes = hexStrings.compactMap { UInt8($0, radix: 16) }

let datar = String(decoding: bytes, as: UTF8.self)
let datas = Data(datar.utf8)
let datarToString = String(data: datas, encoding: String.Encoding.utf8)!

Conversion secret key to String:

 let hexStringKey = "0x0A,0x1A,0x2A,0x3A,0x0B,0x1B,0x2B,0x3B,0x0C,0x1C,0x2C,0x3C,0x0D,0x1D,0x2D,0x3D"
 // Remove all of the "0x"
 let cleanStringKey = hexStringKey.replacingOccurrences(of: "0x", with: "")
 // Create an array of hex strings
 let hexStringsKey = cleanStringKey.components(separatedBy: ",")
 // Convert the array of hex strings into bytes (UInt8)
 let bytesKey = hexStringsKey.compactMap { UInt8($0, radix: 16) }

 let datarKey = String(decoding: bytesKey, as: UTF8.self)
 let datasKey = Data(datarKey.utf8)
 let datarToStringKey = String(data: datasKey, encoding: String.Encoding.utf8)!

Then I use CryptoSwift to perform a AES decrypt function :

func decrypt(cryptedMessage: String , key:String) -> String? {
        var clearMessage:String? = nil;

        let cryptedData = Data(cryptedMessage.data(using: .utf8)!)

        do {
            let aes = try AES(key: Array<UInt8>(key.utf8), blockMode: ECB(), padding: .noPadding) // aes128
            let cipher = try aes.decrypt(Array<UInt8>(cryptedData))
            clearMessage = String(bytes: cipher, encoding: .utf8)
        } catch {
            let error = error as NSError
            print(error)
        }

        return clearMessage
    }

My issue is when I try to get the decrypted message like this :

let cryptedMessage = datarToString.decryptAES(key: datarToStringKey)

I get : Error: invalidData

If someone has an idea I do not understand where I'm doing wrong. I sure about ECB mode and noPadding options.

Thanks!


Solution

  • You cannot convert arbitrary data to a UTF-8 string. These lines are invalid, and you're just getting lucky that they don't crash.

    let datarToString = String(data: datas, encoding: String.Encoding.utf8)!
    let datarToStringKey = String(data: datasKey, encoding: String.Encoding.utf8)!
    

    Similarly, you should not be passing a String to decrypt. Just pass the Data for both the cipher text and the key.