高分求加密(解密)代码
我正在做一个用户注册.用户登陆界面
想在注册界面实现,密码的加密,然后充入数据库,
想在登陆页面读取数据库的信息(解密).
请大侠帮忙,我用的是 C#+SQL , .NET程序
------解决方案--------------------using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace MDcode
{
/// <summary>
/// Rijndael 的摘要说明。
/// </summary>
public class Rijndael
{
private System.Security.Cryptography.SymmetricAlgorithm sa;
private string key;
private string IV;
public Rijndael()
{
sa = new RijndaelManaged();
key = "!w@o#s$h%i^s&j*f(z)x_womenzuihao "; // "2S(a5%aE9&$820blOF*!ao43KplpPTq82%ggj(ai18*afkv3AfghKO28vs9favi(jFf*Fdja98w8JIlfo3v0f%lfjOi3jIf9*2jNv!eq ";//这个自己可以改掉
IV = "1011[{shuyu733}] "; // "v%fjh1aaG&94r8fi*fyh4afg0ir3QYgjOi0fatj3iV5Y(jkg6e!fg0r3f7GVR9gmrn(0ogEnJ6q%gjir30h(T530hbw!gmKeq2g*(n0j ";//这个也可以改掉
}
private byte[] getKey()
{
string gKey = this.key;
sa.GenerateKey();
byte[] bTemp = sa.Key;
int keyLength = bTemp.Length;
if(this.key.Length > keyLength)
{
gKey = gKey.Substring(0, keyLength);
}
else
{
gKey = gKey.PadRight(keyLength, ' ');
}
return ASCIIEncoding.ASCII.GetBytes(gKey);
}
private byte[] getIV()
{
string gIV = this.IV;
sa.GenerateIV();
byte[] bTemp = sa.IV;
int IVLength = bTemp.Length;
if(this.IV.Length > IVLength)
{
gIV = gIV.Substring(0, IVLength);
}
else
{
gIV = gIV.PadRight(IVLength, ' ');
}
return ASCIIEncoding.ASCII.GetBytes(gIV);
}
public string RijndaelEncrypt(string re)
{
byte[] reIn = UTF8Encoding.UTF8.GetBytes(re);
MemoryStream ms = new MemoryStream();
sa.Key = this.getKey();
sa.IV = this.getIV();
System.Security.Cryptography.ICryptoTransform it = sa.CreateEncryptor();
System.Security.Cryptography.CryptoStream cs = new CryptoStream(ms, it, System.Security.Cryptography.CryptoStreamMode.Write);
cs.Write(reIn, 0, reIn.Length);
cs.FlushFinalBlock();
byte[] reOut=ms.ToArray();
ms.Close();
cs.Close();
return Convert.ToBase64String(reOut);
}
public string RijndaelDencrypt(string rd)
{
byte[] rdIn = Convert.FromBase64String(rd);
MemoryStream ms = new MemoryStream(rdIn, 0, rdIn.Length);
sa.Key = this.getKey();
sa.IV = this.getIV();
System.Security.Cryptography.ICryptoTransform it = sa.CreateDecryptor();
System.Security.Cryptography.CryptoStream cs = new CryptoStream(ms, it, System.Security.Cryptography.CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
string str = sr.ReadToEnd();
ms.Close();
cs.Close();
return str;
}
}
}
实例化Rijndael对象
RijndaelEncrypt为加密方法
RijndaelDencrypt为解密方法