Using System;
Using System. Text;
Using System. Security;
Using System. Security. Cryptography;
Using System. IO;
Namespace EncryptClasses
{
/// <Summary>
/// DES encryption is defined here to facilitate future management and maintenance
/// Do not change the password at will, or you must
/// Keep in mind the previous password. Otherwise, the loss will be unpredictable.
/// </Summary>
Public class DESEncrypt
{
# Region "member fields"
Private string iv = "12345678 ";
Private string key = "12345678 ";
Private Encoding encoding = new UnicodeEncoding ();
Private DES des;
# Endregion
/// <Summary>
/// Constructor
/// </Summary>
Public DESEncrypt ()
{
Des = new DESCryptoServiceProvider ();
}
# Region "propertys"
/// <Summary>
/// Set the encryption key
/// </Summary>
Public string EncryptKey
{
Get {return this. key ;}
Set
{
This. key = value;
}
}
/// <Summary>
/// Encoding mode of characters to be encrypted
/// </Summary>
Public Encoding EncodingMode
{
Get {return this. encoding ;}
Set {this. encoding = value ;}
}
# Endregion
# Region "methods"
/// <Summary>
/// Encrypt the string and return the encrypted result
/// </Summary>
/// <Param name = "str"> </param>
/// <Returns> </returns>
Public string EncryptString (string str)
{
Byte [] ivb = Encoding. ASCII. GetBytes (this. iv );
Byte [] keyb = Encoding. ASCII. GetBytes (this. EncryptKey); // obtain the encryption key
Byte [] toEncrypt = this. EncodingMode. GetBytes (str); // obtain the content to be encrypted
Byte [] encrypted;
ICryptoTransform encryptor = des. CreateEncryptor (keyb, ivb );
MemoryStream msEncrypt = new MemoryStream ();
CryptoStream csEncrypt = new CryptoStream (msEncrypt, encryptor, CryptoStreamMode. Write );
CsEncrypt. Write (toEncrypt, 0, toEncrypt. Length );
CsEncrypt. FlushFinalBlock ();
Encrypted = msEncrypt. ToArray ();
CsEncrypt. Close ();
MsEncrypt. Close ();
Return this. EncodingMode. GetString (encrypted );
}
/// <Summary>
/// Encrypt the specified object. If the object is successfully encrypted, True is returned; otherwise, false is returned.
/// </Summary>
/// <Param name = "filePath"> file path to be encrypted </param>
/// <Param name = "outPath"> encrypted file output path </param>
Public void EncryptFile (string filePath, string outPath)
{
Bool isExist = File. Exists (filePath );
If (isExist) // if yes
{
Byte [] ivb = Encoding. ASCII. GetBytes (this. iv );
Byte [] keyb = Encoding. ASCII. GetBytes (this. EncryptKey );
// Obtain the byte stream of the file to be encrypted
FileStream fin = new FileStream (filePath, FileMode. Open, FileAccess. Read );
StreamReader reader = new StreamReader (fin, this. EncodingMode );
String dataStr = reader. ReadToEnd ();
Byte [] toEncrypt = this. EncodingMode. GetBytes (dataStr );
Fin. Close ();
FileStream fout = new FileStream (outPath, FileMode. Create, FileAccess. Write );
ICryptoTransform encryptor = des. CreateEncryptor (keyb, ivb );
CryptoStream csEncrypt = new CryptoStream (fout, encryptor, CryptoStreamMode. Write );
Try
{
// Encrypted file byte stream
CsEncrypt. Write (toEncrypt, 0, toEncrypt. Length );
CsEncrypt. FlushFinalBlock ();
}
Catch (Exception err)
{
Throw new ApplicationException (err. Message );
}
Finally
{
Try
{
Fout. Close ();
CsEncrypt. Close ();
}
Catch
{
;
}
}
}
Else
{
Throw new FileNotFoundException ("the specified file is not found ");
}
}
/// <Summary>
/// The overloaded version of the file encryption function. If the output path is not specified,
/// The original file will be overwritten by the encrypted file
/// </Summary>
/// <Param name = "filePath"> </param>
Public void EncryptFile (string filePath)
{
This. EncryptFile (filePath, filePath );
}
/// <Summary>
/// Decrypt the specified string
/// </Summary>
/// <Param name = "str"> characters to be decrypted </param>
/// <Returns> </returns>
Public string DecryptString (string str)
{
Byte [] ivb = Encoding. ASCII. GetBytes (this. iv );
Byte [] keyb = Encoding. ASCII. GetBytes (this. EncryptKey );
Byte [] toDecrypt = this. EncodingMode. GetBytes (str );
Byte [] deCrypted = new byte [toDecrypt. Length];
ICryptoTransform deCryptor = des. CreateDecryptor (keyb, ivb );
MemoryStream msDecrypt = new MemoryStream (toDecrypt );
CryptoStream csDecrypt = new CryptoStream (msDecrypt, deCryptor, CryptoStreamMode. Read );
Try
{
CsDecrypt. Read (deCrypted, 0, deCrypted. Length );
}
Catch (Exception err)
{
Throw new ApplicationException (err. Message );
}
Finally
{
Try
{
MsDecrypt. Close ();
CsDecrypt. Close ();
}
Catch {;}
}
Return this. EncodingMode. GetString (deCrypted );
}
/// <Summary>
/// Decrypt the specified object
/// </Summary>
/// <Param name = "filePath"> path of the file to be decrypted </param>
/// <Param name = "outPath"> decrypted file output path </param>
Public void DecryptFile (string filePath, string outPath)
{
Bool isExist = File. Exists (filePath );
If (isExist) // if yes
{
Byte [] ivb = Encoding. ASCII. GetBytes (this. iv );
Byte [] keyb = Encoding. ASCII. GetBytes (this. EncryptKey );
FileInfo file = new FileInfo (filePath );
Byte [] deCrypted = new byte;
// Obtain the byte stream of the file to be decrypted
FileStream fin = new FileStream (filePath, FileMode. Open, FileAccess. Read );
// Decrypt the file
Try
{
ICryptoTransform decryptor = des. CreateDecryptor (keyb, ivb );
CryptoStream csDecrypt = new CryptoStream (fin, decryptor, CryptoStreamMode. Read );
CsDecrypt. Read (deCrypted, 0, deCrypted. Length );
}
Catch (Exception err)
{
Throw new ApplicationException (err. Message );
}
Finally
{
Try
{
Fin. Close ();
}
Catch {;}
}
FileStream fout = new FileStream (outPath, FileMode. Create, FileAccess. Write );
Fout. Write (deCrypted, 0, deCrypted. Length );
Fout. Close ();
}
Else
{
Throw new FileNotFoundException ("the specified decryption file is not found ");
}
}
/// <Summary>
/// Reload version of the decrypted file. If the output path of the decrypted file is not provided,
/// The decrypted file will overwrite the previous file
/// </Summary>
/// <Param name = "filePath"> </param>
Public void DecryptFile (string filePath)
{
This. DecryptFile (filePath, filePath );
}
# Endregion
}
/// <Summary>
/// MD5 Encryption Class. Note that MD5-encrypted information cannot be converted back to the original data.
/// Do not use this encryption technology in user sensitive information, such as the user's password,
/// Use symmetric encryption whenever possible
/// </Summary>
Public class MD5Encrypt
{
Private MD5;
Public MD5Encrypt ()
{
Md5 = new MD5CryptoServiceProvider ();
}
/// <Summary>
/// Obtain the hash value from the string
/// </Summary>
/// <Param name = "str"> string to calculate the hash value </param>
/// <Returns> </returns>
Public string GetMD5FromString (string str)
{
Byte [] toCompute = Encoding. Unicode. GetBytes (str );
Byte [] hashed = md5.ComputeHash (toCompute, 0, toCompute. Length );
Return Encoding. ASCII. GetString (hashed );
}
/// <Summary>
/// Calculate the hash value based on the file
/// </Summary>
/// <Param name = "filePath"> path of the file to calculate the hash value </param>
/// <Returns> </returns>
Public string GetMD5FromFile (string filePath)
{
Bool isExist = File. Exists (filePath );
If (isExist) // if the object exists
{
FileStream stream = new FileStream (filePath, FileMode. Open, FileAccess. Read );
StreamReader reader = new StreamReader (stream, Encoding. Unicode );
String str = reader. ReadToEnd ();
Byte [] toHash = Encoding. Unicode. GetBytes (str );
Byte [] hashed = md5.ComputeHash (toHash, 0, toHash. Length );
Stream. Close ();
Return Encoding. ASCII. GetString (hashed );
}
Else // the file does not exist
{
Throw new FileNotFoundException ("the specified file is not found ");
}
}
}
/// <Summary>
/// Hash class for Digital Signature
/// </Summary>
Public class MACTripleDESEncrypt
{
Private MACTripleDES mact;
Private string _ key = "ksn168ch ";
Private byte [] _ data = null;
Public MACTripleDESEncrypt ()
{
Mact = new MACTripleDES ();
}
/// <Summary>
/// Obtain or set the key for Digital Signature
/// </Summary>
Public string Key
{
Get {return this. _ key ;}
Set
{
Int keyLength = value. Length;
Int [] keyAllowLengths = new int [] {8, 16, 24 };
Bool isRight = false;
Foreach (int I in keyAllowLengths)
{
If (keyLength = keyAllowLengths [I])
{
IsRight = true;
Break;
}
}
If (! IsRight)
Throw new ApplicationException ("the length of the key used for digital signature must be 8, 16, or 24 ");
Else
This. _ key = value;
}
}
/// <Summary>
/// Obtain or set user data for Digital Signature
/// </Summary>
Public byte [] Data
{
Get {return this. _ data ;}
Set {this. _ data = value ;}
}
/// <Summary>
/// Obtain the signed hash value
/// </Summary>
/// <Returns> </returns>
Public string GetHashValue ()
{
If (this. Data = null)
Throw new NotSetSpecialPropertyException ("no user set for Digital Signature" +
"Data (property: Data )");
Byte [] key = Encoding. ASCII. GetBytes (this. Key );
This. mact. Key = key;
Byte [] hash_ B = this. mact. ComputeHash (this. mact. ComputeHash (this. Data ));
Return Encoding. ASCII. GetString (hash_ B );
}
}
}