) Use the vernam (vernam/vernam) algorithm to implement file encryption and decryption [C #]

Source: Internet
Author: User

Original article: file encryption and decryption using vernam (vernam/fhem) algorithm [C #]

 

This article describes how to useAlgorithmImplements a simple and stable file encryption and decryption class. The encrypted data cannot be cracked without a key. The basic principle is to have a plaintext to be encrypted and a random decryption key file. Then the two files are combined to generate the ciphertext: (plaintext) combination (key) = encrypted ciphertext.
Using the vernam encryption algorithm, the processed key can have the same length as the size of the file to be encrypted, and the size of the output file does not change (accurate to bytes) compared to the file to be encrypted ). In other words, the larger the key file, the higher the encryption strength! For example, if you want to encrypt a file of 5 MB, the key length will be as high as 40,000,000 bits, and the output file size will still be 5 MB. The preceding figures mean that even a dream-configured PC would not be able to crack the password in the "lifetime! The type of the file to be encrypted is not limited. The key file can also be any data: ApplicationProgram, Exchange files, or music files, or even photos of your pets, etc...

Vernam password algorithm:
1. vernam is the initial method of encryption in modern cryptographic systems.
2. vernam password is a very convenient password designed for telegraph communication in 1917 by the American Telephone and Telegraph Corporation Gilbert vernam. It has been widely used in modern computer and communication system design.
3. vernam passwords are expressed in a binary sequence in plaintext, key, and ciphertext. This is an encryption and decryption method using an exclusive or method.
4. To compile a vernam password, you only need to first express the plaintext and key into a binary sequence, and then add them to the bitwise Mode 2 to obtain the ciphertext.
5. For decryption, you only need to add the binary sequence of the ciphertext and the key to get the plaintext.
6. A fixed-length key sequence is used at the beginning, so that the generated ciphertext can form a regular repetition and is easy to be deciphered. Later, the key used is the same length as the plaintext, and the key sequence is used only once, called "one password system at a time ".

Vernam class:

 
 
  1. UsingSystem;
  2. UsingSystem. IO;
  3.  
  4. Public ClassVernam
  5. {
  6. /// <Summary> 
  7. /// Encrypts a file by the vernam-Algorithm 
  8. /// </Summary> 
  9. /// <Param name = "originalfile"> 
  10. /// Name of the file to be encrypted. Data is read from this file. 
  11. /// </Param> 
  12. /// <Param name = "encryptedfile"> 
  13. /// Name of the encrypted file. The encrypted data gets written to that file. 
  14. /// </Param> 
  15. /// <Param name = "Keyfile"> 
  16. /// Name of the key file. The one time key gets written to that file. 
  17. /// </Param> 
  18. Public VoidEncryptfile (StringOriginalfile,StringEncryptedfile,StringKeyfile)
  19. {
  20. // Read in the bytes from the original file: 
  21. Byte[] Originalbytes;
  22. Using(Filestream FS =NewFilestream (originalfile, filemode. Open ))
  23. {
  24. Originalbytes =New Byte[Fs. Length];
  25. FS. Read (originalbytes, 0, originalbytes. Length );
  26. }
  27.  
  28. // Create the one time key for encryption. This is done 
  29. // By generating random bytes that are of the same lenght 
  30. // As the original Bytes: 
  31. Byte[] Keybytes =New Byte[Originalbytes. Length];
  32. Random random =NewRandom ();
  33. Random. nextbytes (keybytes );
  34.  
  35. // Write the key to the file: 
  36. Using(Filestream FS =NewFilestream (Keyfile, filemode. Create ))
  37. {
  38. FS. Write (keybytes, 0, keybytes. Length );
  39. }
  40.  
  41. // Encrypt the data with the vernam-algorithm: 
  42. Byte[] Encryptedbytes =New Byte[Originalbytes. Length];
  43. Dovernam (originalbytes, keybytes,RefEncryptedbytes );
  44.  
  45. // Write the encrypted file: 
  46. Using(Filestream FS =NewFilestream (encryptedfile, filemode. Create ))
  47. {
  48. FS. Write (encryptedbytes, 0, encryptedbytes. Length );
  49. }
  50. }
  51. //--------------------------------------------------------------------- 
  52. /// <Summary> 
  53. /// Decrypts a file by vernam-Algorithm 
  54. /// </Summary> 
  55. /// <Param name = "encryptedfile"> 
  56. /// Name of the encrypted file 
  57. /// </Param> 
  58. /// <Param name = "Keyfile"> 
  59. /// Name of the key file. The content of this file has to be the same 
  60. /// As the content generated while encrypting 
  61. /// </Param> 
  62. /// <Param name = "decryptedfile"> 
  63. /// Name of the decrypted file. The decrypted data gets written to this 
  64. /// File 
  65. /// </Param> 
  66. Public VoidDecryptfile (StringEncryptedfile,StringKeyfile,StringDecryptedfile)
  67. {
  68. // Read in the encrypted Bytes: 
  69. Byte[] Encryptedbytes;
  70. Using(Filestream FS =NewFilestream (encryptedfile, filemode. Open ))
  71. {
  72. Encryptedbytes =New Byte[Fs. Length];
  73. FS. Read (encryptedbytes, 0, encryptedbytes. Length );
  74. }
  75.  
  76. // Read in the key: 
  77. Byte[] Keybytes;
  78. Using(Filestream FS =NewFilestream (Keyfile, filemode. Open ))
  79. {
  80. Keybytes =New Byte[Fs. Length];
  81. FS. Read (keybytes, 0, keybytes. Length );
  82. }
  83.  
  84. // Decrypt the data with the vernam-algorithm: 
  85. Byte[] Decryptedbytes =New Byte[Encryptedbytes. Length];
  86. Dovernam (encryptedbytes, keybytes,RefDecryptedbytes );
  87.  
  88. // Write the decrypted file: 
  89. Using(Filestream FS =NewFilestream (decryptedfile, filemode. Create ))
  90. {
  91. FS. Write (decryptedbytes, 0, decryptedbytes. Length );
  92. }
  93. }
  94. //--------------------------------------------------------------------- 
  95. /// <Summary> 
  96. /// Computes the vernam-encryption/Decryption 
  97. /// </Summary> 
  98. /// <Param name = "inbytes"> </param> 
  99. /// <Param name = "keybytes"> </param> 
  100. /// <Param name = "outbytes"> </param> 
  101. Private VoidDovernam (Byte[] Inbytes,Byte[] Keybytes,Ref Byte[] Outbytes)
  102. {
  103. // Check arguments: 
  104. If(Inbytes. length! = Keybytes. Length) |
  105. (Keybytes. length! = Outbytes. Length ))
  106. Throw NewArgumentexception ("Byte-array are not of same length");
  107.  
  108. // Encrypt/decrypt by XOR: 
  109. For(IntI = 0; I <inbytes. length; I ++)
  110. Outbytes [I] = (Byte) (Inbytes [I] ^ keybytes [I]);
  111. }
  112. }

Example:

 
 
  1. ClassProgram
  2. {
  3. Static VoidMain (String[] ARGs)
  4. {
  5. Vernam =NewVernam ();
  6.  
  7. // Test with an image: 
  8. Vernam. encryptfile ("Image.gif","Image_encrypted.gif","Key01.dat");
  9. Vernam. decryptfile ("Image_encrypted.gif","Key01.dat","Image_decrypted.gif");
  10.  
  11. // Test with text file: 
  12. Vernam. encryptfile ("Text.txt","Text_encrypted.txt","Key02.dat");
  13. Vernam. decryptfile ("Text_encrypted.txt","Key02.dat","Text_decrypted.txt");
  14.  
  15. // Test with PDF file: 
  16. Vernam. encryptfile ("Textlayout","Text_encrypted.pdf","Key03.dat");
  17. Vernam. decryptfile ("Text_encrypted.pdf","Key03.dat","Text_decrypted.pdf");
  18. }
  19. }

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.