C#AES解密码
我有使用AES算法进行字符串加密/解密的代码。加密工作正常,但我无法得到正确的纯文本时,我解密回来。解密代码中一定有错误C#AES解密码,c#,encryption,cryptography,aes,C#,Encryption,Cryptography,Aes,我有使用AES算法进行字符串加密/解密的代码。加密工作正常,但我无法得到正确的纯文本时,我解密回来。解密代码中一定有错误 public static class EncryptionHelper { private static int BlockSize = 16; private static byte[] Key { get { byte[] hash = SHA1.Create().ComputeHash(
public static class EncryptionHelper
{
private static int BlockSize = 16;
private static byte[] Key
{
get
{
byte[] hash = SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes("BlahBlahBlah"));
byte[] key = new byte[BlockSize];
Array.Copy(hash, 0, key, 0, BlockSize);
return key;
}
}
private static byte[] IV
{
get
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
for (int i = 0; i < BlockSize; i++)
{
char ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
return Encoding.UTF8.GetBytes(builder.ToString());
}
}
public static string DecodeAndDecrypt(string cipherText)
{
string DecodeAndDecrypt = AesDecrypt(Convert.FromBase64String(HttpUtility.UrlDecode(cipherText)));
return (DecodeAndDecrypt);
}
public static string EncryptAndEncode(string plaintext)
{
return HttpUtility.UrlEncode(Convert.ToBase64String(AesEncrypt(plaintext)));
}
public static string AesDecrypt(Byte[] inputBytes)
{
Byte[] outputBytes = inputBytes;
string plaintext = string.Empty;
using (MemoryStream memoryStream = new MemoryStream(outputBytes))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, GetCryptoAlgorithm().CreateDecryptor(Key, IV), CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(cryptoStream))
{
plaintext = srDecrypt.ReadToEnd();
}
}
}
return plaintext;
}
public static byte[] AesEncrypt(string inputText)
{
byte[] inputBytes = UTF8Encoding.UTF8.GetBytes(inputText);
for (int i = 0; i < BlockSize; i++)
{
inputBytes[i] ^= IV[i];
}
byte[] result = null;
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, GetCryptoAlgorithm().CreateEncryptor(Key, IV), CryptoStreamMode.Write))
{
cryptoStream.Write(inputBytes, 0, inputBytes.Length);
cryptoStream.FlushFinalBlock();
result = memoryStream.ToArray();
}
}
return result;
}
private static RijndaelManaged GetCryptoAlgorithm()
{
RijndaelManaged algorithm = new RijndaelManaged();
algorithm.Padding = PaddingMode.PKCS7;
algorithm.Mode = CipherMode.CBC;
algorithm.KeySize = 128;
algorithm.BlockSize = 128;
return algorithm;
}
}
我已复制了下面的代码。请帮忙。谢谢 下面是加密和解密代码
public static class EncryptionHelper
{
private static int BlockSize = 16;
private static byte[] Key
{
get
{
byte[] hash = SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes("BlahBlahBlah"));
byte[] key = new byte[BlockSize];
Array.Copy(hash, 0, key, 0, BlockSize);
return key;
}
}
private static byte[] IV
{
get
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
for (int i = 0; i < BlockSize; i++)
{
char ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
return Encoding.UTF8.GetBytes(builder.ToString());
}
}
public static string DecodeAndDecrypt(string cipherText)
{
string DecodeAndDecrypt = AesDecrypt(Convert.FromBase64String(HttpUtility.UrlDecode(cipherText)));
return (DecodeAndDecrypt);
}
public static string EncryptAndEncode(string plaintext)
{
return HttpUtility.UrlEncode(Convert.ToBase64String(AesEncrypt(plaintext)));
}
public static string AesDecrypt(Byte[] inputBytes)
{
Byte[] outputBytes = inputBytes;
string plaintext = string.Empty;
using (MemoryStream memoryStream = new MemoryStream(outputBytes))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, GetCryptoAlgorithm().CreateDecryptor(Key, IV), CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(cryptoStream))
{
plaintext = srDecrypt.ReadToEnd();
}
}
}
return plaintext;
}
public static byte[] AesEncrypt(string inputText)
{
byte[] inputBytes = UTF8Encoding.UTF8.GetBytes(inputText);
for (int i = 0; i < BlockSize; i++)
{
inputBytes[i] ^= IV[i];
}
byte[] result = null;
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, GetCryptoAlgorithm().CreateEncryptor(Key, IV), CryptoStreamMode.Write))
{
cryptoStream.Write(inputBytes, 0, inputBytes.Length);
cryptoStream.FlushFinalBlock();
result = memoryStream.ToArray();
}
}
return result;
}
private static RijndaelManaged GetCryptoAlgorithm()
{
RijndaelManaged algorithm = new RijndaelManaged();
algorithm.Padding = PaddingMode.PKCS7;
algorithm.Mode = CipherMode.CBC;
algorithm.KeySize = 128;
algorithm.BlockSize = 128;
return algorithm;
}
}
公共静态类EncryptionHelper
{
私有静态int BlockSize=16;
专用静态字节[]密钥
{
得到
{
byte[]hash=SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(“BlahBlahBlah”);
字节[]键=新字节[块大小];
复制(散列,0,键,0,块大小);
返回键;
}
}
专用静态字节[]IV
{
得到
{
StringBuilder=新的StringBuilder();
随机=新随机();
对于(int i=0;i
我做了一些更改,并将在下面发布一些源代码。要使其工作,只需在Form1上放置3个文本框,并将此代码用于Form1.cs。我在上面提到过,我认为一个主要问题是如何在不指定编码的情况下以字符串形式从StreamReader中读取数据。我的更改使用BinaryReader作为字节数组进行读取,然后在读取后转换为UTF-8字符串。我还删除了XOR循环。我认为您试图自己实现CBC(CBC是一种XOR的反馈循环),但它不是必需的——当您指定CBC模式时,.NET为您实现CBC模式。为了使我的版本尽可能接近你的版本,我没有做更多的更改。但请记住上面的一些评论。例如,当.NET提供DeriveBytes函数时,不要使用简单的哈希。像对待纸巾一样对待静脉注射——使用一次,也只能使用一次,永远不要。等等无论如何,如果不是“最佳实践”,功能代码如下:
编辑:对不起,我忘了提到我还将纯文本类型更改为常规字符串,删除了与HTTP相关的代码。这只是为了让我的工作更简单。不过,它对HTTP方法的作用应该与对常规字符串的作用一样好
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
namespace StackOverflow
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
textBox1.Text = EncryptionHelper.EncryptAndEncode(textBox3.Text);
textBox2.Text = EncryptionHelper.DecodeAndDecrypt(textBox1.Text);
}
}
public static class EncryptionHelper
{
private static int BlockSize = 16;
private static byte[] Key
{
get
{
byte[] hash = SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes("BlahBlahBlah"));
byte[] key = new byte[BlockSize];
Array.Copy(hash, 0, key, 0, BlockSize);
return key;
}
}
private static byte[] IV
{
get
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
for (int i = 0; i < BlockSize; i++)
{
char ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
return Encoding.UTF8.GetBytes(builder.ToString());
}
}
public static string DecodeAndDecrypt(string cipherText)
{
byte[] cipherBytes = Convert.FromBase64String(cipherText);
string decodeAndDecrypt = AesDecrypt(cipherBytes);
return decodeAndDecrypt;
}
public static string EncryptAndEncode(string plaintext)
{
return Convert.ToBase64String(AesEncrypt(plaintext));
}
public static string AesDecrypt(Byte[] inputBytes)
{
Byte[] outputBytes = inputBytes;
string plaintext = string.Empty;
using (MemoryStream memoryStream = new MemoryStream(outputBytes))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, GetCryptoAlgorithm().CreateDecryptor(Key, IV), CryptoStreamMode.Read))
{
using (BinaryReader srDecrypt = new BinaryReader(cryptoStream))
{
outputBytes = srDecrypt.ReadBytes(inputBytes.Length);
plaintext = Encoding.UTF8.GetString(outputBytes);
}
}
}
return plaintext;
}
public static byte[] AesEncrypt(string inputText)
{
byte[] inputBytes = UTF8Encoding.UTF8.GetBytes(inputText);
byte[] result = null;
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, GetCryptoAlgorithm().CreateEncryptor(Key, IV), CryptoStreamMode.Write))
{
cryptoStream.Write(inputBytes, 0, inputBytes.Length);
cryptoStream.FlushFinalBlock();
result = memoryStream.ToArray();
}
}
return result;
}
private static RijndaelManaged GetCryptoAlgorithm()
{
RijndaelManaged algorithm = new RijndaelManaged();
algorithm.Padding = PaddingMode.PKCS7;
algorithm.Mode = CipherMode.CBC;
algorithm.KeySize = 128;
algorithm.BlockSize = 128;
return algorithm;
}
}
}
使用系统;
使用System.IO;
使用System.Security.Cryptography;
使用系统文本;
使用System.Windows.Forms;
命名空间堆栈溢出
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有void textBox3\u TextChanged(对象发送方,事件参数e)
{
textBox1.Text=EncryptionHelper.EncryptAndEncode(textBox3.Text);
textBox2.Text=EncryptionHelper.DecodeAndDecrypt(textBox1.Text);
}
}
公共静态类EncryptionHelper
{
私有静态int BlockSize=16;
专用静态字节[]密钥
{
得到
{
byte[]hash=SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(“BlahBlahBlah”);
字节[]键=新字节[块大小];
复制(散列,0,键,0,块大小);
返回键;
}
}
专用静态字节[]IV
{
得到
{
StringBuilder=新的StringBuilder();
随机=新随机();
对于(int i=0;i