0

Im using cryptoswift to decrypt a string

let decrypted2 =  try AES(key: "35%6HyBW", iv: "erewf^%", blockMode: .CBC, padding: .pkcs7).decrypt(text2)

But I keep getting Type of expression is ambiguous without more context Complie time error

techno
  • 6,100
  • 16
  • 86
  • 192

1 Answers1

1

You've got two typos here.

#1, the prototype you're using doesn't exist. Blockmode takes a CBC(iv:) argument.

#2, you need to convert strings into a [UInt8] array.

So use:

let aes = try AES(key: [UInt8]("35%6HyBW".utf8), blockMode: CBC(iv: [UInt8]("erewf^%".utf8)), padding: .pkcs7)
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Thanks for your reply.Im just learning swift.decrypted2 is an AES object.How can I get the decrypted string from it. – techno Jun 06 '18 at 13:21
  • I just modified my answer a little bit. You're creating an AES object (for encrypting and decrypting). So what's missing is that you need to have your encrypted data/string and pass ***that*** to the AES `.decrypt` function. Take a look at the CryptoSwift project's `.playground` and you'll see a nice, self explanatory example under the "ChaChat20" header. – Michael Dautermann Jun 06 '18 at 13:47
  • Thanks for the update,I have managed to encrypt a string using the following code ' let aes1 = try AES(key: "passwordpassword", iv: "drowssapdrowssap") let sdata = try aes1.encrypt(Array(akey.utf8)) ' But the issue is that I cannot use the Mac System Serial Number as the key, it seems that AES requires a specific key length ,I cannot change encryption key as the old implementations uses this method.Im trying to change this because I get `null` value for decrypted string in some cases https://stackoverflow.com/questions/50611187/aes-decryption-in-swift4-producing-null-value – techno Jun 06 '18 at 14:31
  • How can I replicate the same thing with cryptoswift.Please advice – techno Jun 06 '18 at 14:31
  • Ask a separate question (since it's a separate question) and I'll try to respond to that one, too. – Michael Dautermann Jun 06 '18 at 18:29
  • I have opened a new question.Please see https://stackoverflow.com/questions/50727447/migrating-common-crypto-aesencryption-to-cryptoswift-as-decrypted-string-turns-n – techno Jun 06 '18 at 18:44