我使用文本文档保存了登陆时候的用户名和密码,请问怎么把这个文本进行加密,在线等啊,希望高手指点

解决方案 »

  1.   

    C#一个封装的加密解密类
    using System;
    using System.Text;
    using System.Globalization;
    using System.Security.Cryptography;
    using System.Windows.Forms;namespace Jh.Encrypts
    {
    public class JhEncrypt
    {
    /// <summary>
    /// 构造方法
    /// </summary>
    public JhEncrypt()
    {
    }
    /// <summary>
    /// 使用缺省密钥字符串加密
    /// </summary>
    /// <param name="original">明文</param>
    /// <returns>密文</returns>
    public static string Encrypt(string original)
    {
    return Encrypt(original,"JASONHEUNG");
    }
    /// <summary>
    /// 使用缺省密钥解密
    /// </summary>
    /// <param name="original">密文</param>
    /// <returns>明文</returns>
    public static string Decrypt(string original)
    {
    return Decrypt(original,"JASONHEUNG",System.Text.Encoding.Default);
    }
    /// <summary>
    /// 使用给定密钥解密
    /// </summary>
    /// <param name="original">密文</param>
    /// <param name="key">密钥</param>
    /// <returns>明文</returns>
    public static string Decrypt(string original, string key)
    {
    return Decrypt(original,key,System.Text.Encoding.Default);
    }
    /// <summary>
    /// 使用缺省密钥解密,返回指定编码方式明文
    /// </summary>
    /// <param name="original">密文</param>
    /// <param name="encoding">编码方式</param>
    /// <returns>明文</returns>
    public static string Decrypt(string original,Encoding encoding)
    {
    return Decrypt(original,"JASONHEUNG",encoding);
    }
    /// <summary>
    /// 使用给定密钥加密
    /// </summary>
    /// <param name="original">原始文字</param>
    /// <param name="key">密钥</param>
    /// <param name="encoding">字符编码方案</param>
    /// <returns>密文</returns>
    public static string Encrypt(string original, string key)
    {
    byte[] buff = System.Text.Encoding.Default.GetBytes(original);
    byte[] kb = System.Text.Encoding.Default.GetBytes(key);
    return Convert.ToBase64String(Encrypt(buff,kb));
    }/// <summary>
    /// 使用给定密钥解密
    /// </summary>
    /// <param name="encrypted">密文</param>
    /// <param name="key">密钥</param>
    /// <param name="encoding">字符编码方案</param>
    /// <returns>明文</returns>
    public static string Decrypt(string encrypted, string key,Encoding encoding)
    {
    byte[] buff = Convert.FromBase64String(encrypted);
    byte[] kb = System.Text.Encoding.Default.GetBytes(key);
    return encoding.GetString(Decrypt(buff,kb));
    }
    /// <summary>
    /// 生成MD5摘要
    /// </summary>
    /// <param name="original">数据源</param>
    /// <returns>摘要</returns>
    public static byte[] MakeMD5(byte[] original)
    {
    MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
    byte[] keyhash = hashmd5.ComputeHash(original);
    hashmd5 = null;
    return keyhash;
    }/// <summary>
    /// 使用给定密钥加密
    /// </summary>
    /// <param name="original">明文</param>
    /// <param name="key">密钥</param>
    /// <returns>密文</returns>
    public static byte[] Encrypt(byte[] original, byte[] key)
    {
    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
    des.Key = MakeMD5(key);
    des.Mode = CipherMode.ECB;return des.CreateEncryptor().TransformFinalBlock(original, 0, original.Length);
    }/// <summary>
    /// 使用给定密钥解密数据
    /// </summary>
    /// <param name="encrypted">密文</param>
    /// <param name="key">密钥</param>
    /// <returns>明文</returns>
    public static byte[] Decrypt(byte[] encrypted, byte[] key)
    {
    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
    des.Key = MakeMD5(key);
    des.Mode = CipherMode.ECB;return des.CreateDecryptor().TransformFinalBlock(encrypted, 0, encrypted.Length);
    }/// <summary>
    /// 使用给定密钥加密
    /// </summary>
    /// <param name="original">原始数据</param>
    /// <param name="key">密钥</param>
    /// <returns>密文</returns>
    public static byte[] Encrypt(byte[] original)
    {
    byte[] key = System.Text.Encoding.Default.GetBytes("JASONHEUNG");
    return Encrypt(original,key);
    }/// <summary>
    /// 使用缺省密钥解密数据
    /// </summary>
    /// <param name="encrypted">密文</param>
    /// <param name="key">密钥</param>
    /// <returns>明文</returns>
    public static byte[] Decrypt(byte[] encrypted)
    {
    byte[] key = System.Text.Encoding.Default.GetBytes("JASONHEUNG");
    return Decrypt(encrypted,key);
    }}
    }
      

  2.   

    第二种public sealed class Cryption
      {
    //members of the Cryption 
    //algorithm type in my case it’s RijndaelManaged
        private RijndaelManaged Algorithm;
    //memory stream
        private MemoryStream memStream;
    //ICryptoTransform interface
        private ICryptoTransform EncryptorDecryptor;
    //CryptoStream
        private CryptoStream crStream;
    //Stream writer and Stream reader
        private StreamWriter strWriter;
        private StreamReader strReader;
    //internal members
        private string m_key;
        private string m_iv;
    //the Key and the Inicialization Vector
        private byte[] key;
        private byte[] iv;
    //password view
        private string pwd_str;
        private byte[] pwd_byte;
    //Constructor
        public Cryption(string key_val, string iv_val)
        {
          key = new byte[32];
          iv = new byte[32];      int i;
          m_key = key_val;
          m_iv = iv_val;
    //key calculation, depends on first constructor parameter
          for(i=0;i < m_key.Length;i++)
          {
            key[i] = Convert.ToByte(m_key[i]);
          }
    //IV calculation, depends on second constructor parameter
          for(i=0;i < m_iv.Length;i++)
          {
            iv[i] = Convert.ToByte(m_iv[i]);
          }    }
    //Encrypt method implementation
        public string Encrypt(string s)
        {
    //new instance of algorithm creation
          Algorithm = new RijndaelManaged();//setting algorithm bit size
          Algorithm.BlockSize = 256;
          Algorithm.KeySize = 256;//creating new instance of Memory stream
          memStream = new MemoryStream();//creating new instance of the Encryptor
          EncryptorDecryptor = Algorithm.CreateEncryptor(key,iv);//creating new instance of CryptoStream
          crStream = new CryptoStream(memStream, EncryptorDecryptor, 
          CryptoStreamMode.Write);//creating new instance of Stream Writer
          strWriter = new StreamWriter(crStream);//cipher input string
          strWriter.Write(s);//clearing buffer for currnet writer and writing any 
    //buffered data to //the underlying device
          strWriter.Flush();
          crStream.FlushFinalBlock();//storing cipher string as byte array 
          pwd_byte = new byte[memStream.Length];
          memStream.Position = 0;
          memStream.Read(pwd_byte,0,(int)pwd_byte.Length);//storing cipher string as Unicode string
          pwd_str = new UnicodeEncoding().GetString(pwd_byte);      return pwd_str;
        }//Decrypt method implementation 
        public string Decrypt(string s)
        {
    //new instance of algorithm creation
          Algorithm = new RijndaelManaged();//setting algorithm bit size
          Algorithm.BlockSize = 256;
          Algorithm.KeySize = 256;//creating new Memory stream as stream for input string      
    MemoryStream memStream = new MemoryStream(
       new UnicodeEncoding().GetBytes(s));//Decryptor creating 
          ICryptoTransform EncryptorDecryptor = 
              Algorithm.CreateDecryptor(key,iv);//setting memory stream position
          memStream.Position = 0;//creating new instance of Crupto stream
          CryptoStream crStream = new CryptoStream(
              memStream,EncryptorDecryptor,CryptoStreamMode.Read);//reading stream
          strReader = new StreamReader(crStream);//returnig decrypted string
          return strReader.ReadToEnd();
        }
      }调用方法:
    Cryptography.Cryption Cryption = new Cryptography.Cryption(
      "Your key","Your vector");
      

  3.   

    何必自己写,有现成的不用?
    String 密文 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(你的文本, "MD5");或String 密文 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(你的文本, "SHA1");
      

  4.   

    这个方法,看 方法名称就知道它就是干你要的这事的.HashPasswordForStoringInConfigFileHash-Password-For-Storing-In-Config-File  扰乱 密码 为了 存储 在配置文件里 ......
      

  5.   

    http://www.ccsharp.net/list.aspx?bid=48
      

  6.   

    /// <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("没有找到指定的文件");
                }
            }
      

  7.   

    /// <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("指定的解密文件没有找到");
                }
            }
      

  8.   

    http://support.microsoft.com/default.aspx?scid=kb%3Bzh-cn%3B307010