首页 > 代码库 > 加密与解密

加密与解密

  1. 加密解密文件
     1         /// <summary> 2         /// Enctypt File 3         /// </summary> 4         /// <param name="sInputFilename"></param> 5         /// <param name="sOutputFilename"></param> 6         /// <param name="sKey"></param> 7         public void EncryptFile(string sInputFilename, string sOutputFilename, string sKey) 8         { 9             DESCryptoServiceProvider DES = new DESCryptoServiceProvider();10             //A 64 bit key and IV is required for this provider.11             //Set secret key For DES algorithm.12             DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);13             //Set initialization vector.14             DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);15 16             FileStream fin = null;17             FileStream fout = null;18             CryptoStream cryptoStream = null;19             try20             {21                 fin = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);22                 fout = new FileStream(sOutputFilename, FileMode.OpenOrCreate, FileAccess.Write);23                 cryptoStream = new CryptoStream(fout, DES.CreateEncryptor(), CryptoStreamMode.Write);24 25                 byte[] bin = new byte[100];  //This is intermediate storage for the decryption.26                 long rdlen = 0;              //This is the total number of bytes written.27                 long totlen = fin.Length;    //This is the total length of the input file.28                 int len;                     //This is the number of bytes to be written at a time.29 30                 //Read from the input file, then encrypt and write to the output file.31                 while (rdlen < totlen)32                 {33                     len = fin.Read(bin, 0, 100);34                     cryptoStream.Write(bin, 0, len);35                     rdlen = rdlen + len;36                 }37             }38             catch (Exception ex)39             {40                 throw ex;41             }42             finally43             {44                 if (cryptoStream != null) { cryptoStream.Close(); }45                 if (fout != null) { fout.Close(); }46                 if (fin != null) { fin.Close(); }47             }48         }49 50         /// <summary>51         /// Decrypt File52         /// </summary>53         /// <param name="sInputFilename"></param>54         /// <param name="sOutputFilename"></param>55         /// <param name="sKey"></param>56         public void DecryptFile(string sInputFilename, string sOutputFilename,string sKey)57         {58             DESCryptoServiceProvider DES = new DESCryptoServiceProvider();59             //A 64 bit key and IV is required for this provider.60             //Set secret key For DES algorithm.61             DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);62             //Set initialization vector.63             DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);64 65             FileStream fin = null;66             FileStream fout = null;67             CryptoStream cryptoStream = null;68             try69             {70                 fin = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);71                 fout = new FileStream(sOutputFilename, FileMode.OpenOrCreate, FileAccess.Write);72                 cryptoStream = new CryptoStream(fin, DES.CreateDecryptor(), CryptoStreamMode.Read);73               74                 byte[] bin = new byte[100];  //This is intermediate storage for the decryption.75                 long rdlen = 0;              //This is the total number of bytes written.76                 long totlen = fin.Length;    //This is the total length of the input file.77                 int len;                     //This is the number of bytes to be written at a time.78 79                 //Read from the input file, then encrypt and write to the output file.80                 while (rdlen < totlen)81                 {82                     len = cryptoStream.Read(bin, 0, 100);83                     if (len == 0) { break; }84                     fout.Write(bin, 0, len);85                     rdlen = rdlen + len;86                 }87             }88             catch(Exception ex)89             {90                 throw ex;91             }92             finally93             {94                 if (cryptoStream != null) { cryptoStream.Close(); }95                 if (fout != null) { fout.Close(); }96                 if (fin != null) { fin.Close(); }97             }98         }
    View Code
  2. 加密解密文本
     1         /// <summary> 2         /// Encrypt Text 3         /// </summary> 4         public string DesEncrypt(string pToEncrypt, string sKey) 5         { 6             MemoryStream ms = null; 7             CryptoStream ctyptoStream = null; 8             try 9             {10                 DESCryptoServiceProvider des = new DESCryptoServiceProvider();11                 des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);12                 des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);13 14                 byte[] inputByteArray = Encoding.ASCII.GetBytes(pToEncrypt);15               16                 ms = new System.IO.MemoryStream();17                 ctyptoStream = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);18                 ctyptoStream.Write(inputByteArray, 0, inputByteArray.Length);19                 ctyptoStream.FlushFinalBlock();20             }21             catch (Exception ex)22             {23                 throw ex;24             }25             finally26             {27                 if (ctyptoStream != null) { ctyptoStream.Close(); }28                 if (ms != null) { ms.Close(); }29             }30             return Convert.ToBase64String(ms.ToArray());31         }32 33         /// <summary>34         /// Dectypt Text35         /// </summary>36         public string DesDecrypt(string pToDecrypt, string sKey)37         {38             MemoryStream ms = null;39             CryptoStream ctyptoStream = null;40             try41             {42                 DESCryptoServiceProvider des = new DESCryptoServiceProvider();43                 des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);44                 des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);45 46                 byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);47 48                 ms = new System.IO.MemoryStream();49                 ctyptoStream = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);50                 ctyptoStream.Write(inputByteArray, 0, inputByteArray.Length);51                 ctyptoStream.FlushFinalBlock();52             }53             catch (Exception ex)54             {55                 throw ex;56             }57             finally58             {59                 if (ctyptoStream != null) { ctyptoStream.Close(); }60                 if (ms != null) { ms.Close(); }61             }62             return Encoding.ASCII.GetString(ms.ToArray());63         }
    View Code

加密与解密