Reputation: 943
Probably I have a stupid question, but I am not able to make it working. I am doing the AES encryption\decryption in F# according to the MSDN example which is in C#:
http://msdn.microsoft.com/en-us/library/system.security.cryptography.aes.aspx
My encryption method is as follows:
let EncryptStringToBytesAes (plainText : string) (key : byte[]) (iv : byte[]) =
use aesAlg = Aes.Create()
aesAlg.Key <- key
aesAlg.IV <- iv
// Create a decrytor to perform the stream transform.
let encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)
// Create the streams used for encryption.
use msEncrypt = new MemoryStream()
use csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
use swEncrypt = new StreamWriter(csEncrypt)
swEncrypt.Write(plainText)
msEncrypt.ToArray()
The problem is that this method always returns me an empty array of bytes. I do not have any exception. Key and IV are proper arrays of bytes. Seems like the StreamWriter is not working...
Thank you for help.
Upvotes: 5
Views: 1406
Reputation: 943
To make it working we need to explicitly close the StreamWriter and CryptoStream
Upvotes: -1
Reputation: 47904
Building on @usr's answer...
The easiest way to make sure the stream is closed is to place the use
statements within a block that goes out of scope before ToArray
is called.
let EncryptStringToBytesAes (plainText : string) (key : byte[]) (iv : byte[]) =
use aesAlg = Aes.Create()
aesAlg.Key <- key
aesAlg.IV <- iv
// Create a decrytor to perform the stream transform.
let encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)
// Create the streams used for encryption.
use msEncrypt = new MemoryStream()
(
use csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
use swEncrypt = new StreamWriter(csEncrypt)
swEncrypt.Write(plainText)
)
msEncrypt.ToArray()
Upvotes: 5
Reputation: 171218
Before you call msEncrypt.ToArray
you must flush all intermediate streams, or close them, because they are buffering data.
Upvotes: 2