SHA1是取摘要算法,不是加密算法。
不能还原,对称加密算法,比如RC4之类

解决方案 »

  1.   

    #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
    }
    }
      

  2.   

    SHAx???
    这是不可逆算法
    在验证用户的时候,一般都采用,把用户输入的密码再加一次密,与数据库或别的什么地方的加密密码进行比较
    如果你要实现“找回密码”功能,就只能给用户一个随机产生的密码了,然后要用户自己去改
      

  3.   

    对称加密算法有很多
    DES就是最出名的一个
    还有Blowfish、ElGamal算法
    对了,还有公钥密码系统DSA、RSA
    好像还有一个什么ECC算法体系
      

  4.   

    //加密,使用密码产生加密算法的公钥,并使用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"));