【.net】3des加密

来源:互联网 发布:知乎专栏怎么写文章 编辑:程序博客网 时间:2024/04/30 17:46
内存加密解密
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. using System.IO;
  5. class TrippleDESCSPSample
  6. {
  7.     static void Main()
  8.     {
  9.         try
  10.         {
  11.             // Create a new TripleDESCryptoServiceProvider object
  12.             // to generate a key and initialization vector (IV).
  13.             TripleDESCryptoServiceProvider tDESalg = new TripleDESCryptoServiceProvider();
  14.             // Create a string to encrypt.
  15.             string sData = "Here is some data to encrypt.";
  16.             // Encrypt the string to an in-memory buffer.
  17.             byte[] Data = EncryptTextToMemory(sData, tDESalg.Key, tDESalg.IV);
  18.             // Decrypt the buffer back to a string.
  19.             string Final = DecryptTextFromMemory(Data, tDESalg.Key, tDESalg.IV);
  20.             
  21.             // Display the decrypted string to the console.
  22.             Console.WriteLine(Final);
  23.         }
  24.         catch (Exception e)
  25.         {
  26.             Console.WriteLine(e.Message);
  27.         }
  28.        
  29.     }
  30.     public static byte[] EncryptTextToMemory(string Data,  byte[] Key, byte[] IV)
  31.     {
  32.         try
  33.         {
  34.             // Create a MemoryStream.
  35.             MemoryStream mStream = new MemoryStream();
  36.             // Create a CryptoStream using the MemoryStream 
  37.             // and the passed key and initialization vector (IV).
  38.             CryptoStream cStream = new CryptoStream(mStream, 
  39.                 new TripleDESCryptoServiceProvider().CreateEncryptor(Key, IV), 
  40.                 CryptoStreamMode.Write);
  41.             // Convert the passed string to a byte array.
  42.             byte[] toEncrypt = new ASCIIEncoding().GetBytes(Data);
  43.             // Write the byte array to the crypto stream and flush it.
  44.             cStream.Write(toEncrypt, 0, toEncrypt.Length);
  45.             cStream.FlushFinalBlock();
  46.         
  47.             // Get an array of bytes from the 
  48.             // MemoryStream that holds the 
  49.             // encrypted data.
  50.             byte[] ret = mStream.ToArray();
  51.             // Close the streams.
  52.             cStream.Close();
  53.             mStream.Close();
  54.             // Return the encrypted buffer.
  55.             return ret;
  56.         }
  57.         catch(CryptographicException e)
  58.         {
  59.             Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
  60.             return null;
  61.         }
  62.     }
  63.     public static string DecryptTextFromMemory(byte[] Data,  byte[] Key, byte[] IV)
  64.     {
  65.         try
  66.         {
  67.             // Create a new MemoryStream using the passed 
  68.             // array of encrypted data.
  69.             MemoryStream msDecrypt = new MemoryStream(Data);
  70.             // Create a CryptoStream using the MemoryStream 
  71.             // and the passed key and initialization vector (IV).
  72.             CryptoStream csDecrypt = new CryptoStream(msDecrypt, 
  73.                 new TripleDESCryptoServiceProvider().CreateDecryptor(Key, IV), 
  74.                 CryptoStreamMode.Read);
  75.             // Create buffer to hold the decrypted data.
  76.             byte[] fromEncrypt = new byte[Data.Length];
  77.             // Read the decrypted data out of the crypto stream
  78.             // and place it into the temporary buffer.
  79.             csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
  80.             //Convert the buffer into a string and return it.
  81.             return new ASCIIEncoding().GetString(fromEncrypt);
  82.         }
  83.         catch(CryptographicException e)
  84.         {
  85.             Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
  86.             return null;
  87.         }
  88.     }
  89. }

文件加密解密
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. using System.IO;
  5. class TrippleDESCSPSample
  6. {
  7.     static void Main()
  8.     {
  9.         try
  10.         {
  11.             // Create a new TripleDESCryptoServiceProvider object
  12.             // to generate a key and initialization vector (IV).
  13.             TripleDESCryptoServiceProvider tDESalg = new TripleDESCryptoServiceProvider();
  14.             // Create a string to encrypt.
  15.             string sData = "Here is some data to encrypt.";
  16.             string FileName = "CText.txt";
  17.             // Encrypt text to a file using the file name, key, and IV.
  18.             EncryptTextToFile(sData, FileName, tDESalg.Key, tDESalg.IV);
  19.             // Decrypt the text from a file using the file name, key, and IV.
  20.             string Final = DecryptTextFromFile(FileName, tDESalg.Key, tDESalg.IV);
  21.             
  22.             // Display the decrypted string to the console.
  23.             Console.WriteLine(Final);
  24.         }
  25.         catch (Exception e)
  26.         {
  27.             Console.WriteLine(e.Message);
  28.         }
  29.        
  30.     }
  31.     public static void EncryptTextToFile(String Data, String FileName, byte[] Key, byte[] IV)
  32.     {
  33.         try
  34.         {
  35.             // Create or open the specified file.
  36.             FileStream fStream = File.Open(FileName,FileMode.OpenOrCreate);
  37.             // Create a CryptoStream using the FileStream 
  38.             // and the passed key and initialization vector (IV).
  39.             CryptoStream cStream = new CryptoStream(fStream, 
  40.                 new TripleDESCryptoServiceProvider().CreateEncryptor(Key,IV), 
  41.                 CryptoStreamMode.Write); 
  42.             // Create a StreamWriter using the CryptoStream.
  43.             StreamWriter sWriter = new StreamWriter(cStream);
  44.             // Write the data to the stream 
  45.             // to encrypt it.
  46.             sWriter.WriteLine(Data);
  47.   
  48.             // Close the streams and
  49.             // close the file.
  50.             sWriter.Close();
  51.             cStream.Close();
  52.             fStream.Close();
  53.         }
  54.         catch(CryptographicException e)
  55.         {
  56.             Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
  57.         }
  58.         catch(UnauthorizedAccessException  e)
  59.         {
  60.             Console.WriteLine("A file access error occurred: {0}", e.Message);
  61.         }
  62.     }
  63.     public static string DecryptTextFromFile(String FileName, byte[] Key, byte[] IV)
  64.     {
  65.         try
  66.         {
  67.             // Create or open the specified file. 
  68.             FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate);
  69.   
  70.             // Create a CryptoStream using the FileStream 
  71.             // and the passed key and initialization vector (IV).
  72.             CryptoStream cStream = new CryptoStream(fStream, 
  73.                 new TripleDESCryptoServiceProvider().CreateDecryptor(Key,IV), 
  74.                 CryptoStreamMode.Read); 
  75.             // Create a StreamReader using the CryptoStream.
  76.             StreamReader sReader = new StreamReader(cStream);
  77.             // Read the data from the stream 
  78.             // to decrypt it.
  79.             string val = sReader.ReadLine();
  80.     
  81.             // Close the streams and
  82.             // close the file.
  83.             sReader.Close();
  84.             cStream.Close();
  85.             fStream.Close();
  86.             // Return the string. 
  87.             return val;
  88.         }
  89.         catch(CryptographicException e)
  90.         {
  91.             Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
  92.             return null;
  93.         }
  94.         catch(UnauthorizedAccessException  e)
  95.         {
  96.             Console.WriteLine("A file access error occurred: {0}", e.Message);
  97.             return null;
  98.         }
  99.     }
  100. }