http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=35935

解决方案 »

  1.   


      see:
      http://community.csdn.net/Expert/topic/3109/3109695.xml?temp=.194256
      

  2.   

    /// <summary>
    /// des进行加密
    /// </summary>
    /// <param name="pwd"></param>
    /// <returns></returns>
    static String Encrypt(String pwd) 
    {
    DESCryptoServiceProvider desc = new DESCryptoServiceProvider();//des进行加密
    PasswordDeriveBytes db = new PasswordDeriveBytes(pwd, null);//产生key
    byte[] key = db.GetBytes(8);
    MemoryStream ms = new MemoryStream();//存储加密后的数据
    CryptoStream cs = new CryptoStream(ms,desc.CreateEncryptor(key, key),CryptoStreamMode.Write);
    byte[] data = Encoding.Unicode.GetBytes(pwd);//取到密码的字节流
    cs.Write(data, 0, data.Length);//进行加密
    cs.FlushFinalBlock();
    byte[] res = ms.ToArray();//取加密后的数据
    return Encoding.Unicode.GetString(res);//转换到字符串返回
    }
    /// <summary>
    /// des进行解密:
    /// </summary>
    /// <param name="pwd"></param>
    /// <param name="data"></param>
    /// <returns></returns> static String Decrypt(String pwd, String data) 
    {
    DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
    PasswordDeriveBytes db = new PasswordDeriveBytes(pwd, null);//产生key
    byte[] key = db.GetBytes(8);
    MemoryStream ms = new MemoryStream();//存储解密后的数据
    CryptoStream cs = new CryptoStream(ms,desc.CreateDecryptor(key, key),CryptoStreamMode.Write);
    byte[] databytes = Encoding.Unicode.GetBytes(data);//取到加密后的数据的字节流
    cs.Write(databytes, 0, databytes.Length);//解密数据
    cs.FlushFinalBlock();
    byte[] res = ms.ToArray();
    return Encoding.Unicode.GetString(res);//返回解密后的数据
    }
      

  3.   

    appleblossom(编程人生)
    是个MM哦。
    靓MM
      

  4.   

    不会啊,兄弟
      我只会MD5的,帮你顶顶,记着给我分哦,看ID就知道我是谁了啊
        嘻嘻.........
      

  5.   

    看看这个类吧using System;
    using System.IO;
    using System.Text;
    using System.Security;
    using System.Security.Cryptography;namespace FE_SymmetricNamespace
    { public class FE_Symmetric 
    {
    static private Byte[] m_Key = new Byte[8]; //
    static private Byte[] m_IV = new Byte[8]; //

    //////////////////////////
    //Function to encrypt data
    public string EncryptData(String strKey, String strData)
    {
    string strResult; //Return Result //1. String Length cannot exceed 90Kb. Otherwise, buffer will overflow. See point 3 for reasons
    if (strData.Length > 92160)
    {
    strResult="Error. Data String too large. Keep within 90Kb.";
    return strResult;
    }

    //2. Generate the Keys
    if (!InitKey(strKey))
    {
    strResult="Error. Fail to generate key for encryption";
    return strResult;
    } //3. Prepare the String
    // The first 5 character of the string is formatted to store the actual length of the data.
    // This is the simplest way to remember to original length of the data, without resorting to complicated computations.
    // If anyone figure a good way to 'remember' the original length to facilite the decryption without having to use additional function parameters, pls let me know.
    strData = String.Format("{0,5:00000}"+strData, strData.Length);
    //4. Encrypt the Data
    byte[] rbData = new byte[strData.Length];
    ASCIIEncoding aEnc = new ASCIIEncoding();
    aEnc.GetBytes(strData, 0, strData.Length, rbData, 0);

    DESCryptoServiceProvider descsp = new DESCryptoServiceProvider(); 

    ICryptoTransform desEncrypt = descsp.CreateEncryptor(m_Key, m_IV); 
    //5. Perpare the streams:
    // mOut is the output stream. 
    // mStream is the input stream.
    // cs is the transformation stream.
    MemoryStream mStream = new MemoryStream(rbData); 
    CryptoStream cs = new CryptoStream(mStream, desEncrypt, CryptoStreamMode.Read);        
    MemoryStream mOut = new MemoryStream();

    //6. Start performing the encryption
    int bytesRead; 
    byte[] output = new byte[1024]; 
    do 

    bytesRead = cs.Read(output,0,1024);
    if (bytesRead != 0) 
    mOut.Write(output,0,bytesRead); 
    } while (bytesRead > 0); 

    //7. Returns the encrypted result after it is base64 encoded
    // In this case, the actual result is converted to base64 so that it can be transported over the HTTP protocol without deformation.
    if (mOut.Length == 0)
    strResult = "";
    else
    strResult = Convert.ToBase64String(mOut.GetBuffer(), 0, (int)mOut.Length);

    return strResult;
    } //////////////////////////
    //Function to decrypt data
    public string DecryptData(String strKey, String strData)
    {
    string strResult; //1. Generate the Key used for decrypting
    if (!InitKey(strKey))
    {
    strResult="Error. Fail to generate key for decryption";
    return strResult;
    } //2. Initialize the service provider
    int nReturn = 0;
    DESCryptoServiceProvider descsp = new DESCryptoServiceProvider(); 
    ICryptoTransform desDecrypt = descsp.CreateDecryptor(m_Key, m_IV); 

    //3. Prepare the streams:
    // mOut is the output stream. 
    // cs is the transformation stream.
    MemoryStream mOut = new MemoryStream();
    CryptoStream cs = new CryptoStream(mOut, desDecrypt, CryptoStreamMode.Write);        

    //4. Remember to revert the base64 encoding into a byte array to restore the original encrypted data stream
    byte[] bPlain = new byte[strData.Length];
    try 
    {
    bPlain = Convert.FromBase64CharArray(strData.ToCharArray(), 0, strData.Length);
    }
    catch (Exception) 

    strResult = "Error. Input Data is not base64 encoded.";
    return strResult;
    }

    long lRead = 0;
    long lTotal = strData.Length;

    try
    {
    //5. Perform the actual decryption
    while (lTotal >= lRead)

    cs.Write(bPlain,0,(int)bPlain.Length); 
    //descsp.BlockSize=64
    lRead = mOut.Length + Convert.ToUInt32(((bPlain.Length / descsp.BlockSize) * descsp.BlockSize));
    };

    ASCIIEncoding aEnc = new ASCIIEncoding();
    strResult = aEnc.GetString(mOut.GetBuffer(), 0, (int)mOut.Length);

    //6. Trim the string to return only the meaningful data
    // Remember that in the encrypt function, the first 5 character holds the length of the actual data
    // This is the simplest way to remember to original length of the data, without resorting to complicated computations.
    String strLen = strResult.Substring(0,5);
    int nLen = Convert.ToInt32(strLen);
    strResult = strResult.Substring(5, nLen);
    nReturn = (int)mOut.Length;

    return strResult;
    }
    catch (Exception)
    {
    strResult = "Error. Decryption Failed. Possibly due to incorrect Key or corrputed data";
    return strResult;
    }
    } /////////////////////////////////////////////////////////////
    //Private function to generate the keys into member variables
    static private bool InitKey(String strKey)
    {
    try
    {
    // Convert Key to byte array
    byte[] bp = new byte[strKey.Length];
    ASCIIEncoding aEnc = new ASCIIEncoding();
    aEnc.GetBytes(strKey, 0, strKey.Length, bp, 0);

    //Hash the key using SHA1
    SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
    byte[] bpHash = sha.ComputeHash(bp);

    int i;
    // use the low 64-bits for the key value
    for (i=0; i<8; i++) 
    m_Key[i] = bpHash[i];

    for (i=8; i<16; i++) 
    m_IV[i-8] = bpHash[i];

    return true;
    }
    catch (Exception)
    {
    //Error Performing Operations
    return false;
    }
    }
    }
    }
      

  6.   

    using FE_SymmetricNamespace;
    FE_Symmetric Crypt;Crypt = new FE_Symmetric();
    this.EmailAddress = Crypt.DecryptData("42", tempAddress);
    this.Password = Crypt.DecryptData("42", tempPassword);
    MessageBox.Show(Crypt.EncryptData("42", this.EmailAddress));
      

  7.   

    ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfSystemSecurityCryptographyDESMembersTopic.htm