0

I have an error trying to encrypt my password. I can't find a reason why encryption(iv = "") works well with swift 3 but now is not working with iv = "" in swift 5. How do I fix this?

userText = userText.replacingOccurrences(of: "\\s+", with: "", options: .regularExpression, range: nil)
let request:NSMutableURLRequest!
let capcha:String! = ""
let encryptedBase64Password = try! passText.encrypt(cipher: AES(key: encrypKeyForLogin, iv: ""))
Fatal error: 'try!' expression unexpectedly raised an error: CryptoSwift.CBC.Error.invalidInitializationVector: file /Users/vietnguyen/Documents/Mac os/Code/Swift5/SlideOutMenu/SlideOutMenu/Login/LoginServer.swift, line 97 
2020-09-17 21:48:13.326595+0700 SlideOutMenu[2104:103521] Fatal error: 'try!' expression unexpectedly raised an error: CryptoSwift.CBC.Error.invalidInitializationVector: file /Users/vietnguyen/Documents/Mac os/Code/Swift 5/SlideOutMenu/SlideOutMenu/Login/LoginServer.swift, line 97

Line 97: let encryptedBase64Password = try! passText.encrypt(cipher: AES(key: encrypKeyForLogin, iv: ""))

I'm confused because in swift 5 iv 16byte is available but before swift 3 iv = "" work well.

Holger Just
  • 52,918
  • 14
  • 115
  • 123
  • What exactly happens when it is "not working"? Do you get any errors? Do you expect some behavior to happen which doesn't occur? Something else? Please edit your question and add more details about what you expect to happen and what exactly happens instead. – Holger Just Sep 17 '20 at 14:34
  • 1
    @HolgerJust. Can you explain and how to fix it? Thank you. – nguyen viet Sep 17 '20 at 15:23
  • 2
    An initvector IV is of length of 16 bytes when working with AES. Some crypto libs do fill a too short iv with (hex) x00. So maybe swift 3 was doing so but not swift 5. As solution you should use some value of length 16 as iv and it will work. Security warning: **a static iv makes the complete encryption UNSECURE !**. – Michael Fehr Sep 17 '20 at 16:39

0 Answers0