DebugLZQ searches for related file encryption programs on the Internet, and finds that the given programs are basically encrypted and decrypted for "character creation" and "text. There are few encryption and decryption procedures for video files, images, and other general files. Therefore, write this article to implement a small tool for encrypting general files.
The main function of the program is: select the file to be encrypted through the file selection box-> enter the password for encryption; select the encrypted file and enter the password for decryption.
The main interface of the program is as follows:
The Click event handler for the three buttons is as follows:Copy codeThe Code is as follows: private void btnSelectFile_Click (object sender, EventArgs e)
{
If (openFileDialog1.ShowDialog () = System. Windows. Forms. DialogResult. OK)
{
TxtFileName. Text = openFileDialog1.FileName;
}
}
Private void btnEncryptFile_Click (object sender, EventArgs e)
{
String inFile = txtFileName. Text;
String outFile = inFile + ". dat ";
String password = txtPassword. Text;
DESFile. DESFileClass. EncryptFile (inFile, outFile, password); // encrypt the file
// Delete the encrypted file
File. Delete (inFile );
TxtFileName. Text = string. Empty;
MessageBox. Show ("encrypted ");
}
Private void btnDecryptFile_Click (object sender, EventArgs e)
{
String inFile = txtFileName. Text;
String outFile = inFile. Substring (0, inFile. Length-4 );
String password = txtPassword. Text;
DESFile. DESFileClass. DecryptFile (inFile, outFile, password); // decrypt the file
// Delete the file before decryption
File. Delete (inFile );
TxtFileName. Text = string. Empty;
MessageBox. Show ("decrypted ");
}
The source code of the Help file for encryption and decryption is as follows:Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Security. Cryptography;
Using System. IO;
Namespace DESFile
{
/// <Summary>
/// Exception handling class
/// </Summary>
Public class CryptoHelpException: ApplicationException
{
Public CryptoHelpException (string msg): base (msg ){}
}
/// <Summary>
/// CryptHelp
/// </Summary>
Public class DESFileClass
{
Private const ulong FC_TAG = 0xFC010203040506CF;
Private const int BUFFER_SIZE = 128*1024;
/// <Summary>
/// Check whether the two Byte arrays are the same
/// </Summary>
/// <Param name = "b1"> Byte array </param>
/// <Param name = "b2"> Byte array </param>
/// <Returns> true-equal </returns>
Private static bool CheckByteArrays (byte [] b1, byte [] b2)
{
If (b1.Length = b2.Length)
{
For (int I = 0; I <b1.Length; ++ I)
{
If (b1 [I]! = B2 [I])
Return false;
}
Return true;
}
Return false;
}
/// <Summary>
/// Create DebugLZQ, http://www.cnblogs.com/DebugLZQ
/// </Summary>
/// <Param name = "password"> password </param>
/// <Param name = "salt"> </param>
/// <Returns> encrypted object </returns>
Private static response ricalgorithm CreateRijndael (string password, byte [] salt)
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes (password, salt, "SHA256", 1000 );
Repeated ricalgorithm sma = Rijndael. Create ();
Sma. Key size = 256;
Sma. Key = pdb. GetBytes (32 );
Sma. Padding = PaddingMode. PKCS7;
Return sma;
}
/// <Summary>
/// Generate random number of encrypted files
/// </Summary>
Private static RandomNumberGenerator rand = new RNGCryptoServiceProvider ();
/// <Summary>
/// Generate a random Byte array of the specified length
/// </Summary>
/// <Param name = "count"> Byte array length </param>
/// <Returns> random Byte array </returns>
Private static byte [] GenerateRandomBytes (int count)
{
Byte [] bytes = new byte [count];
Rand. GetBytes (bytes );
Return bytes;
}
/// <Summary>
/// Encrypt the file
/// </Summary>
/// <Param name = "inFile"> file to be encrypted </param>
/// <Param name = "outFile"> encrypted input file </param>
/// <Param name = "password"> encrypt the password </param>
Public static void EncryptFile (string inFile, string outFile, string password)
{
Using (FileStream fin = File. OpenRead (inFile ),
Fout = File. OpenWrite (outFile ))
{
Long lSize = fin. Length; // Length of the input file
Int size = (int) lSize;
Byte [] bytes = new byte [BUFFER_SIZE]; // Cache
Int read =-1; // number of input files read
Int value = 0;
// Obtain IV and salt
Byte [] IV = GenerateRandomBytes (16 );
Byte [] salt = GenerateRandomBytes (16 );
// Create an encrypted object
Repeated ricalgorithm sma = DESFileClass. CreateRijndael (password, salt );
Sma. IV = IV;
// Write IV and salt at the beginning of the output file
Fout. Write (IV, 0, IV. Length );
Fout. Write (salt, 0, salt. Length );
// Create hash Encryption
HashAlgorithm hasher = SHA256.Create ();
Using (CryptoStream cout = new CryptoStream (fout, sma. CreateEncryptor (), CryptoStreamMode. Write ),
Chash = new CryptoStream (Stream. Null, hasher, CryptoStreamMode. Write ))
{
BinaryWriter bw = new BinaryWriter (cout );
Bw. Write (lSize );
Bw. Write (FC_TAG );
// Read/write bytes to the encrypted Stream Buffer
While (read = fin. Read (bytes, 0, bytes. Length ))! = 0)
{
Cout. Write (bytes, 0, read );
Chash. Write (bytes, 0, read );
Value + = read;
}
// Disable the encrypted stream
Chash. Flush ();
Chash. Close ();
// Read the hash
Byte [] hash = hasher. Hash;
// Input file write hash
Cout. Write (hash, 0, hash. Length );
// Close the file stream
Cout. Flush ();
Cout. Close ();
}
}
}
/// <Summary>
/// Decrypt the file
/// </Summary>
/// <Param name = "inFile"> files to be decrypted </param>
/// <Param name = "outFile"> decrypted output file </param>
/// <Param name = "password"> decrypt the password </param>
Public static void DecryptFile (string inFile, string outFile, string password)
{
// Create an open file stream
Using (FileStream fin = File. OpenRead (inFile ),
Fout = File. OpenWrite (outFile ))
{
Int size = (int) fin. Length;
Byte [] bytes = new byte [BUFFER_SIZE];
Int read =-1;
Int value = 0;
Int outValue = 0;
Byte [] IV = new byte [16];
Fin. Read (IV, 0, 16 );
Byte [] salt = new byte [16];
Fin. Read (salt, 0, 16 );
Repeated ricalgorithm sma = DESFileClass. CreateRijndael (password, salt );
Sma. IV = IV;
Value = 32;
Long lSize =-1;
// Create a hash object and verify the file
HashAlgorithm hasher = SHA256.Create ();
Using (CryptoStream cin = new CryptoStream (fin, sma. CreateDecryptor (), CryptoStreamMode. Read ),
Chash = new CryptoStream (Stream. Null, hasher, CryptoStreamMode. Write ))
{
// Read the object Length
BinaryReader br = new BinaryReader (cin );
LSize = br. ReadInt64 ();
Ulong tag = br. ReadUInt64 ();
If (FC_TAG! = Tag)
Throw new CryptoHelpException ("file damaged ");
Long numReads = lSize/BUFFER_SIZE;
Long slack = (long) lSize % BUFFER_SIZE;
For (int I = 0; I <numReads; ++ I)
{
Read = cin. Read (bytes, 0, bytes. Length );
Fout. Write (bytes, 0, read );
Chash. Write (bytes, 0, read );
Value + = read;
OutValue + = read;
}
If (slack> 0)
{
Read = cin. Read (bytes, 0, (int) slack );
Fout. Write (bytes, 0, read );
Chash. Write (bytes, 0, read );
Value + = read;
OutValue + = read;
}
Chash. Flush ();
Chash. Close ();
Fout. Flush ();
Fout. Close ();
Byte [] curHash = hasher. Hash;
// Obtain the comparison and old hash objects
Byte [] oldHash = new byte [hasher. HashSize/8];
Read = cin. Read (oldHash, 0, oldHash. Length );
If (oldHash. Length! = Read) | (! CheckByteArrays (oldHash, curHash )))
Throw new CryptoHelpException ("file damaged ");
}
If (outValue! = LSize)
Throw new CryptoHelpException ("file size mismatch ");
}
}
}
}
Encryption/Decryption results:
Taking 1. avi on an encrypted D disk as an example, if the encrypted file is 1. avi. dat, the file cannot be opened even if it is renamed back to 1. avi (the file is encrypted ).
After the password is entered for decryption, the file will be decrypted and can be opened smoothly ~