aes解密时编码模式能改吗?

Laplace_Primitives 2018-05-16 02:47:49
貌似解密时默认时Ascii编码,如果解密出来不在Ascii范围内的都都标记为3f了
   string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key)
{
byte[] IV = new byte[16];

if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
aesAlg.Padding = PaddingMode.Zeros;

// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

byte[] plainText = decryptor.TransformFinalBlock(cipherText, 0, cipherText.Length);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{

using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
plaintext = srDecrypt.ReadToEnd();
}
}
}

}
return plaintext;

}

代码就是这样
...全文
239 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Laplace_Primitives 2018-05-16
  • 打赏
  • 举报
回复
引用 7 楼 xuzuning 的回复:
另外你的方法中有 plaintext = srDecrypt.ReadToEnd(); 此时是从流读出字符串,而字符串是有编码的(流默认的编码是 utf-8) 由于不能识别单个的扩展 ASCII 字符 f2 ab 88,于是就成了 ?(0x3f)了
抱歉抱歉,大佬 我自己看错了一个地方,我发现问题了,浪费您时间了抱歉
Laplace_Primitives 2018-05-16
  • 打赏
  • 举报
回复
引用 5 楼 xuzuning 的回复:
不要乱猜原因!你的 string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key) 传入的参数都是 byte[] 类型的,而你描述时都有的是字符串。那么你怎么就知道不是你在将字符串转成字节数组时出错了呢? 另外你没有给出加密方法,也无法验证是否是你的解密函数写错了
大佬你说的很对,主要是加密那一块在个单片机上,然后我用别的解密软件的解密了一下,没有我说的那种3f的问题,所以把我自己绕懵逼了 至于这个字符串和byte[] 转换的问题,方法虽然是我自己写的,但是我调试时候发现byte[] 的值和转换出来的字符串是对的
xuzuning 2018-05-16
  • 打赏
  • 举报
回复
另外你的方法中有 plaintext = srDecrypt.ReadToEnd(); 此时是从流读出字符串,而字符串是有编码的(流默认的编码是 utf-8) 由于不能识别单个的扩展 ASCII 字符 f2 ab 88,于是就成了 ?(0x3f)了
xuzuning 2018-05-16
  • 打赏
  • 举报
回复
不要乱猜原因!你的 string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key) 传入的参数都是 byte[] 类型的,而你描述时都有的是字符串。那么你怎么就知道不是你在将字符串转成字节数组时出错了呢? 另外你没有给出加密方法,也无法验证是否是你的解密函数写错了
Laplace_Primitives 2018-05-16
  • 打赏
  • 举报
回复
引用 1 楼 xuzuning 的回复:
加密时是什么,解密后还是什么
我加密前是这个 010300f200010005ab8800000000 解密后 0103003F000100053F3F000000000000 都是16进制 f2 ab 88 这种都变成3f了,具体咋回事我不太清楚,让我感觉是编码方式的问题,
  • 打赏
  • 举报
回复
另外你这种方式StreamReader注意其构造函数,有Encoding参数的
  • 打赏
  • 举报
回复
你用的什么编码方式Encoding跟加解密没一毛线关系…… 加解密接受的参数是byte[],返回的也是byte[] 你用我这个封装过的类吧 https://blog.csdn.net/starfd/article/details/51908758
xuzuning 2018-05-16
  • 打赏
  • 举报
回复
加密时是什么,解密后还是什么

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧