在f#中实现AES加密(根据MSDN c#示例)

时间:2021-06-08 18:25:00

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#:

也许我有一个愚蠢的问题,但我不能让它发挥作用。根据c#中的MSDN示例,我在f#中进行AES加密和解密:

http://msdn.microsoft.com/en-us/library/system.security.cryptography.aes.aspx

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...

问题是这个方法总是返回一个空的字节数组。我没有任何例外。键和IV是适当的字节数组。看起来流作者没在工作……

Thank you for help.

谢谢你的帮助。

3 个解决方案

#1


3  

Building on @usr's answer...

基于@usr的回答…

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.

确保流关闭的最简单方法是将use语句放在调用ToArray之前超出范围的块中。

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()

#2


2  

Before you call msEncrypt.ToArray you must flush all intermediate streams, or close them, because they are buffering data.

你叫msEncrypt之前。必须刷新或关闭所有中间流,因为它们是缓冲数据。

#3


-2  

To make it working we need to explicitly close the StreamWriter and CryptoStream

要使其工作,我们需要显式地关闭StreamWriter和加密流

#1


3  

Building on @usr's answer...

基于@usr的回答…

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.

确保流关闭的最简单方法是将use语句放在调用ToArray之前超出范围的块中。

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()

#2


2  

Before you call msEncrypt.ToArray you must flush all intermediate streams, or close them, because they are buffering data.

你叫msEncrypt之前。必须刷新或关闭所有中间流,因为它们是缓冲数据。

#3


-2  

To make it working we need to explicitly close the StreamWriter and CryptoStream

要使其工作,我们需要显式地关闭StreamWriter和加密流