0

I'm new to iOS and I don't have idea about how to encrypt string using AES 256 with ECB mode and padding I take look of cryptoswift but I get error of key length I have 64 character key and I'm not able to encrypt

func aes_Encrypt(AES_KEY: String) -> String {
    var result = ""
    do {
        let key: [UInt8] = Array(AES_KEY.utf8) as [UInt8]
        let aes = try! AES(key: key, blockMode: ECB() as BlockMode, padding: .pkcs5) 
        let encrypted = try aes.encrypt(Array(self.utf8))
        result = encrypted.toHexString()
        print("AES Encryption Result: \(result)")
    } catch {
        print("Error: \(error)")
    }
    return result
}
M Reza
  • 18,350
  • 14
  • 66
  • 71
kruti
  • 47
  • 1
  • 8

1 Answers1

0

64 characters × 8 bits = 512 bits, not the 256 required for AES256.

If the string you are passing in is a hexadecimal representation of a key (e.g. "1234abcd…"), you will need to break it into a sequence of two-character substrings and use UInt8(…, radix:16) to parse each one as hexadecimal.

  • i have 5 (e.g. "12345") character key and convert it into 16 byte SHA 256 can you please provide code for that – kruti Jan 24 '19 at 03:35