.Net中关于RijndaelManaged加密算法问题?

linxi1151 2009-11-25 10:56:37
为什么下面的方法得到的加密串为空?


private byte[] key = { 24, 55, 102, 24, 56, 26, 67, 29, 84, 19, 37, 118, 104, 85, 178, 27, 93, 86, 24, 55, 102, 24, 98, 26, 67, 29, 9, 2, 49, 69, 73, 92 };
private byte[] iv = { 9, 5, 89, 7, 76, 3, 12, 45, 89, 63, 78, 95, 42, 15, 73, 92 };

public string encriptString(string input)
{
string rst = "";
if (string.IsNullOrEmpty(input))
{
return "";
}
byte[] data = Encoding.UTF7.GetBytes(input);
MemoryStream msOut = new MemoryStream();
//定义对称算法对象实例和接口

RijndaelManaged rij = new RijndaelManaged();
ICryptoTransform transform = rij.CreateEncryptor(key, iv);
CryptoStream cstream = new CryptoStream(msOut, transform, CryptoStreamMode.Write);
cstream.Write(data, 0,data.Length);
StreamReader sr = new StreamReader(msOut);
rst = sr.ReadToEnd();
cstream.FlushFinalBlock();
cstream.Close();
msOut.Close();
sr.Close();
return rst;
}
...全文
652 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
John_Yang 2009-11-25
  • 打赏
  • 举报
回复
你的代码问题在于CryptoStream对象在Write后没有Close,所以并没有执行加密算法,
应该在cstream.Write(data, 0,data.Length);这句代码后加上cstream.close();


给你看MSDN上面的解释
You should always explicitly close your CryptoStream object after you are done using it by calling the Close method. Doing so flushes the stream and causes all remaining blocks of data to be processed by the CryptoStream object. However, if an exception occurs before you call the Close method, the CryptoStream object might not be closed. To ensure that the Close method always gets called, place your call to the Close method within the finally block of a try/catch statement.
John_Yang 2009-11-25
  • 打赏
  • 举报
回复
static byte[] encryptStringToBytes_AES(string plainText, byte[] Key, byte[] IV)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("Key");

// Declare the stream used to encrypt to an in memory
// array of bytes.
MemoryStream msEncrypt = null;

// Declare the RijndaelManaged object
// used to encrypt the data.
RijndaelManaged aesAlg = null;

try
{
// Create a RijndaelManaged object
// with the specified key and IV.
aesAlg = new RijndaelManaged();
aesAlg.Key = Key;
aesAlg.IV = IV;

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

// Create the streams used for encryption.
msEncrypt = new MemoryStream();
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{

//Write all data to the stream.
swEncrypt.Write(plainText);
}
}

}
finally
{

// Clear the RijndaelManaged object.
if (aesAlg != null)
aesAlg.Clear();
}

// Return the encrypted bytes from the memory stream.
return msEncrypt.ToArray();

}
accpzwt 2009-11-25
  • 打赏
  • 举报
回复
回答错了,嘿嘿,一般我的MD5就是用我的这个方法写的。
accpzwt 2009-11-25
  • 打赏
  • 举报
回复
public string Md5(string password)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
//使用加密服务提供程序 (CSP) 提供的实现,计算输入数据的 MD5 哈希值。无法继承此类。
return BitConverter.ToString(hashmd5.ComputeHash(Encoding.Default.GetBytes(password))).Replace("-", "");
//System命名空间BitConverter将基础数据类型与字节数组相互转换。
//GetBytes将指定的数据转换为字节数组。
//ComputeHash计算输入数据的哈希值的方法。
}

62,265

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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