如何在c#中为加密方法编写解密方法
我能够将如下所示的加密示例放在一起,但在解密过程中我得到无效数据(异常)。我应该如何解密
加密方法
public static string EncryptWithAes(string plainText, byte[] key, byte[] initiationVector)
{
byte[] cryptoBytes = Encoding.UTF8.GetBytes(plainText);
using (RijndaelManaged aesAlgorithm = new RijndaelManaged())
{
aesAlgorithm.Key = key;
aesAlgorithm.IV = initiationVector;
aesAlgorithm.Mode = CipherMode.ECB;
using (ICryptoTransform encryptoTransform = aesAlgorithm.CreateEncryptor(aesAlgorithm.Key, aesAlgorithm.IV))
{
cryptoBytes = encryptoTransform.TransformFinalBlock(cryptoBytes, 0, cryptoBytes.Length);
}
}
return Convert.ToBase64String(cryptoBytes);
}
解密方法
public static string DecryptAesCryptoString(string cipherText, byte[] key, byte[] initiationVector)
{
byte[] decryptedByte;
using (RijndaelManaged aesAlgorithm = new RijndaelManaged())
{
aesAlgorithm.Key = key;
aesAlgorithm.IV = initiationVector;
aesAlgorithm.Mode = CipherMode.ECB;
using (ICryptoTransform decryptoTransform = aesAlgorithm.CreateDecryptor(aesAlgorithm.Key, aesAlgorithm.IV))
{
byte[] cipherBytes = Convert.FromBase64String(cipherText);
decryptedByte = decryptoTransform.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
}
}
return Encoding.UTF8.GetString(decryptedByte);
}
我认为问题出在这些方法内部的所有编码
示例数据
plainText = stackoverflow
base64encoded Key = B8Y/6doxwqU870C6jzYWhsr3hKSLokAOkkLCDiy+TS4=
(应该很容易转换为字节不是吗)
base64encoded IV = NZIpD60eBmdsOFFhA2bfvw==
encryptedValue = 77+977+977+977+977+9Ce+/ve+/vQ3vv70F77+9UzHvv73vv70=< /code>
我提供相同的加密值、IV 和密钥解密到 Stackoverflow
I was able to put together a encryption sample like below but during decryption i get invalid data(Exception). How am i supposed to decrypt
Encryption Method
public static string EncryptWithAes(string plainText, byte[] key, byte[] initiationVector)
{
byte[] cryptoBytes = Encoding.UTF8.GetBytes(plainText);
using (RijndaelManaged aesAlgorithm = new RijndaelManaged())
{
aesAlgorithm.Key = key;
aesAlgorithm.IV = initiationVector;
aesAlgorithm.Mode = CipherMode.ECB;
using (ICryptoTransform encryptoTransform = aesAlgorithm.CreateEncryptor(aesAlgorithm.Key, aesAlgorithm.IV))
{
cryptoBytes = encryptoTransform.TransformFinalBlock(cryptoBytes, 0, cryptoBytes.Length);
}
}
return Convert.ToBase64String(cryptoBytes);
}
Decryption Method
public static string DecryptAesCryptoString(string cipherText, byte[] key, byte[] initiationVector)
{
byte[] decryptedByte;
using (RijndaelManaged aesAlgorithm = new RijndaelManaged())
{
aesAlgorithm.Key = key;
aesAlgorithm.IV = initiationVector;
aesAlgorithm.Mode = CipherMode.ECB;
using (ICryptoTransform decryptoTransform = aesAlgorithm.CreateDecryptor(aesAlgorithm.Key, aesAlgorithm.IV))
{
byte[] cipherBytes = Convert.FromBase64String(cipherText);
decryptedByte = decryptoTransform.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
}
}
return Encoding.UTF8.GetString(decryptedByte);
}
i think the problem is with all the encoding that are going inside these methods
Sample Data
plainText = stackoverflow
base64encoded Key = B8Y/6doxwqU870C6jzYWhsr3hKSLokAOkkLCDiy+TS4=
(should be easy to convert to bytes ain't it)
base64encoded IV = NZIpD60eBmdsOFFhA2bfvw==
encryptedValue = 77+977+977+977+977+9Ce+/ve+/vQ3vv70F77+9UzHvv73vv70=
I provide same encrypted value , IV and Key to decrypt to Stackoverflow
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你的问题是你的静脉注射的长度,也许是关键。我记得 IV 应该是 16 个字节长,密钥有不同的选项,你应该查一下。
//测试:
I think your problem is the length of your IV and maybe the key. The IV should be 16 bytes long as i recall, the key has different options, you should look that up.
//TEST:
可悲的是,这肯定是由于编码问题造成的。现在解决了,如下
加密
解密
and sadly this was certainly due to the Encoding problem. Now solved it like below
Encryption
Decryption
为什么你不尝试删除 Encoding 呢?
这是一个简单的实现:
Why you don't give a try by removing the Encoding ?
Here's a simple implementation :