(转)使用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类:
- using System;
- using System.IO;
- public class Vernam
- {
- /// <summary>
- /// Encrypts a file by the Vernam-algorithm
- /// </summary>
- /// <param name="originalFile">
- /// Name of the file to be encrypted. Data is read from this file.
- /// </param>
- /// <param name="encryptedFile">
- /// Name of the encrypted file. The encrypted data gets written to that file.
- /// </param>
- /// <param name="keyFile">
- /// Name of the key file. The one time key gets written to that file.
- /// </param>
- public void EncryptFile(string originalFile, string encryptedFile, string keyFile)
- {
- // Read in the bytes from the original file:
- byte[] originalBytes;
- using (FileStream fs = new FileStream(originalFile, FileMode.Open))
- {
- originalBytes = new byte[fs.Length];
- fs.Read(originalBytes, 0, originalBytes.Length);
- }
- // Create the one time key for encryption. This is done
- // by generating random bytes that are of the same lenght
- // as the original bytes:
- byte[] keyBytes = new byte[originalBytes.Length];
- Random random = new Random();
- random.NextBytes(keyBytes);
- // Write the key to the file:
- using (FileStream fs = new FileStream(keyFile, FileMode.Create))
- {
- fs.Write(keyBytes, 0, keyBytes.Length);
- }
- // Encrypt the data with the Vernam-algorithm:
- byte[] encryptedBytes = new byte[originalBytes.Length];
- DoVernam(originalBytes, keyBytes, ref encryptedBytes);
- // Write the encrypted file:
- using (FileStream fs = new FileStream(encryptedFile, FileMode.Create))
- {
- fs.Write(encryptedBytes, 0, encryptedBytes.Length);
- }
- }
- //---------------------------------------------------------------------
- /// <summary>
- /// Decrypts a file by Vernam-algorithm
- /// </summary>
- /// <param name="encryptedFile">
- /// Name of the encrypted file
- /// </param>
- /// <param name="keyFile">
- /// Name of the key file. The content of this file has to be the same
- /// as the content generated while encrypting
- /// </param>
- /// <param name="decryptedFile">
- /// Name of the decrypted file. The decrypted data gets written to this
- /// file
- /// </param>
- public void DecryptFile(string encryptedFile, string keyFile, string decryptedFile)
- {
- // Read in the encrypted bytes:
- byte[] encryptedBytes;
- using (FileStream fs = new FileStream(encryptedFile, FileMode.Open))
- {
- encryptedBytes = new byte[fs.Length];
- fs.Read(encryptedBytes, 0, encryptedBytes.Length);
- }
- // Read in the key:
- byte[] keyBytes;
- using (FileStream fs = new FileStream(keyFile, FileMode.Open))
- {
- keyBytes = new byte[fs.Length];
- fs.Read(keyBytes, 0, keyBytes.Length);
- }
- // Decrypt the data with the Vernam-algorithm:
- byte[] decryptedBytes = new byte[encryptedBytes.Length];
- DoVernam(encryptedBytes, keyBytes, ref decryptedBytes);
- // Write the decrypted file:
- using (FileStream fs = new FileStream(decryptedFile, FileMode.Create))
- {
- fs.Write(decryptedBytes, 0, decryptedBytes.Length);
- }
- }
- //---------------------------------------------------------------------
- /// <summary>
- /// Computes the Vernam-encryption/decryption
- /// </summary>
- /// <param name="inBytes"></param>
- /// <param name="keyBytes"></param>
- /// <param name="outBytes"></param>
- private void DoVernam(byte[] inBytes, byte[] keyBytes, ref byte[] outBytes)
- {
- // Check arguments:
- if ((inBytes.Length != keyBytes.Length) ||
- (keyBytes.Length != outBytes.Length))
- throw new ArgumentException("Byte-array are not of same length");
- // Encrypt/decrypt by XOR:
- for (int i = 0; i < inBytes.Length; i++)
- outBytes[i] = (byte)(inBytes[i] ^ keyBytes[i]);
- }
- }
使用范例:
- class Program
- {
- static void Main(string[] args)
- {
- Vernam vernam = new Vernam();
- // Test with an image:
- vernam.EncryptFile("Image.gif", "Image_encrypted.gif", "Key01.dat");
- vernam.DecryptFile("Image_encrypted.gif", "Key01.dat", "Image_decrypted.gif");
- // Test with text file:
- vernam.EncryptFile("Text.txt", "Text_encrypted.txt", "Key02.dat");
- vernam.DecryptFile("Text_encrypted.txt", "Key02.dat", "Text_decrypted.txt");
- // Test with pdf file:
- vernam.EncryptFile("Text.pdf", "Text_encrypted.pdf", "Key03.dat");
- vernam.DecryptFile("Text_encrypted.pdf", "Key03.dat", "Text_decrypted.pdf");
- }
- }