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:
-
- UsingSystem;
-
- UsingSystem. IO;
-
-
-
- Public ClassVernam
-
- {
- /// <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 VoidEncryptfile (StringOriginalfile,StringEncryptedfile,StringKeyfile)
-
- {
-
- // Read in the bytes from the original file:
-
- Byte[] Originalbytes;
- Using(Filestream FS =NewFilestream (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 =NewRandom ();
-
- Random. nextbytes (keybytes );
-
-
-
- // Write the key to the file:
-
- Using(Filestream FS =NewFilestream (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,RefEncryptedbytes );
-
-
-
- // Write the encrypted file:
-
- Using(Filestream FS =NewFilestream (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 VoidDecryptfile (StringEncryptedfile,StringKeyfile,StringDecryptedfile)
-
- {
-
- // Read in the encrypted Bytes:
-
- Byte[] Encryptedbytes;
- Using(Filestream FS =NewFilestream (encryptedfile, filemode. Open ))
-
- {
-
- Encryptedbytes =New Byte[Fs. Length];
-
- FS. Read (encryptedbytes, 0, encryptedbytes. Length );
-
- }
-
-
- // Read in the key:
-
- Byte[] Keybytes;
-
- Using(Filestream FS =NewFilestream (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,RefDecryptedbytes );
-
-
-
- // Write the decrypted file:
-
- Using(Filestream FS =NewFilestream (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 VoidDovernam (Byte[] Inbytes,Byte[] Keybytes,Ref Byte[] Outbytes)
-
- {
- // Check arguments:
-
- If(Inbytes. length! = Keybytes. Length) |
-
- (Keybytes. length! = Outbytes. Length ))
-
- Throw NewArgumentexception ("Byte-array are not of same length");
-
-
- // Encrypt/decrypt by XOR:
-
- For(IntI = 0; I <inbytes. length; I ++)
-
- Outbytes [I] = (Byte) (Inbytes [I] ^ keybytes [I]);
-
- }
-
- }
Example:
-
- ClassProgram
-
- {
- Static VoidMain (String[] ARGs)
-
- {
-
- Vernam =NewVernam ();
-
-
-
- // 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 ("Textlayout","Text_encrypted.pdf","Key03.dat");
-
- Vernam. decryptfile ("Text_encrypted.pdf","Key03.dat","Text_decrypted.pdf");
-
- }
-
- }