未标明原创的文章皆为转载~

(转)使用Vernam(维尔南/弗纳姆)算法实现文件加密解密[C#]

原文:使用Vernam(维尔南/弗纳姆)算法实现文件加密解密[C#]

 

本文介绍如何通过Gilbert Sandford Vernam的算法实现一个简洁而又稳定的文件加密解密类。通过此类加密的数据是绝对无法在没有密钥的情况下被破解的。它的基本原理是,需要有一个需要加密的明文和一个随机生成的解密钥匙文件。然后使用这两个文件组合起来生成密文:(明文) 组合 (密钥) = 加密后的密文。
使用Vernam加密算法,经其处理的密钥可以拥有与待加密文件大小相同的密钥长度,而且输出文件的大小相比待加密文件无任何改变(精确到字节)。换言之,密钥文件越大,加密强度越高!举个例子,如果想加密一个5M的文件,那么密钥长度将高达40,000,000位,输出文件大小则仍为5M。前面的数字意味着即使是梦幻配置的个人电脑,想要在“有生之年”靠穷取法破解出密码,也是不可能完成的任务!待加密文件类型不限,密钥文件也可以是任何数据:应用程序、交换文件,或者音乐文件,甚至是您宠物的靓照,等等...

Vernam密码算法:
    1、 现代密码体制的萌芽是Vernam加密方法。
    2、Vernam密码是美国电话电报公司的Gilbert Vernam在1917年为电报通信设计的一种非常方便的密码,它在近代计算机和通信系统设计中得到了广泛应用。
    3、Vernam密码的明文、密钥和密文均用二元数字序列表示。这是一种使用异或方法进行加密解密的方法。
    4、要编制Vernam密码,只需先把明文和密钥表示成二元序列,再把它们按位模2相加,就可得到密文。
    5、而解密只需把密文和密钥的二元序列按位模2相加便可得到明文。
    6、开始时使用一个定长的密钥序列,这样产生的密文能形成有规律的反复,易被破译;后来采用的密钥与明文同长,且密钥序列只用一次,称为“一次一密体制”。

Vernam类:

  1. using System; 
  2. using System.IO; 
  3.  
  4. public class Vernam 
  5.     /// <summary> 
  6.     /// Encrypts a file by the Vernam-algorithm 
  7.     /// </summary> 
  8.     /// <param name="originalFile"> 
  9.     /// Name of the file to be encrypted. Data is read from this file. 
  10.     /// </param> 
  11.     /// <param name="encryptedFile"> 
  12.     /// Name of the encrypted file. The encrypted data gets written to that file. 
  13.     /// </param> 
  14.     /// <param name="keyFile"> 
  15.     /// Name of the key file. The one time key gets written to that file. 
  16.     /// </param> 
  17.     public void EncryptFile(string originalFile, string encryptedFile, string keyFile) 
  18.     { 
  19.         // Read in the bytes from the original file: 
  20.         byte[] originalBytes; 
  21.         using (FileStream fs = new FileStream(originalFile, FileMode.Open)) 
  22.         { 
  23.             originalBytes = new byte[fs.Length]; 
  24.             fs.Read(originalBytes, 0, originalBytes.Length); 
  25.         } 
  26.  
  27.         // Create the one time key for encryption. This is done 
  28.         // by generating random bytes that are of the same lenght  
  29.         // as the original bytes: 
  30.         byte[] keyBytes = new byte[originalBytes.Length]; 
  31.         Random random = new Random(); 
  32.         random.NextBytes(keyBytes); 
  33.  
  34.         // Write the key to the file: 
  35.         using (FileStream fs = new FileStream(keyFile, FileMode.Create)) 
  36.         { 
  37.             fs.Write(keyBytes, 0, keyBytes.Length); 
  38.         } 
  39.  
  40.         // Encrypt the data with the Vernam-algorithm: 
  41.         byte[] encryptedBytes = new byte[originalBytes.Length]; 
  42.         DoVernam(originalBytes, keyBytes, ref encryptedBytes); 
  43.  
  44.         // Write the encrypted file: 
  45.         using (FileStream fs = new FileStream(encryptedFile, FileMode.Create)) 
  46.         { 
  47.             fs.Write(encryptedBytes, 0, encryptedBytes.Length); 
  48.         } 
  49.     } 
  50.     //--------------------------------------------------------------------- 
  51.     /// <summary> 
  52.     /// Decrypts a file by Vernam-algorithm 
  53.     /// </summary> 
  54.     /// <param name="encryptedFile"> 
  55.     /// Name of the encrypted file 
  56.     /// </param> 
  57.     /// <param name="keyFile"> 
  58.     /// Name of the key file. The content of this file has to be the same 
  59.     /// as the content generated while encrypting 
  60.     /// </param> 
  61.     /// <param name="decryptedFile"> 
  62.     /// Name of the decrypted file. The decrypted data gets written to this  
  63.     /// file 
  64.     /// </param> 
  65.     public void DecryptFile(string encryptedFile, string keyFile, string decryptedFile) 
  66.     { 
  67.         // Read in the encrypted bytes: 
  68.         byte[] encryptedBytes; 
  69.         using (FileStream fs = new FileStream(encryptedFile, FileMode.Open)) 
  70.         { 
  71.             encryptedBytes = new byte[fs.Length]; 
  72.             fs.Read(encryptedBytes, 0, encryptedBytes.Length); 
  73.         } 
  74.  
  75.         // Read in the key: 
  76.         byte[] keyBytes; 
  77.         using (FileStream fs = new FileStream(keyFile, FileMode.Open)) 
  78.         { 
  79.             keyBytes = new byte[fs.Length]; 
  80.             fs.Read(keyBytes, 0, keyBytes.Length); 
  81.         } 
  82.  
  83.         // Decrypt the data with the Vernam-algorithm: 
  84.         byte[] decryptedBytes = new byte[encryptedBytes.Length]; 
  85.         DoVernam(encryptedBytes, keyBytes, ref decryptedBytes); 
  86.  
  87.         // Write the decrypted file: 
  88.         using (FileStream fs = new FileStream(decryptedFile, FileMode.Create)) 
  89.         { 
  90.             fs.Write(decryptedBytes, 0, decryptedBytes.Length); 
  91.         } 
  92.     } 
  93.     //--------------------------------------------------------------------- 
  94.     /// <summary> 
  95.     /// Computes the Vernam-encryption/decryption 
  96.     /// </summary> 
  97.     /// <param name="inBytes"></param> 
  98.     /// <param name="keyBytes"></param> 
  99.     /// <param name="outBytes"></param> 
  100.     private void DoVernam(byte[] inBytes, byte[] keyBytes, ref byte[] outBytes) 
  101.     { 
  102.         // Check arguments: 
  103.         if ((inBytes.Length != keyBytes.Length) || 
  104.             (keyBytes.Length != outBytes.Length)) 
  105.             throw new ArgumentException("Byte-array are not of same length"); 
  106.  
  107.         // Encrypt/decrypt by XOR: 
  108.         for (int i = 0; i < inBytes.Length; i++) 
  109.             outBytes[i] = (byte)(inBytes[i] ^ keyBytes[i]); 
  110.     } 

使用范例:

  1. class Program 
  2.     static void Main(string[] args) 
  3.     { 
  4.         Vernam vernam = new Vernam(); 
  5.  
  6.         // Test with an image: 
  7.         vernam.EncryptFile("Image.gif""Image_encrypted.gif""Key01.dat"); 
  8.         vernam.DecryptFile("Image_encrypted.gif""Key01.dat""Image_decrypted.gif"); 
  9.  
  10.         // Test with text file: 
  11.         vernam.EncryptFile("Text.txt""Text_encrypted.txt""Key02.dat"); 
  12.         vernam.DecryptFile("Text_encrypted.txt""Key02.dat""Text_decrypted.txt"); 
  13.  
  14.         // Test with pdf file: 
  15.         vernam.EncryptFile("Text.pdf""Text_encrypted.pdf""Key03.dat"); 
  16.         vernam.DecryptFile("Text_encrypted.pdf""Key03.dat""Text_decrypted.pdf"); 
  17.     } 

 

 

posted @ 2010-06-04 12:03  CodeYu  阅读(2650)  评论(0编辑  收藏  举报