请教加密时关于Hash的计算问题 SHA1是取摘要算法,不是加密算法。不能还原,对称加密算法,比如RC4之类 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 #region rc4算法 public void RC4(byte[] Source,int SourceLength,byte[] seed) { Rc4Key key = new Rc4Key(); prepare_key(seed,seed.Length,key); rc4(Source,SourceLength,key); } public int RC4(string SourceFileName,string ObjFileName,string seed) { return RC4(SourceFileName,ObjFileName,MD5string(seed)); } public int RC4(string SourceFileName,string ObjFileName,byte[] seed) { try { Rc4Key key = new Rc4Key(); prepare_key(seed,seed.Length,key); bool fin = System.IO.File.Exists( SourceFileName); bool fout = System.IO.File.Exists( ObjFileName); if(!fin) return 1; if(fout) return 2; System.IO.FileStream fw=null; System.IO.FileStream fr=null; fr = System.IO.File.OpenRead( SourceFileName ); fw = System.IO.File.OpenRead( ObjFileName ); byte[] buffer = new byte[BUFFER_SIZE]; int size=(int)fr.Length; int ReadLength=0; while(size>0) { ReadLength=size>BUFFER_SIZE?BUFFER_SIZE:size; ReadLength=fr.Read(buffer,0,ReadLength); size-=ReadLength; rc4(buffer,ReadLength,key); fw.Write(buffer,0,ReadLength); } fw.Close(); fr.Close(); return 0; } catch(IOException e) { throw e; return -1; } } public void prepare_key(byte[] key_data_ptr, int key_data_len, Rc4Key key) { int index1; int index2; byte[] state; short counter; state = key.state; for (counter = 0; counter < 256; counter++) state[counter] = (byte)counter; key.x = 0; key.y = 0; index1 = 0; index2 = 0; byte temp; for(counter = 0; counter < 256; counter++) { index2 = (key_data_ptr[index1] + state[counter] + index2)&0xff; temp = state[counter]; state[counter] = state[index2]; state[index2] = temp; index1 = (index1 + 1) % key_data_len; } } public void rc4(byte[] buffer_ptr, int buffer_len, Rc4Key key) { int x; int y; byte[] state; int xorIndex; short counter; byte temp; x = key.x; y = key.y; state = key.state; for(counter = 0; counter < buffer_len; counter++) { x =(x + 1)&0xff; y = (state[x] + y)&0xff; temp = state[x]; state[x] = state[y]; state[y] = (byte)temp; xorIndex = (state[x] + state[y])&0xff; buffer_ptr[counter] ^= state[xorIndex]; } key.x = (byte)x; key.y = (byte)y; } public bool bytCompare(byte[] Source,byte[] Object) { int Length=Source.Length; for(int i=0;i<Length;i++) if(Source[i]!=Object[i]) return false; return true; } #endregion } /** * * rc主键 * */ public class Rc4Key { #region 数据变量 public byte[] state = new byte[256]; public byte x; public byte y; #endregion }} SHAx???这是不可逆算法在验证用户的时候,一般都采用,把用户输入的密码再加一次密,与数据库或别的什么地方的加密密码进行比较如果你要实现“找回密码”功能,就只能给用户一个随机产生的密码了,然后要用户自己去改 对称加密算法有很多DES就是最出名的一个还有Blowfish、ElGamal算法对了,还有公钥密码系统DSA、RSA好像还有一个什么ECC算法体系 //加密,使用密码产生加密算法的公钥,并使用TripleDES对密码进行加密。static String Encrypt(String pass) {byte[] bt = Encoding.Unicode.GetBytes(pass);PasswordDeriveBytes pdb = new PasswordDeriveBytes(pass, null);byte[] key = pdb.GetBytes(24);byte[] iv = pdb.GetBytes(8);MemoryStream ms = new MemoryStream();TripleDESCryptoServiceProvider tdesc = new TripleDESCryptoServiceProvider();CryptoStream cs = new CryptoStream(ms,tdesc.CreateEncryptor(key, iv),CryptoStreamMode.Write);cs.Write(bt, 0, bt.Length);cs.FlushFinalBlock();return Encoding.Unicode.GetString(ms.ToArray());}//解密,使用密码产生加密算法的公钥,并使用TripleDES对加密数据进行解密。static String Decrypt(String str, String pass) {byte[] bt = Encoding.Unicode.GetBytes(str);PasswordDeriveBytes pdb = new PasswordDeriveBytes(pass, null);byte[] key = pdb.GetBytes(24);byte[] iv = pdb.GetBytes(8);MemoryStream ms = new MemoryStream();TripleDESCryptoServiceProvider tdesc = new TripleDESCryptoServiceProvider();CryptoStream cs = new CryptoStream(ms,tdesc.CreateDecryptor(key, iv),CryptoStreamMode.Write);cs.Write(bt, 0, bt.Length);cs.FlushFinalBlock();return Encoding.Unicode.GetString(ms.ToArray());}使用:String str = Encrypt("bbb");Console.WriteLine(Decrypt(str, "bbb")); 细解对象克隆代码!!! 跪求一个Winform控件, 能够显示 表情和文本信息的控件..类似于QQ的文本框 登录与注销任务栏窗体数问题 C#中动态创建数据库 请推荐几个.Net控件下载站。 直线的生成算法 不想去翻帖子,送分题!(c#) 求教关于PictureBox的问题 关于读写文件的格式化 大哥大姐们,帮我实现两个TreeView控件上节点之间的画线,谢谢了! 请问,在c#中如何得到一个渐变得颜色,如从白色到黑色;? 我只看过《c程序设计》和《c++编程思想》,想直接学习《Microsoft .NET程序设计技术内幕》,不知道看不看得懂??
public void RC4(byte[] Source,int SourceLength,byte[] seed)
{
Rc4Key key = new Rc4Key();
prepare_key(seed,seed.Length,key);
rc4(Source,SourceLength,key);
} public int RC4(string SourceFileName,string ObjFileName,string seed)
{
return RC4(SourceFileName,ObjFileName,MD5string(seed));
} public int RC4(string SourceFileName,string ObjFileName,byte[] seed)
{
try
{
Rc4Key key = new Rc4Key();
prepare_key(seed,seed.Length,key);
bool fin = System.IO.File.Exists( SourceFileName);
bool fout = System.IO.File.Exists( ObjFileName);
if(!fin) return 1;
if(fout) return 2;
System.IO.FileStream fw=null;
System.IO.FileStream fr=null;
fr = System.IO.File.OpenRead( SourceFileName );
fw = System.IO.File.OpenRead( ObjFileName );
byte[] buffer = new byte[BUFFER_SIZE];
int size=(int)fr.Length;
int ReadLength=0;
while(size>0)
{
ReadLength=size>BUFFER_SIZE?BUFFER_SIZE:size;
ReadLength=fr.Read(buffer,0,ReadLength);
size-=ReadLength;
rc4(buffer,ReadLength,key);
fw.Write(buffer,0,ReadLength);
}
fw.Close();
fr.Close();
return 0;
}
catch(IOException e)
{
throw e;
return -1;
}
} public void prepare_key(byte[] key_data_ptr, int key_data_len, Rc4Key key)
{
int index1;
int index2;
byte[] state;
short counter;
state = key.state;
for (counter = 0; counter < 256; counter++)
state[counter] = (byte)counter;
key.x = 0;
key.y = 0;
index1 = 0;
index2 = 0;
byte temp;
for(counter = 0; counter < 256; counter++)
{
index2 = (key_data_ptr[index1] + state[counter] + index2)&0xff;
temp = state[counter];
state[counter] = state[index2];
state[index2] = temp;
index1 = (index1 + 1) % key_data_len;
}
} public void rc4(byte[] buffer_ptr, int buffer_len, Rc4Key key)
{
int x;
int y;
byte[] state;
int xorIndex;
short counter;
byte temp;
x = key.x;
y = key.y;
state = key.state;
for(counter = 0; counter < buffer_len; counter++)
{
x =(x + 1)&0xff;
y = (state[x] + y)&0xff;
temp = state[x];
state[x] = state[y];
state[y] = (byte)temp;
xorIndex = (state[x] + state[y])&0xff;
buffer_ptr[counter] ^= state[xorIndex];
}
key.x = (byte)x;
key.y = (byte)y;
} public bool bytCompare(byte[] Source,byte[] Object)
{
int Length=Source.Length;
for(int i=0;i<Length;i++)
if(Source[i]!=Object[i]) return false;
return true;
}
#endregion
} /**
*
* rc主键
*
*/
public class Rc4Key
{
#region 数据变量
public byte[] state = new byte[256];
public byte x;
public byte y;
#endregion
}
}
这是不可逆算法
在验证用户的时候,一般都采用,把用户输入的密码再加一次密,与数据库或别的什么地方的加密密码进行比较
如果你要实现“找回密码”功能,就只能给用户一个随机产生的密码了,然后要用户自己去改
DES就是最出名的一个
还有Blowfish、ElGamal算法
对了,还有公钥密码系统DSA、RSA
好像还有一个什么ECC算法体系
static String Encrypt(String pass) {
byte[] bt = Encoding.Unicode.GetBytes(pass);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(pass, null);
byte[] key = pdb.GetBytes(24);
byte[] iv = pdb.GetBytes(8);
MemoryStream ms = new MemoryStream();
TripleDESCryptoServiceProvider tdesc = new TripleDESCryptoServiceProvider();
CryptoStream cs = new CryptoStream(ms,tdesc.CreateEncryptor(key, iv),CryptoStreamMode.Write);
cs.Write(bt, 0, bt.Length);
cs.FlushFinalBlock();
return Encoding.Unicode.GetString(ms.ToArray());
}
//解密,使用密码产生加密算法的公钥,并使用TripleDES对加密数据进行解密。
static String Decrypt(String str, String pass) {
byte[] bt = Encoding.Unicode.GetBytes(str);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(pass, null);
byte[] key = pdb.GetBytes(24);
byte[] iv = pdb.GetBytes(8);
MemoryStream ms = new MemoryStream();
TripleDESCryptoServiceProvider tdesc = new TripleDESCryptoServiceProvider();
CryptoStream cs = new CryptoStream(ms,tdesc.CreateDecryptor(key, iv),CryptoStreamMode.Write);
cs.Write(bt, 0, bt.Length);
cs.FlushFinalBlock();
return Encoding.Unicode.GetString(ms.ToArray());
}
使用:
String str = Encrypt("bbb");
Console.WriteLine(Decrypt(str, "bbb"));