(转)使用Vernam(维尔南/弗纳姆)算法实现文件加密解密[C#]
2010-06-04 12:03
1141 查看
原文:使用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类:
使用范例:
本文介绍如何通过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"); } }
相关文章推荐
- c#使用SHA256算法实现对文件的加密和解密
- C#实现对文件进行加密解密的方法
- Java实现的RSA算法,含加密解密算法,不使用工具类jar
- C#使用RSA证书文件加密和解密示例
- c#实现文件加密、解密及文件拖拽至程序图标直接打开
- 使用C#实现AES加密解密
- C#使用DES和AES实现加密解密功能示例
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
- 远程桌面,RDP文件密码加密、解密算法(C#)
- C#使用RSA证书文件加密和解密示例
- C#使用RSA证书文件加密和解密示例
- 使用对称加密与解密算法对文件加密与解密
- C#中使用WinRAR实现加密压缩及解压缩文件
- 【转】asp.net(c#)加密解密算法之sha1、md5、des、aes实现源码详解
- C# 利用ICSharpCode.SharpZipLib实现在线加密压缩和解密解压缩 C# 文件压缩加解密
- 使用C#实现AES加密解密
- c#实现文件加密、解密及文件拖拽至程序图标直接打开(转)
- C# 与JAVA 的RSA 加密解密交互,互通,C#使用BouncyCastle来实现私钥加密,公钥解密的方法
- [转]C# 使用 256 位 AES 加密和解密文件
- Java使用异或运算实现简单的加密解密算法实例代码