Skip to content
This repository has been archived by the owner on Sep 3, 2020. It is now read-only.

Could the IV be automatically extracted from the encrypted data? #84

Closed
backslash-f opened this issue Apr 5, 2019 · 2 comments
Closed
Labels
question Further information is requested

Comments

@backslash-f
Copy link

backslash-f commented Apr 5, 2019

More like a question.

So this works fine:

let data = "The black knight always triumphs!".convertToData()
let key = "01234567890123456789012345678901".convertToData()
let iv = try CryptoRandom().generateData(count: 16)
let encryptedData = try AES256CBC.encrypt(data, key: key, iv: iv)

let decryptedData = try AES256CBC.decrypt(encryptedData, key: key, iv: iv)
print(decryptedData.convert(to: String.self)) // The black knight always triumphs!

Question is can I not pass in the iv as argument so it could be automatically extracted from the encrypted data? As in:

let decryptedData = try AES256CBC.decrypt(encryptedData, key: key) <<--
print(decryptedData.convert(to: String.self))

Throws: ⚠️ [CryptoError.cipherIVSize: Invalid cipher IV length 0 != 16.]

I'm trying to avoid having to pass the IV around as in here (relies on Apple's CommonCryto / CCCryptor).

Thanks.

@backslash-f backslash-f changed the title Automatically extract IV from encrypted data? Could the IV be automatically extracted from the encrypted data? Apr 5, 2019
@vzsg
Copy link
Member

vzsg commented Apr 5, 2019

No, but if you want, you could prepend the IV to the ciphertext, send/store them together, then split off the first 16 bytes when decrypting. After checking your library, you seem to be doing the same thing implicitly.

Well, with vapor/crypto, you'll have to do it explicitly :)

@backslash-f
Copy link
Author

backslash-f commented Apr 5, 2019

you seem to be doing the same thing implicitly <<-- exactly 🙃
(also this)

All right, then. Thanks for confirming. 👍🏻

@tanner0101 tanner0101 added the question Further information is requested label Apr 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants