AES加密算法在中文win10系统加解密正常,但是在英文版win10系统可以加密,但是解密有问题
/// <summary>
/// AES base64 解密算法;Key为16位
/// </summary>
/// <param name="Data">需要解密的字符串</param>
/// <param name="Key">Key为16位 密钥</param>
/// <returns></returns>
public static string RST_AesDecrypt_Base64(string Data)
{
try
{
if (string.IsNullOrEmpty(Data))
{
return null;
}
if (string.IsNullOrEmpty(Key))
{
return null;
}
string Vector = Key.Substring(0, 16);
byte[] encryptedBytes =Convert.FromBase64String(Data);
byte[] bKey = new byte[32];
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
byte[] bVector = new byte[16];
Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);
byte[] original = null; // 解密后的明文
Rijndael Aes = Rijndael.Create();
//add
Aes.Mode = CipherMode.CBC;//兼任其他语言的des
Aes.BlockSize = 128;
Aes.Padding = PaddingMode.PKCS7;
//add end
try
{
// 开辟一块内存流,存储密文
using (MemoryStream Memory = new MemoryStream(encryptedBytes))
{
//把内存流对象包装成加密流对象
using (CryptoStream Decryptor = new CryptoStream(Memory,
Aes.CreateDecryptor(bKey, bVector),
CryptoStreamMode.Read))
{
// 明文存储区
using (MemoryStream originalMemory = new MemoryStream())
{
byte[] Buffer = new byte[1024];
Int32 readBytes = 0;
while ((readBytes = Decryptor.Read(Buffer, 0, Buffer.Length)) > 0)
{
originalMemory.Write(Buffer, 0, readBytes);
}
original = originalMemory.ToArray();
}
}
}
}
catch
{
original = null;
}
//var aa= Encoding.UTF32.GetString(original);
return Encoding.UTF8.GetString(original);
}
catch { return null; }
}
/// <summary>
/// AES base64 加密算法;Key 为16位
/// </summary>
/// <param name="Data">需要加密的字符串</param>
/// <returns></returns>
public static string RST_AesEncrypt_Base64(string Data)
{
if (string.IsNullOrEmpty(Data))
{
return null;
}
if (string.IsNullOrEmpty(Key))
{
return null;
}
string Vector = Key.Substring(0, 16);
Byte[] plainBytes = Encoding.UTF8.GetBytes(Data);
Byte[] bKey = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
Byte[] bVector = new Byte[16];
Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);
Byte[] Cryptograph = null; // 加密后的密文
Rijndael Aes = Rijndael.Create();
//add
Aes.Mode = CipherMode.CBC;//兼任其他语言的des
Aes.BlockSize = 128;
Aes.Padding = PaddingMode.PKCS7;
//add end
try
{
// 开辟一块内存流
using (MemoryStream Memory = new MemoryStream())
{
// 把内存流对象包装成加密流对象
using (CryptoStream Encryptor = new CryptoStream(Memory,
Aes.CreateEncryptor(bKey, bVector),
CryptoStreamMode.Write))
{
// 明文数据写入加密流
Encryptor.Write(plainBytes, 0, plainBytes.Length);
Encryptor.FlushFinalBlock();
Cryptograph = Memory.ToArray();
}
}
}
catch
{
Cryptograph = null;
}
return Convert.ToBase64String(Cryptograph);
}
建议把 RST_AesEncrypt_Base64
中下面的代码去掉,看是否是因为发生了异常
catch
{
Cryptograph = null;
}
已经发生异常了,文中的照片那个就是异常原因了
@IT浪客: 是否可以提供一下 byte[] encryptedBytes =Convert.FromBase64String(Data);
中 Data 的值
@dudu: 刚想弄个加密的文件给你,为了不暴露一些信息,就把加密的密码改了,结果可以了,可能是之前的密码含有一些特殊字符导致的加密问题吧,仍然谢谢大佬的热心关注
看看!!!!
byte[] encryptedBytes =Convert.FromBase64String(Data);文中的异常是在这句代码(忘记贴出来,抱歉)
– IT浪客 1年前