VS2003、WINFORM,C#,一个简单的加密、解密算法。要求把字符串加密后再写到XML里,怎么实现?求大家帮助!谢谢先!

解决方案 »

  1.   

    如果不用还原,可以用MD5byte[] data = new byte[DATA_SIZE];// This is one implementation of the abstract class MD5.
    MD5 md5 = new MD5CryptoServiceProvider();byte[] result = md5.ComputeHash(data);如果需要还原,可以用 3des
    private static void EncryptData(String inName, String outName, byte[] desKey, byte[] desIV)
     {    
         //Create the file streams to handle the input and output files.
         FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
         FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
         fout.SetLength(0);
           
         //Create variables to help with read and write.
         byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
         long rdlen = 0;              //This is the total number of bytes written.
         long totlen = fin.Length;    //This is the total length of the input file.
         int len;                     //This is the number of bytes to be written at a time.
     
         DES des = new DESCryptoServiceProvider();          
         CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
                    
         Console.WriteLine("Encrypting...");
     
         //Read from the input file, then encrypt and write to the output file.
         while(rdlen < totlen)
         {
             len = fin.Read(bin, 0, 100);
             encStream.Write(bin, 0, len);
             rdlen = rdlen + len;
             Console.WriteLine("{0} bytes processed", rdlen);
         }
     
         encStream.Close();  
         fout.Close();
         fin.Close();                   
     }
      

  2.   

    http://www.jr163.org/cup2/49/49302.htm
      

  3.   

    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;namespace BS.Utility
    {
    public class MySecurity
    {
    #region 构造函数
    public MySecurity()
    {
    }
    #endregion #region ( 0 )Rijndael算法
    private SymmetricAlgorithm mobjCryptoService= new RijndaelManaged();
    private static string Key= "Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7";
    /// <summary>
    /// 获得密钥
    /// </summary>
    /// <returns>密钥</returns>
    private byte[] GetLegalKey()
    {
    string sTemp = Key;
    mobjCryptoService.GenerateKey();
    byte[] bytTemp = mobjCryptoService.Key;
    int KeyLength = bytTemp.Length;
    if (sTemp.Length > KeyLength)
    sTemp = sTemp.Substring(0, KeyLength);
    else if (sTemp.Length < KeyLength)
    sTemp = sTemp.PadRight(KeyLength, ' ');
    return ASCIIEncoding.ASCII.GetBytes(sTemp);
    }
    /// <summary>
    /// 获得初始向量IV
    /// </summary>
    /// <returns>初试向量IV</returns>
    private byte[] GetLegalIV()
    {
    string sTemp = "E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk";
    mobjCryptoService.GenerateIV();
    byte[] bytTemp = mobjCryptoService.IV;
    int IVLength = bytTemp.Length;
    if (sTemp.Length > IVLength)
    sTemp = sTemp.Substring(0, IVLength);
    else if (sTemp.Length < IVLength)
    sTemp = sTemp.PadRight(IVLength, ' ');
    return ASCIIEncoding.ASCII.GetBytes(sTemp);
    }
    /// <summary>
    /// 加密方法
    /// </summary>
    /// <param name="Source">待加密的串</param>
    /// <returns>经过加密的串</returns>
    public string RijndaelEncrypt(string Source)
    {
    byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
    MemoryStream ms = new MemoryStream();
    mobjCryptoService.Key = GetLegalKey();
    mobjCryptoService.IV = GetLegalIV();
    ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
    CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
    cs.Write(bytIn, 0, bytIn.Length);
    cs.FlushFinalBlock();
    ms.Close();
    byte[] bytOut = ms.ToArray();
    return Convert.ToBase64String(bytOut);
    }
    /// <summary>
    /// 解密方法
    /// </summary>
    /// <param name="Source">待解密的串</param>
    /// <returns>经过解密的串</returns>
    public string RijndaelDecrypt(string Source)
    {
    byte[] bytIn = Convert.FromBase64String(Source);
    MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
    mobjCryptoService.Key = GetLegalKey();
    mobjCryptoService.IV = GetLegalIV();
    ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
    CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
    StreamReader sr = new StreamReader(cs);
    return sr.ReadToEnd();
    }
    #endregion #region ( 1 )Base64与UTF8混用
    public string BUEncrypt( string bb )  
    {
    byte[] by=new byte[bb.Length];
    by=System.Text.Encoding.UTF8.GetBytes( bb );
        
    string r=Convert.ToBase64String( by );
    return r;
    } public string BUDecrypt( string aa )  
    {
    byte[] by=Convert.FromBase64String( aa );
        
    string r=Encoding.UTF8.GetString( by );
    return r;
    } #endregion #region ( 2 )倒序加1与顺序减1(中文支持不好)
    public string AddEncrypt( string rs ) //倒序加1加密  
    {
    byte[] by=new byte[rs.Length];
    for( int i=0;
    i<=rs.Length-1;
    i++ )
    {
    by[i]=( byte )( ( byte )rs[i]+1 );
    }
    rs="";
    for( int i=by.Length-1;
    i>=0;
    i-- )
    {
    rs+=( ( char )by[i] ).ToString( );
    }
    return rs;
    }
    public string AddDecrypt( string rs ) //顺序减1解码  
    {
    byte[] by=new byte[rs.Length];
    for( int i=0;
    i<=rs.Length-1;
    i++ )
    {
    by[i]=( byte )( ( byte )rs[i]-1 );
    }
    rs="";
    for( int i=by.Length-1;
    i>=0;
    i-- )
    {
    rs+=( ( char )by[i] ).ToString( );
    }
    return rs;
    } #endregion #region ( 3 )固定密钥算法
    private Byte[] Iv64={11, 22, 33, 44, 55, 66, 77, 88};
    private Byte[] byKey64={10, 20, 30, 40, 50, 60, 70, 80};
    //字符串加密
    public string SKeyEncrypt(string strText)
    {
    try
    {
    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    Byte[] inputByteArray  = Encoding.UTF8.GetBytes(strText);
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey64, Iv64), CryptoStreamMode.Write);
    cs.Write(inputByteArray, 0, inputByteArray.Length);
    cs.FlushFinalBlock();
    return Convert.ToBase64String(ms.ToArray());
    }
    catch(Exception ex)
    {
    return ex.Message;
    }
    } //字符串解密
    public string SKeyDecrypt(string strText)
    {
    Byte[] inputByteArray = new byte[strText.Length];
    try
    {
    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    inputByteArray = Convert.FromBase64String(strText);
    MemoryStream  ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey64, Iv64), CryptoStreamMode.Write);
    cs.Write(inputByteArray, 0, inputByteArray.Length);
    cs.FlushFinalBlock();
    System.Text.Encoding encoding = System.Text.Encoding.UTF8;
    return encoding.GetString(ms.ToArray());
    }
    catch(Exception ex)
    {
    return ex.Message;
    }
    }
    #endregion #region ( 4 )DES算法
    public static byte[] DESKey = new byte[] {0x82, 0xBC, 0xA1, 0x6A, 0xF5, 0x87, 0x3B, 0xE6, 0x59, 0x6A, 0x32, 0x64, 0x7F, 0x3A, 0x2A, 0xBB, 0x2B, 0x68, 0xE2, 0x5F, 0x06, 0xFB, 0xB8, 0x2D, 0x67, 0xB3, 0x55, 0x19, 0x4E, 0xB8, 0xBF, 0xDD };
    /// <summary>
    /// DES加密
    /// </summary>
    /// <param name="strSource">待加密字串</param>
    /// <param name="key">32位Key值</param>
    /// <returns>加密后的字符串</returns>
    public string DESEncrypt(string strSource) 
    {
    return DESEncrypt(strSource, DESKey);
    }
    private string DESEncrypt(string strSource,byte[] key)
    {
    SymmetricAlgorithm sa = Rijndael.Create();
    sa.Key = key;
    sa.Mode= CipherMode.ECB;
    sa.Padding = PaddingMode.Zeros;
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, sa.CreateEncryptor(), CryptoStreamMode.Write);
    byte[] byt = Encoding.Unicode.GetBytes(strSource);
    cs.Write(byt, 0, byt.Length);
    cs.FlushFinalBlock();
    cs.Close();
    return Convert.ToBase64String(ms.ToArray());
    }
    /// <summary>
    /// DES解密
    /// </summary>
    /// <param name="strSource">待解密的字串</param>
    /// <param name="key">32位Key值</param>
    /// <returns>解密后的字符串</returns>
    public string DESDecrypt(string strSource) 
    {
    return DESDecrypt(strSource, DESKey);
    }
    private string DESDecrypt(string strSource,byte[] key)
    {
    SymmetricAlgorithm sa = Rijndael.Create();
    sa.Key = key;
    sa.Mode = CipherMode.ECB;
    sa.Padding = PaddingMode.Zeros;
    ICryptoTransform ct = sa.CreateDecryptor();
    byte[] byt = Convert.FromBase64String(strSource);
    MemoryStream ms = new MemoryStream(byt);
    CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Read);
    StreamReader sr = new StreamReader(cs, Encoding.Unicode);
    return sr.ReadToEnd();
    }
    #endregion
    }
    }
      

  4.   

    public sealed class Secret
    { public static TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();  public Secret()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }
    /// 加密函数
    /// </summary>
    public static string EncryptString(string thisEncode)
    {
    string encrypted;
    byte[] Code = ASCIIEncoding.ASCII.GetBytes(thisEncode);
            
    encrypted = Convert.ToBase64String(
    des.CreateEncryptor().TransformFinalBlock(Code, 0, 
    Code.Length)
    );
            
    return encrypted;
    } /// <summary>
    /// 解密函数
    /// </summary>
    public static string DecryptString(string thisDecode)
    {
    string decrypted;
    byte[] Code = Convert.FromBase64String(thisDecode);
            
    decrypted = ASCIIEncoding.ASCII.GetString(
    des.CreateDecryptor().TransformFinalBlock(Code, 0, 
    Code.Length)
    ); return decrypted;
    }
    //加密一个给定的字符串,strEncrKey是一个字符串做为加密算子
    public string DesEncrypt(string strText, string strEncrKey)
    {
    if(strEncrKey == String.Empty)
    {
    strEncrKey = "songxiud";
    }
    byte[] byKey=null;
    byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
    try
    {
    byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0,strEncrKey.Length));
    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write) ;
    cs.Write(inputByteArray, 0, inputByteArray.Length);
    cs.FlushFinalBlock();
    return Convert.ToBase64String(ms.ToArray());
    }
    catch(System.Exception error)
    {
    return "error:" +error.Message+"\r";
    }
    } //解密一个给定的字符串,sDecrKey是一个字符串做为解密算子,
    public string DesDecrypt(string strText,string sDecrKey)
    {
    if(sDecrKey == "")
    {
    sDecrKey = "songxiud";
    }
    byte[] byKey = null;
    byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
    try
    {
    byte[] inputByteArray = Convert.FromBase64String(strText);
    byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0,8));
    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    //inputByteArray = Convert.FromBase64String(strText);
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write); 
    cs.Write(inputByteArray, 0, inputByteArray.Length); 
    cs.FlushFinalBlock();
    System.Text.Encoding encoding = new System.Text.UTF8Encoding();
    return encoding.GetString(ms.ToArray());
    }
    catch(System.Exception error)
    {
    return "error:"+error.Message+"\r";
    }
    }

    //加密一个文件
    public void DesEncrypt(string m_InFilePath,string m_OutFilePath,string strEncrKey)
    {
    byte[] byKey=null;
    byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
    try
    {
    byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0,8));
    FileStream fin = new FileStream(m_InFilePath, FileMode.Open, FileAccess.Read);
    FileStream fout = new FileStream(m_OutFilePath, FileMode.OpenOrCreate, FileAccess.Write);
    fout.SetLength(0);
    byte[] bin = new byte[100]; 
    long rdlen = 0;
    long totlen = fin.Length;
    int len; 
    DES des = new DESCryptoServiceProvider();
    CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
    while(rdlen < totlen)
    {
    len = fin.Read(bin, 0, 100);
    encStream.Write(bin, 0, len);
    rdlen = rdlen + len;
    } encStream.Close();
    fout.Close();
    fin.Close();
    }
    catch
    {

    }
    } //解密一个文件
    public void DesDecrypt(string m_InFilePath,string m_OutFilePath,string sDecrKey)
    {
    byte[] byKey = null;
    byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
    try
    {
    byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0,8));
    FileStream fin = new FileStream(m_InFilePath, FileMode.Open, FileAccess.Read);
    FileStream fout = new FileStream(m_OutFilePath, FileMode.OpenOrCreate, FileAccess.Write);
    fout.SetLength(0);

    byte[] bin = new byte[100]; 
    long rdlen = 0; 
    long totlen = fin.Length; 
    int len;  DES des = new DESCryptoServiceProvider();
    CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write); while(rdlen < totlen)
    {
    len = fin.Read(bin, 0, 100);
    encStream.Write(bin, 0, len);
    rdlen = rdlen + len;
    } encStream.Close();
    fout.Close();
    fin.Close(); }
    catch
    {

    }
    } //
    public string MD5Encrypt(string strText)
    {
    MD5 md5 = new MD5CryptoServiceProvider();
    byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strText));
    return System.Text.Encoding.Default.GetString(result);
    } //
    public string MD5Decrypt(string strText)
    {
    MD5 md5 = new MD5CryptoServiceProvider();
    byte[] result = md5.TransformFinalBlock(System.Text.Encoding.Default.GetBytes(strText),0,strText.Length);
    return System.Text.Encoding.Default.GetString(result);
    }
    }
      

  5.   

    另一种支持中文的加密算法
    版权信息:           此信息由 冰河魔法师 在 2006-8-5 提交 using System;
     
    /* The reason that i am using interface is that, in several 
    * weeks i will use a bitwise operator for have encryption and decryption
    * */
    public interface IBindesh
    {
        string encode(string str);
        string decode(string str);
    }
     
    namespace EncryptionDecryption
    {
        /// <summary>
        /// Summary description for EncryptionDecryption.
        /// </summary>
        public class EncryptionDecryption : IBindesh
        {
            public string encode(string str)
            {
                string htext = ""; // blank text
     
                for ( int i = 0; i < str.Length; i++)
                {
                    htext = htext + (char) (str[i] + 10 - 1 * 2);
                }
                return htext;
            }
     
            public string decode(string str)
            {
                string dtext = ""; 
     
                for ( int i=0; i < str.Length; i++)
                {
                    dtext = dtext + (char) (str[i] - 10 + 1*2);
                }
                return dtext;
            }
        }
    }
      

  6.   

    using System;
    using System.Text;
    using System.Security;
    using System.Security.Cryptography;
    using System.IO;namespace EncryptClasses
    {
        /// <summary>
        /// 此处定义的是DES加密,为了便于今后的管理和维护
        /// 请不要随便改动密码,或者改变了密码后请一定要
        /// 牢记先前的密码,否则将会照成不可预料的损失
        /// </summary>
        public class DESEncrypt
        {
            #region "member fields"
            private string iv = "12345678";
            private string key = "12345678";
            private Encoding encoding = new UnicodeEncoding();
            private DES des;
            #endregion
            /// <summary>
            /// 构造函数
            /// </summary>
            public DESEncrypt()
            {
                des = new DESCryptoServiceProvider();
            }
            #region "propertys"
            /// <summary>
            /// 设置加密密钥
            /// </summary>
            public string EncryptKey
            {
                get { return this.key; }
                set
                {
                    this.key = value;
                }
            }
            /// <summary>
            /// 要加密字符的编码模式
            /// </summary>
            public Encoding EncodingMode
            {
                get { return this.encoding; }
                set { this.encoding = value; }
            }
            #endregion
            #region "methods"
            /// <summary>
            /// 加密字符串并返回加密后的结果
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            public string EncryptString(string str)
            {
                byte[] ivb = Encoding.ASCII.GetBytes(this.iv);
                byte[] keyb = Encoding.ASCII.GetBytes(this.EncryptKey);//得到加密密钥
                byte[] toEncrypt = this.EncodingMode.GetBytes(str);//得到要加密的内容
                byte[] encrypted;
                ICryptoTransform encryptor = des.CreateEncryptor(keyb, ivb);
                MemoryStream msEncrypt = new MemoryStream();
                CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
                csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
                csEncrypt.FlushFinalBlock();
                encrypted = msEncrypt.ToArray();
                csEncrypt.Close();
                msEncrypt.Close();
                return this.EncodingMode.GetString(encrypted);
            }
            /// <summary>
            /// 加密指定的文件,如果成功返回True,否则false
            /// </summary>
            /// <param name="filePath">要加密的文件路径</param>
            /// <param name="outPath">加密后的文件输出路径</param>
            public void EncryptFile(string filePath, string outPath)
            {
                bool isExist = File.Exists(filePath);
                if (isExist)//如果存在
                {
                    byte[] ivb = Encoding.ASCII.GetBytes(this.iv);
                    byte[] keyb = Encoding.ASCII.GetBytes(this.EncryptKey);
                    //得到要加密文件的字节流
                    FileStream fin = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                    StreamReader reader = new StreamReader(fin, this.EncodingMode);
                    string dataStr = reader.ReadToEnd();
                    byte[] toEncrypt = this.EncodingMode.GetBytes(dataStr);
                    fin.Close();                FileStream fout = new FileStream(outPath, FileMode.Create, FileAccess.Write);
                    ICryptoTransform encryptor = des.CreateEncryptor(keyb, ivb);
                    CryptoStream csEncrypt = new CryptoStream(fout, encryptor, CryptoStreamMode.Write);
                    try
                    {
                        //加密得到的文件字节流
                        csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
                        csEncrypt.FlushFinalBlock();
                    }
                    catch (Exception err)
                    {
                        throw new ApplicationException(err.Message);
                    }
                    finally
                    {
                        try
                        {
                            fout.Close();
                            csEncrypt.Close();
                        }
                        catch
                        {
                            ;
                        }
                    }
                }
                else
                {
                    throw new FileNotFoundException("没有找到指定的文件");
                }
            }
            /// <summary>
            /// 文件加密函数的重载版本,如果不指定输出路径,
            /// 那么原来的文件将被加密后的文件覆盖
            /// </summary>
            /// <param name="filePath"></param>
            public void EncryptFile(string filePath)
            {
                this.EncryptFile(filePath, filePath);
            }    }
    }
      

  7.   

    /// <summary>
            /// 解密给定的字符串
            /// </summary>
            /// <param name="str">要解密的字符</param>
            /// <returns></returns>
            public string DecryptString(string str)
            {
                byte[] ivb = Encoding.ASCII.GetBytes(this.iv);
                byte[] keyb = Encoding.ASCII.GetBytes(this.EncryptKey);
                byte[] toDecrypt = this.EncodingMode.GetBytes(str);
                byte[] deCrypted = new byte[toDecrypt.Length];
                ICryptoTransform deCryptor = des.CreateDecryptor(keyb, ivb);
                MemoryStream msDecrypt = new MemoryStream(toDecrypt);
                CryptoStream csDecrypt = new CryptoStream(msDecrypt, deCryptor, CryptoStreamMode.Read);
                try
                {
                    csDecrypt.Read(deCrypted, 0, deCrypted.Length);
                }
                catch (Exception err)
                {
                    throw new ApplicationException(err.Message);
                }
                finally
                {
                    try
                    {
                        msDecrypt.Close();
                        csDecrypt.Close();
                    }
                    catch { ;}
                }
                return this.EncodingMode.GetString(deCrypted);
            }
            /// <summary>
            /// 解密指定的文件
            /// </summary>
            /// <param name="filePath">要解密的文件路径</param>
            /// <param name="outPath">解密后的文件输出路径</param>
            public void DecryptFile(string filePath, string outPath)
            {
                bool isExist = File.Exists(filePath);
                if (isExist)//如果存在
                {
                    byte[] ivb = Encoding.ASCII.GetBytes(this.iv);
                    byte[] keyb = Encoding.ASCII.GetBytes(this.EncryptKey);
                    FileInfo file = new FileInfo(filePath);
                    byte[] deCrypted = new byte[file.Length];
                    //得到要解密文件的字节流
                    FileStream fin = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                    //解密文件
                    try
                    {
                        ICryptoTransform decryptor = des.CreateDecryptor(keyb, ivb);
                        CryptoStream csDecrypt = new CryptoStream(fin, decryptor, CryptoStreamMode.Read);
                        csDecrypt.Read(deCrypted, 0, deCrypted.Length);
                    }
                    catch (Exception err)
                    {
                        throw new ApplicationException(err.Message);
                    }
                    finally
                    {
                        try
                        {
                            fin.Close();
                        }
                        catch { ;}
                    }
                    FileStream fout = new FileStream(outPath, FileMode.Create, FileAccess.Write);
                    fout.Write(deCrypted, 0, deCrypted.Length);
                    fout.Close();
                }
                else
                {
                    throw new FileNotFoundException("指定的解密文件没有找到");
                }
            }
            /// <summary>
            /// 解密文件的重载版本,如果没有给出解密后文件的输出路径,
            /// 则解密后的文件将覆盖先前的文件
            /// </summary>
            /// <param name="filePath"></param>
            public void DecryptFile(string filePath)
            {
                this.DecryptFile(filePath, filePath);
            }
            #endregion
      

  8.   

    /// <summary>
    /// 公钥
    /// </summary>
    private string xmlPublicKey; /// <summary>
    /// 私钥
    /// </summary>
    private string xmlPrivateKey; /// <summary>
    /// RSACryptoServiceProvider对象实例
    /// </summary>
    RSACryptoServiceProvider cryptoRSA ;                   /// <summary>
    /// 构造函数
    /// </summary>
    private RSAKey()
    {
    cryptoRSA = new RSACryptoServiceProvider(); xmlPublicKey = this.m_cryptoRSA.ToXmlString(false);          xmlPrivateKey = this.m_cryptoRSA.ToXmlString(true);
    }                  /// <summary>
    /// 获得公钥
    /// </summary>
    /// <returns>返回公钥xml字符串</returns>
    public string GetPublicKey()
    {
    return this.xmlPublicKey ;
    } /// <summary>
    /// 获得私钥
    /// </summary>
    /// <returns>返回私钥xml字符串</returns>
    public string GetPrivateKey()
    {
    return this.xmlPrivateKey ;
    }
                      
                      /// <summary>
    /// 采用RSA算法加密指定字符串
    /// </summary>
    /// <param name="source">待加密的源字符串</param>
    /// <param name="xmlPublicKey">xml格式的公钥</param>
    /// <param name="doOAEPPadding">是否采用OAEP填充</param>
    /// <returns>返回加密后的密文,Base64字符串表示形式</returns>
    public string RSAEncryptString(string source, string xmlPublicKey, bool doOAEPPadding)
    {
    byte[] buffer = Encoding.ASCII.GetBytes(source);
    byte[] result = RSAEncryptByte(buffer, xmlPublicKey, doOAEPPadding);
    return Convert.ToBase64String(result);
    } /// <summary>
    /// 解密指定用RSA加密了的字节字符串,以base64表示的字符串
    /// </summary>
    /// <param name="source">采用RSA加密了的字节数组</param>
    /// <param name="xmlPrivateKey">xml格式的私钥</param>
    /// <param name="doOAEPPadding">是否采用OAEP填充</param>
    /// <returns>解密后的字节数组</returns>
    public string RSADecryptString(string source, string xmlPrivateKey, bool doOAEPPadding)
    {
    byte[] buffer = Convert.FromBase64String(source);
    byte[] result = RSADecryptByte(buffer, xmlPrivateKey, doOAEPPadding);
    return Encoding.ASCII.GetString(result);
    }