This class is summarized by referring to several documents on the Internet. It can be used directly after tests. There is a section of MD5 which should be independent of one class. I am lazy, so I wrote it into a file during the test. It still feels practical. If there is any confidential file, I will use it to handle it. In the future, I will handle it in turn, it's just that you should not forget the password. If you are as lazy as me, you can directly copy the following code and use it directly.
Program code: using System;
Using System. IO;
Using System. Text;
Using System. Security. Cryptography;
Using System. Web;
Namespace Test. Com
{
/// <Summary>
/// DESEncryptor abstract description.
/// </Summary>
Public class DESEncryptor
{
# Region private member
/// <Summary>
/// Input string
/// </Summary>
Private string inputString = null;
/// <Summary>
/// Output string
/// </Summary>
Private string outString = null;
/// <Summary>
/// Input file path
/// </Summary>
Private string inputFilePath = null;
/// <Summary>
/// Output file path
/// </Summary>
Private string outFilePath = null;
/// <Summary>
/// Encryption key
/// </Summary>
Private string encryptKey = null;
/// <Summary>
/// Decrypt the key
/// </Summary>
Private string decryptKey = null;
/// <Summary>
/// Prompt message
/// </Summary>
Private string noteMessage = null;
# Endregion
# Region Public attributes
/// <Summary>
/// Input string
/// </Summary>
Public string InputString
{
Get {return inputString ;}
Set {inputString = value ;}
}
/// <Summary>
/// Output string
/// </Summary>
Public string OutString
{
Get {return outString ;}
Set {outString = value ;}
}
/// <Summary>
/// Input file path
/// </Summary>
Public string InputFilePath
{
Get {return inputFilePath ;}
Set {inputFilePath = value ;}
}
/// <Summary>
/// Output file path
/// </Summary>
Public string OutFilePath
{
Get {return outFilePath ;}
Set {outFilePath = value ;}
}
/// <Summary>
/// Encryption key
/// </Summary>
Public string EncryptKey
{
Get {return encryptKey ;}
Set {encryptKey = value ;}
}
/// <Summary>
/// Decrypt the key
/// </Summary>
Public string DecryptKey
{
Get {return decryptKey ;}
Set {decryptKey = value ;}
}
/// <Summary>
/// Error message
/// </Summary>
Public string NoteMessage
{
Get {return noteMessage ;}
Set {noteMessage = value ;}
}
# Endregion
# Region Constructor
Public DESEncryptor ()
{
//
// TODO: add the constructor logic here
//
}
# Endregion
# Region DES encrypted string
/// <Summary>
/// Encrypted string
/// Note: The key must be 8 bits.
/// </Summary>
/// <Param name = "strText"> string </param>
/// <Param name = "encryptKey"> key </param>
Public void DesEncrypt ()
{
Byte [] byKey = null;
Byte [] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
Try
{
ByKey = System. Text. Encoding. UTF8.GetBytes (this. encryptKey. Substring (0, 8 ));
DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
Byte [] inputByteArray = Encoding. UTF8.GetBytes (this. inputString );
MemoryStream MS = new MemoryStream ();
CryptoStream cs = new CryptoStream (MS, des. CreateEncryptor (byKey, IV), CryptoStreamMode. Write );
Cs. Write (inputByteArray, 0, inputByteArray. Length );
Cs. FlushFinalBlock ();
This. outString = Convert. ToBase64String (ms. ToArray ());
}
Catch (System. Exception error)
{
This. noteMessage = error. Message;
}
}
# Endregion
# Region DES decryption string
/// <Summary>
/// Decrypt the string
/// </Summary>
/// <Param name = "this. inputString"> encrypted string </param>
/// <Param name = "decryptKey"> key </param>
Public void DesDecrypt ()
{
Byte [] byKey = null;
Byte [] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
Byte [] inputByteArray = new Byte [this. inputString. Length];
Try
{
ByKey = System. Text. Encoding. UTF8.GetBytes (decryptKey. Substring (0, 8 ));
DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
InputByteArray = Convert. FromBase64String (this. inputString );
MemoryStream MS = new MemoryStream ();
CryptoStream cs = new CryptoStream (MS, des. CreateDecryptor (byKey, IV), CryptoStreamMode. Write );
Cs. Write (inputByteArray, 0, inputByteArray. Length );
Cs. FlushFinalBlock ();
System. Text. Encoding encoding = new System. Text. UTF8Encoding ();
This. outString = encoding. GetString (ms. ToArray ());
}
Catch (System. Exception error)
{
This. noteMessage = error. Message;
}
}
# Endregion
# Region DES encrypted file
/// <Summary>
/// DES encrypted file
/// </Summary>
/// <Param name = "this. inputFilePath"> source file path </param>
/// <Param name = "this. outFilePath"> output file path </param>
/// <Param name = "encryptKey"> key </param>
Public void FileDesEncrypt ()
{
Byte [] byKey = null;
Byte [] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
Try
{
ByKey = System. Text. Encoding. UTF8.GetBytes (this. encryptKey. Substring (0, 8 ));
FileStream fin = new FileStream (this. inputFilePath, FileMode. Open, FileAccess. Read );
FileStream fout = new FileStream (this. outFilePath, FileMode. OpenOrCreate, FileAccess. Write );
Fout. SetLength (0 );
// Create variables to help with read and write.
Byte [] bin = new byte [100]; // This is intermediate storage for the encryption.
Long rdlen = 0; // This is the total number of bytes written.
Long totlen = fin. Length; // This is the total length of the input file.
Int len; // This is the number of bytes to be written at a time.
DES des = new DESCryptoServiceProvider ();
CryptoStream encStream = new CryptoStream (fout, des. CreateEncryptor (byKey, IV), CryptoStreamMode. Write );
// Read from the input file, then encrypt and write to the output file.
While (rdlen <totlen)
{
Len = fin. Read (bin, 0,100 );
EncStream. Write (bin, 0, len );
Rdlen = rdlen + len;
}
EncStream. Close ();
Fout. Close ();
Fin. Close ();
}
Catch (System. Exception error)
{
This. noteMessage = error. Message. ToString ();
}
}
# Endregion
# Region DES file decryption
/// <Summary>
/// Decrypt the file
/// </Summary>
/// <Param name = "this. inputFilePath"> encrypted file path </param>
/// <Param name = "this. outFilePath"> output file path </param>
/// <Param name = "decryptKey"> key </param>
Public void FileDesDecrypt ()
{
Byte [] byKey = null;
Byte [] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
Try
{
ByKey = System. Text. Encoding. UTF8.GetBytes (decryptKey. Substring (0, 8 ));
FileStream fin = new FileStream (this. inputFilePath, FileMode. Open, FileAccess. Read );
FileStream fout = new FileStream (this. outFilePath, FileMode. OpenOrCreate, FileAccess. Write );
Fout. SetLength (0 );
// Create variables to help with read and write.
Byte [] bin = new byte [100]; // This is intermediate storage for the encryption.
Long rdlen = 0; // This is the total number of bytes written.
Long totlen = fin. Length; // This is the total length of the input file.
Int len; // This is the number of bytes to be written at a time.
DES des = new DESCryptoServiceProvider ();
CryptoStream encStream = new CryptoStream (fout, des. CreateDecryptor (byKey, IV), CryptoStreamMode. Write );
// Read from the input file, then encrypt and write to the output file.
While (rdlen <totlen)
{
Len = fin. Read (bin, 0,100 );
EncStream. Write (bin, 0, len );
Rdlen = rdlen + len;
}
EncStream. Close ();
Fout. Close ();
Fin. Close ();
}
Catch (System. Exception error)
{
This. noteMessage = error. Message. ToString ();
}
}
# Endregion
# Region MD5
/// <Summary>
/// MD5 Encrypt
/// </Summary>
/// <Param name = "strText"> text </param>
/// <Returns> md5 Encrypt string </returns>
Public void MD5Encrypt ()
{
MD5 md5 = new MD5CryptoServiceProvider ();
Byte [] result = md5.ComputeHash (System. Text. Encoding. Default. GetBytes (this. inputString ));
This. outString = System. Text. Encoding. Default. GetString (result );
}
# Endregion
}
}