Method 1:
//Must add a reference
using ;
...
/// <summary>
/// SHA1 encryption string
/// </summary>
/// <param name="source">source string</param>
/// <returns> Encrypted string</returns>
public string SHA1(string source)
{
return (source, "SHA1");
}
/// <summary>
/// MD5 encrypted string
/// </summary>
/// <param name="source">source string</param>
/// <returns> Encrypted string</returns>
public string MD5(string source)
{
return (source, "MD5");;
}
Method 2 (reversible encryption and decryption):
using ;
...
public string Encode(string data)
{
byte[] byKey = (KEY_64);
byte[] byIV = (IV_64);
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
int i = ;
MemoryStream ms = new MemoryStream();
CryptoStream cst = new CryptoStream(ms, (byKey, byIV), );
StreamWriter sw = new StreamWriter(cst);
(data);
();
();
();
return Convert.ToBase64String((), 0, (int));
}
public string Decode(string data)
{
byte[] byKey = (KEY_64);
byte[] byIV = (IV_64);
byte[] byEnc;
try
{
byEnc = Convert.FromBase64String(data);
}
catch
{
return null;
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(byEnc);
CryptoStream cst = new CryptoStream(ms, (byKey, byIV), );
StreamReader sr = new StreamReader(cst);
return ();
}
Method 3 (MD5 irreversible):
using ;
...
//MD5 irreversible encryption
//32-bit encryption
public string GetMD5_32(string s, string _input_charset)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] t = ((_input_charset).GetBytes(s));
StringBuilder sb = new StringBuilder(32);
for (int i = 0; i < ; i++)
{
(t[i].ToString("x").PadLeft(2, '0'));
}
return ();
}
//16-bit encryption
public static string GetMd5_16(string ConvertString)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string t2 = (((ConvertString)), 4, 8);
t2 = ("-", "");
return t2;
}
Method 4 (Symmetric encryption):
using ;
using ;
...
private SymmetricAlgorithm mobjCryptoService;
private string Key;
/// <summary>
/// Constructor of symmetric encryption class
/// </summary>
public SymmetricMethod()
{
mobjCryptoService = new RijndaelManaged();
Key = "Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7";
}
/// <summary>
/// Obtain the key
/// </summary>
/// <returns>Key</returns>
private byte[] GetLegalKey()
{
string sTemp = Key;
();
byte[] bytTemp = ;
int KeyLength = ;
if ( > KeyLength)
sTemp = (0, KeyLength);
else if ( < KeyLength)
sTemp = (KeyLength, ' ');
return (sTemp);
}
/// <summary>
/// Obtain the initial vector IV
/// </summary>
/// <returns>First trial vector IV</returns>
private byte[] GetLegalIV()
{
string sTemp = "E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk";
();
byte[] bytTemp = ;
int IVLength = ;
if ( > IVLength)
sTemp = (0, IVLength);
else if ( < IVLength)
sTemp = (IVLength, ' ');
return (sTemp);
}
/// <summary>
/// Encryption method
/// </summary>
/// <param name="Source">String to be encrypted</param>
/// <returns>Encrypted string</returns>
public string Encrypto(string Source)
{
byte[] bytIn = UTF8Encoding.(Source);
MemoryStream ms = new MemoryStream();
= GetLegalKey();
= GetLegalIV();
ICryptoTransform encrypto = ();
CryptoStream cs = new CryptoStream(ms, encrypto, );
(bytIn, 0, );
();
();
byte[] bytOut = ();
return Convert.ToBase64String(bytOut);
}
/// <summary>
/// Decryption method
/// </summary>
/// <param name="Source">Serial to be decrypted</param>
/// <returns>Decrypted string</returns>
public string Decrypto(string Source)
{
byte[] bytIn = Convert.FromBase64String(Source);
MemoryStream ms = new MemoryStream(bytIn, 0, );
= GetLegalKey();
= GetLegalIV();
ICryptoTransform encrypto = ();
CryptoStream cs = new CryptoStream(ms, encrypto, );
StreamReader sr = new StreamReader(cs);
return ();
}
Method 5:
using ;
using ;
using ;
...
//Default key vector
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
/// <summary>
/// DES encryption string
/// </summary>
/// <param name="encryptString">Stand to be encrypted</param>
/// <param name="encryptKey">Encrypt key, required to be 8 bits</param>
/// <returns> Encryption successfully returns the encrypted string, and fails to return the source string</returns>
public static string EncryptDES(string encryptString, string encryptKey)
{
try
{
byte[] rgbKey = Encoding.((0, 8));
byte[] rgbIV = Keys;
byte[] inputByteArray = Encoding.(encryptString);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, (rgbKey, rgbIV), );
(inputByteArray, 0, );
();
return Convert.ToBase64String(());
}
catch
{
return encryptString;
}
}
/// <summary>
/// DES decryption string
/// </summary>
/// <param name="decryptString">Stand to be decrypted</param>
/// <param name="decryptKey">The decrypt key is required to be 8 bits, the same as the encryption key</param>
/// <returns>The decryption successfully returns the decrypted string, and the source string is returned to the failed</returns>
public static string DecryptDES(string decryptString, string decryptKey)
{
try
{
byte[] rgbKey = Encoding.(decryptKey);
byte[] rgbIV = Keys;
byte[] inputByteArray = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, (rgbKey, rgbIV), );
(inputByteArray, 0, );
();
return Encoding.(());
}
catch
{
return decryptString;
}
}
Method 6 (file encryption):
using ;
using ;
using ;
...
//Encrypted file
private static void EncryptData(String inName, String outName, byte[] desKey, byte[] desIV)
{
//Create the file streams to handle the input and output files.
FileStream fin = new FileStream(inName, , );
FileStream fout = new FileStream(outName, , );
(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 = ; //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, (desKey, desIV), );
//Read from the input file, then encrypt and write to the output file.
while (rdlen < totlen)
{
len = (bin, 0, 100);
(bin, 0, len);
rdlen = rdlen + len;
}
();
();
();
}
//Decrypt the file
private static void DecryptData(String inName, String outName, byte[] desKey, byte[] desIV)
{
//Create the file streams to handle the input and output files.
FileStream fin = new FileStream(inName, , );
FileStream fout = new FileStream(outName, , );
(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 = ; //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, (desKey, desIV), );
//Read from the input file, then encrypt and write to the output file.
while (rdlen < totlen)
{
len = (bin, 0, 100);
(bin, 0, len);
rdlen = rdlen + len;
}
();
();
();
}
using System;
using ;//This is the prerequisite for handling text encoding
using ;
using ;
/// <summary>
/// DES encryption method
/// </summary>
/// <param name="strPlain">Plaintext</param>
/// <param name="strDESKey">key</param>
/// <param name="strDESIV">Vector</param>
/// <returns>ciphertext</returns>
public string DESEncrypt(string strPlain,string strDESKey,string strDESIV)
{
//Convert key to byte array
byte[] bytesDESKey=(strDESKey);
//Convert vectors into byte arrays
byte[] bytesDESIV=(strDESIV);
//Declare 1 new DES object
DESCryptoServiceProvider desEncrypt=new DESCryptoServiceProvider();
//Open a memory stream
MemoryStream msEncrypt=new MemoryStream();
//Package the memory stream object into an encrypted stream object
CryptoStream csEncrypt=new CryptoStream(msEncrypt,(bytesDESKey,bytesDESIV),);
//Package the encrypted stream object into a write stream object
StreamWriter swEncrypt=new StreamWriter(csEncrypt);
//Write stream object to write plain text
(strPlain);
//Write stream closes
();
//Encrypted stream is closed
();
//Convert the memory stream into a byte array, and the memory stream is now ciphertext
byte[] bytesCipher=();
//Memory stream is closed
();
//Convert ciphertext byte array to string and return
return (bytesCipher);
}
/// <summary>
/// DES decryption method
/// </summary>
/// <param name="strCipher">ciphertext</param>
/// <param name="strDESKey">key</param>
/// <param name="strDESIV">Vector</param>
/// <returns>Plain text</returns>
public string DESDecrypt(string strCipher,string strDESKey,string strDESIV)
{
//Convert key to byte array
byte[] bytesDESKey=(strDESKey);
//Convert vectors into byte arrays
byte[] bytesDESIV=(strDESIV);
//Convert ciphertext to byte array
byte[] bytesCipher=(strCipher);
//Declare 1 new DES object
DESCryptoServiceProvider desDecrypt=new DESCryptoServiceProvider();
//Open a memory stream and store a cryptographic byte array
MemoryStream msDecrypt=new MemoryStream(bytesCipher);
//Package the memory stream object into a decrypted stream object
CryptoStream csDecrypt=new CryptoStream(msDecrypt,(bytesDESKey,bytesDESIV),);
//Package the decrypted stream object into a read stream object
StreamReader srDecrypt=new StreamReader(csDecrypt);
//Plain text = read content of read stream
string strPlainText=();
//Read stream is closed
();
//Decryption stream is closed
();
//Memory stream is closed
();
//Return to the plain text
return strPlainText;
}
Copy the codeThe code is as follows:
//Must add a reference
using ;
...
/// <summary>
/// SHA1 encryption string
/// </summary>
/// <param name="source">source string</param>
/// <returns> Encrypted string</returns>
public string SHA1(string source)
{
return (source, "SHA1");
}
/// <summary>
/// MD5 encrypted string
/// </summary>
/// <param name="source">source string</param>
/// <returns> Encrypted string</returns>
public string MD5(string source)
{
return (source, "MD5");;
}
Method 2 (reversible encryption and decryption):
Copy the codeThe code is as follows:
using ;
...
public string Encode(string data)
{
byte[] byKey = (KEY_64);
byte[] byIV = (IV_64);
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
int i = ;
MemoryStream ms = new MemoryStream();
CryptoStream cst = new CryptoStream(ms, (byKey, byIV), );
StreamWriter sw = new StreamWriter(cst);
(data);
();
();
();
return Convert.ToBase64String((), 0, (int));
}
public string Decode(string data)
{
byte[] byKey = (KEY_64);
byte[] byIV = (IV_64);
byte[] byEnc;
try
{
byEnc = Convert.FromBase64String(data);
}
catch
{
return null;
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(byEnc);
CryptoStream cst = new CryptoStream(ms, (byKey, byIV), );
StreamReader sr = new StreamReader(cst);
return ();
}
Method 3 (MD5 irreversible):
Copy the codeThe code is as follows:
using ;
...
//MD5 irreversible encryption
//32-bit encryption
public string GetMD5_32(string s, string _input_charset)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] t = ((_input_charset).GetBytes(s));
StringBuilder sb = new StringBuilder(32);
for (int i = 0; i < ; i++)
{
(t[i].ToString("x").PadLeft(2, '0'));
}
return ();
}
//16-bit encryption
public static string GetMd5_16(string ConvertString)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string t2 = (((ConvertString)), 4, 8);
t2 = ("-", "");
return t2;
}
Method 4 (Symmetric encryption):
Copy the codeThe code is as follows:
using ;
using ;
...
private SymmetricAlgorithm mobjCryptoService;
private string Key;
/// <summary>
/// Constructor of symmetric encryption class
/// </summary>
public SymmetricMethod()
{
mobjCryptoService = new RijndaelManaged();
Key = "Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7";
}
/// <summary>
/// Obtain the key
/// </summary>
/// <returns>Key</returns>
private byte[] GetLegalKey()
{
string sTemp = Key;
();
byte[] bytTemp = ;
int KeyLength = ;
if ( > KeyLength)
sTemp = (0, KeyLength);
else if ( < KeyLength)
sTemp = (KeyLength, ' ');
return (sTemp);
}
/// <summary>
/// Obtain the initial vector IV
/// </summary>
/// <returns>First trial vector IV</returns>
private byte[] GetLegalIV()
{
string sTemp = "E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk";
();
byte[] bytTemp = ;
int IVLength = ;
if ( > IVLength)
sTemp = (0, IVLength);
else if ( < IVLength)
sTemp = (IVLength, ' ');
return (sTemp);
}
/// <summary>
/// Encryption method
/// </summary>
/// <param name="Source">String to be encrypted</param>
/// <returns>Encrypted string</returns>
public string Encrypto(string Source)
{
byte[] bytIn = UTF8Encoding.(Source);
MemoryStream ms = new MemoryStream();
= GetLegalKey();
= GetLegalIV();
ICryptoTransform encrypto = ();
CryptoStream cs = new CryptoStream(ms, encrypto, );
(bytIn, 0, );
();
();
byte[] bytOut = ();
return Convert.ToBase64String(bytOut);
}
/// <summary>
/// Decryption method
/// </summary>
/// <param name="Source">Serial to be decrypted</param>
/// <returns>Decrypted string</returns>
public string Decrypto(string Source)
{
byte[] bytIn = Convert.FromBase64String(Source);
MemoryStream ms = new MemoryStream(bytIn, 0, );
= GetLegalKey();
= GetLegalIV();
ICryptoTransform encrypto = ();
CryptoStream cs = new CryptoStream(ms, encrypto, );
StreamReader sr = new StreamReader(cs);
return ();
}
Method 5:
Copy the codeThe code is as follows:
using ;
using ;
using ;
...
//Default key vector
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
/// <summary>
/// DES encryption string
/// </summary>
/// <param name="encryptString">Stand to be encrypted</param>
/// <param name="encryptKey">Encrypt key, required to be 8 bits</param>
/// <returns> Encryption successfully returns the encrypted string, and fails to return the source string</returns>
public static string EncryptDES(string encryptString, string encryptKey)
{
try
{
byte[] rgbKey = Encoding.((0, 8));
byte[] rgbIV = Keys;
byte[] inputByteArray = Encoding.(encryptString);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, (rgbKey, rgbIV), );
(inputByteArray, 0, );
();
return Convert.ToBase64String(());
}
catch
{
return encryptString;
}
}
/// <summary>
/// DES decryption string
/// </summary>
/// <param name="decryptString">Stand to be decrypted</param>
/// <param name="decryptKey">The decrypt key is required to be 8 bits, the same as the encryption key</param>
/// <returns>The decryption successfully returns the decrypted string, and the source string is returned to the failed</returns>
public static string DecryptDES(string decryptString, string decryptKey)
{
try
{
byte[] rgbKey = Encoding.(decryptKey);
byte[] rgbIV = Keys;
byte[] inputByteArray = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, (rgbKey, rgbIV), );
(inputByteArray, 0, );
();
return Encoding.(());
}
catch
{
return decryptString;
}
}
Method 6 (file encryption):
Copy the codeThe code is as follows:
using ;
using ;
using ;
...
//Encrypted file
private static void EncryptData(String inName, String outName, byte[] desKey, byte[] desIV)
{
//Create the file streams to handle the input and output files.
FileStream fin = new FileStream(inName, , );
FileStream fout = new FileStream(outName, , );
(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 = ; //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, (desKey, desIV), );
//Read from the input file, then encrypt and write to the output file.
while (rdlen < totlen)
{
len = (bin, 0, 100);
(bin, 0, len);
rdlen = rdlen + len;
}
();
();
();
}
//Decrypt the file
private static void DecryptData(String inName, String outName, byte[] desKey, byte[] desIV)
{
//Create the file streams to handle the input and output files.
FileStream fin = new FileStream(inName, , );
FileStream fout = new FileStream(outName, , );
(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 = ; //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, (desKey, desIV), );
//Read from the input file, then encrypt and write to the output file.
while (rdlen < totlen)
{
len = (bin, 0, 100);
(bin, 0, len);
rdlen = rdlen + len;
}
();
();
();
}
Copy the codeThe code is as follows:
using System;
using ;//This is the prerequisite for handling text encoding
using ;
using ;
/// <summary>
/// DES encryption method
/// </summary>
/// <param name="strPlain">Plaintext</param>
/// <param name="strDESKey">key</param>
/// <param name="strDESIV">Vector</param>
/// <returns>ciphertext</returns>
public string DESEncrypt(string strPlain,string strDESKey,string strDESIV)
{
//Convert key to byte array
byte[] bytesDESKey=(strDESKey);
//Convert vectors into byte arrays
byte[] bytesDESIV=(strDESIV);
//Declare 1 new DES object
DESCryptoServiceProvider desEncrypt=new DESCryptoServiceProvider();
//Open a memory stream
MemoryStream msEncrypt=new MemoryStream();
//Package the memory stream object into an encrypted stream object
CryptoStream csEncrypt=new CryptoStream(msEncrypt,(bytesDESKey,bytesDESIV),);
//Package the encrypted stream object into a write stream object
StreamWriter swEncrypt=new StreamWriter(csEncrypt);
//Write stream object to write plain text
(strPlain);
//Write stream closes
();
//Encrypted stream is closed
();
//Convert the memory stream into a byte array, and the memory stream is now ciphertext
byte[] bytesCipher=();
//Memory stream is closed
();
//Convert ciphertext byte array to string and return
return (bytesCipher);
}
/// <summary>
/// DES decryption method
/// </summary>
/// <param name="strCipher">ciphertext</param>
/// <param name="strDESKey">key</param>
/// <param name="strDESIV">Vector</param>
/// <returns>Plain text</returns>
public string DESDecrypt(string strCipher,string strDESKey,string strDESIV)
{
//Convert key to byte array
byte[] bytesDESKey=(strDESKey);
//Convert vectors into byte arrays
byte[] bytesDESIV=(strDESIV);
//Convert ciphertext to byte array
byte[] bytesCipher=(strCipher);
//Declare 1 new DES object
DESCryptoServiceProvider desDecrypt=new DESCryptoServiceProvider();
//Open a memory stream and store a cryptographic byte array
MemoryStream msDecrypt=new MemoryStream(bytesCipher);
//Package the memory stream object into a decrypted stream object
CryptoStream csDecrypt=new CryptoStream(msDecrypt,(bytesDESKey,bytesDESIV),);
//Package the decrypted stream object into a read stream object
StreamReader srDecrypt=new StreamReader(csDecrypt);
//Plain text = read content of read stream
string strPlainText=();
//Read stream is closed
();
//Decryption stream is closed
();
//Memory stream is closed
();
//Return to the plain text
return strPlainText;
}