如何加密一个字符串。

tztz520 2004-07-08 01:38:21
加密一个字符串,这个字符串有小数点,汉字,数字,什么都有,怎样加密呢,又怎样把加密的字符串解密呢,最好有源码。
...全文
182 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
tztz520 2004-07-08
  • 打赏
  • 举报
回复
多谢帮忙!
Dugu_Niu 2004-07-08
  • 打赏
  • 举报
回复
这是一个不可逆加密的例子
public static string Encrypt(string cleanString)
{
Byte[] clearBytes = new UnicodeEncoding().GetBytes(cleanString);
Byte[] hashedBytes = ((HashAlgorithm) CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);

return BitConverter.ToString(hashedBytes);
}

这是一个可逆加密的例子
static byte[] key = new Byte[32]
{
0x8D, 0xE0, 0xB9, 0xF4,
0xF5, 0x97, 0x90, 0xB0,
0x6B, 0x9F, 0xDC, 0x37,
0x75, 0x7C, 0x76, 0x1B,
0x30, 0x87, 0xB9, 0x29,
0x25, 0xD1, 0x0D, 0xC2,
0xFE, 0xB6, 0x8F, 0x63,
0xF0, 0x2D, 0x22, 0x9D
};
static byte[] iv = new Byte[16]
{
0x1D, 0x21, 0x9B, 0x79,
0xF7, 0x5B, 0x59, 0x88,
0xAD, 0xE4, 0x2E, 0x7D,
0x17, 0xEA, 0x23, 0x5A
};
private static string GetDatabaseConnectionString()
{
FileStream fin;
try
{
fin = new FileStream(System.Windows.Forms.Application.StartupPath + DeployFile,
FileMode.Open, FileAccess.Read);
}
catch
{
System.Diagnostics.Trace.Fail("严重错误: 打开安全配置文件错误,请与系统开发商联系!!!");
return string.Empty;
}

SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();
CryptoStream encStream = new CryptoStream(fin, rijn.CreateDecryptor(key, iv), CryptoStreamMode.Read);

int len = encStream.ReadByte();
byte[] buffer = new byte[len];
encStream.Read(buffer, 0, len);
encStream.Close();
fin.Close();

System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
return encoder.GetString(buffer);
}

private static void SetDatabaseConnectionString(string str)
{
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
FileStream fout;
try
{
fout = new FileStream(System.Windows.Forms.Application.StartupPath + DeployFile,
FileMode.Open, FileAccess.Write);
}
catch
{
System.Diagnostics.Trace.Fail("严重错误: 打开安全配置文件错误,请与系统开发商联系!!!");
return ;
}
fout.SetLength(0);
SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();
CryptoStream encStream = new CryptoStream(fout, rijn.CreateEncryptor(key, iv), CryptoStreamMode.Write);

byte[] buffer = encoder.GetBytes(str);
encStream.WriteByte((byte)buffer.Length);
encStream.Write(buffer, 0, buffer.Length);
encStream.Close();
fout.Close();
}
孟子E章 2004-07-08
  • 打赏
  • 举报
回复
http://dotnet.aspx.cc/ShowDetail.aspx?id=7AE7D20A-A5DA-4303-AC2D-32046BE4D086

16,549

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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