0

I am trying to Encrypt some data and I keep getting this error, Meanwhile my iv and key are at the same size but contain 29 values...This is my code. When I try a key with 16 values it works but I got to work with this other key too and I don't know how to solve it. Can somebody help me?

class AESHelper {

var key: String 
var iv : String
let BLOCK_SIZE = AES.blockSize

init(key: String, iv: String){
    self.key = key
    self.iv = iv
}

func encrypt(stringToEncrypt: String) -> String {
    let messageData = stringToEncrypt.dataUsingEncoding(NSUTF8StringEncoding)
    let byteArray = messageData!.arrayOfBytes()
    let encryptedBytes = try! AES(key: self.key, iv: self.iv, blockMode: .CBC).encrypt(byteArray, padding: PKCS7())
    let toBase64 = NSData(bytes: encryptedBytes).base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
    return toBase64

}
jww
  • 97,681
  • 90
  • 411
  • 885
Xhulio Hasa
  • 1
  • 1
  • 2
  • AES only supports key sizes of 128, 192 and 256 bit. There are no other supported key size. Don't confuse keys with passwords. Keys must appear like random noise. What you probably want is password-based encryption with PBKDF2 or Argon2. – Artjom B. Apr 11 '16 at 14:07
  • Possible duplicate of [Swift: How to call CCKeyDerivationPBKDF from Swift](http://stackoverflow.com/questions/25691613/swift-how-to-call-cckeyderivationpbkdf-from-swift) – Artjom B. Apr 11 '16 at 14:11
  • Thank you @ArtjomB. ...the method you suggested me looks very helpful – Xhulio Hasa Apr 12 '16 at 08:49
  • If it did help, then you can confirm that this question is a duplicate of the question I linked to. You should see a button above your question. – Artjom B. Apr 12 '16 at 17:52

0 Answers0