0

I don't why I cannot make a AES encryption using the code below.

    let key="12345678900987654321564738290123".bytes;//32
    let iv="passwordpasswordwordpasswordpass".bytes;//32
    let message="Hello text fcrypt Hello text for enfor r encryptg ";
    print(message.count) //50
    let data = Padding.pkcs7.add(to: message.bytes, blockSize: AES.blockSize)
    do {
        let aes = try AES(key: key, blockMode: .CBC(iv: iv), padding: .pkcs7)
        let ciphertext = try aes.encrypt(data)
        print(ciphertext)
    } catch {
        print(error)//dataPaddingRequired
    }
Rajamohan S
  • 7,229
  • 5
  • 36
  • 54
phearun
  • 13
  • 6
  • 1
    Possible duplicate of [AES Encrypt and Decrypt](https://stackoverflow.com/questions/27072021/aes-encrypt-and-decrypt) – Ujesh Nov 14 '17 at 06:32
  • Where is the question,what is the code? – zaph Nov 14 '17 at 22:51
  • 1
    Agreed; nothing in this code makes sense. What problem are you trying to solve, and what is "SwiftCrypto?" Are you intended to use CryptoSwift? – Rob Napier Nov 14 '17 at 23:55

1 Answers1

2

Your IV should be 16 bytes, not 32. AES always has a block size of 128 bits, which divided by 8 is of course 16 bytes. CBC mode uses an IV of the same size as the block size as most (but not all) modes of encryption do.

The authenticated GCM mode is a bit of an odd one out: it uses a nonce / IV of 12 bytes by default and is only available for ciphers with a block size of 128 bits.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263