AES加密器不工作 [英] AES encryptor not working
问题描述
我试图让这个AES的示例代码工作。但是我没有得到回到我的密文变任何东西。我没有收到错误,只是没有返回。 ?我在做什么错在这里。
字节公众[]键{搞定;组; }
公共字节[] {IV获得;组; }
公共字节[] {密文获得;组; }
公共字符串明文{搞定;组; }
公共字节[]加密(字符串InputPlaintext)
{
InputPlaintext =攻击黎明;
使用(AesCryptoServiceProvider AESEncryptor =新AesCryptoServiceProvider())
{
////使用AesCryptoServiceProvider产生IV和密钥
键= AESEncryptor.Key;
IV = AESEncryptor.IV;
ICryptoTransform的加密= AESEncryptor.CreateEncryptor(键,IV);使用(MemoryStream的msEncrypt =新的MemoryStream())
{
使用
(CryptoStream的csEncrypt =新的CryptoStream(msEncrypt,加密,CryptoStreamMode.Write))
$ {使用b
$ b(StreamWriter的swEncrypt =新的StreamWriter(csEncrypt))
{
swEncrypt.Write(InputPlaintext);
=密文msEncrypt.ToArray();
返回密文;
}
}
}
};
}
三个选项,所有这一切都做同样的事情,
无论是电话 csEncrypt.Close()
或使用 csEncrypt.FlushFinalBlock()
来刷新加密的数据到内存流 - 前 cipertext = msEncrypt.ToArray称之为()
或者,移动密码= msEncrypt.ToArray();返回cipertext;
using块,你正在写的加密数据流之外
请注意 csEncrypt.Flush( )
这可能是第一个猜测什么都不做。
公共覆盖无效的flush()
{
的回报;
}
I am attempting to get this AES sample code working. However I am not getting anything returned to my cipherText variable. I am not getting errors, just nothing returned. What am I doing wrong here?
public byte[] key { get; set; }
public byte[] IV { get; set; }
public byte[] ciphertext { get; set; }
public string plainText { get; set; }
public byte[] Encrypt(string InputPlaintext)
{
InputPlaintext = "attack at dawn";
using (AesCryptoServiceProvider AESEncryptor = new AesCryptoServiceProvider())
{
////using the AesCryptoServiceProvider to generate the IV and Key
key = AESEncryptor.Key;
IV = AESEncryptor.IV;
ICryptoTransform encryptor = AESEncryptor.CreateEncryptor(key, IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(InputPlaintext);
ciphertext = msEncrypt.ToArray();
return ciphertext;
}
}
}
};
}
Three options, all of which do the same thing,
Either call csEncrypt.Close()
or use csEncrypt.FlushFinalBlock()
to flush the encrypted data to the memory stream - call it before cipertext = msEncrypt.ToArray()
.
Or, move cipher = msEncrypt.ToArray(); return cipertext;
outside the using block where you're writing to the crypto stream.
Note csEncrypt.Flush()
which might be the first guess does nothing..
public override void Flush()
{
return;
}
这篇关于AES加密器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!