Class Deshelper
{
<summary>
Des encryption method
</summary>
<param name= "Prestr" > Strings to be encrypted </param>
<param name= "key" > Encrypted password (only 8 bits long) </param>
<param name= "Encoding" > Encoding method (default = UTF-8) </param>
<returns> Encryption Results </returns>
public static string Encrypt (string prestr, String key, Encoding Encoding = null)
{
encoding = encoding?? Encoding.GetEncoding ("UTF-8");
Note the length of IV must be the same as the length of the password in key
var IV = Encoding.ASCII.GetBytes (key);
var Keyarray = Encoding.ASCII.GetBytes (key);
var datas = encoding. GetBytes (PRESTR);
var descryptoserviceprovider = new DESCryptoServiceProvider ();
using (var MemoryStream = new MemoryStream ())
{
using (var CryptoStream = new CryptoStream (MemoryStream, Descryptoserviceprovider.createencryptor (iv, KeyArray), CryptoStreamMode.Write))
{
Cryptostream.write (datas, 0, Datas. Length);
Cryptostream.flushfinalblock ();
StringBuilder ret = new StringBuilder ();
foreach (Byte b in Memorystream.toarray ())
{
Ret. AppendFormat ("{0:x2}", b);//Hex Code
}
return ret. ToString ();
}
}
}
<summary>
DES Decryption method
</summary>
<param name= "Prestr" > Strings to be encrypted </param>
<param name= "key" > Encrypted password (only 8 bits long) </param>
<param name= "Encoding" > Encoding method (default = UTF-8) </param>
<returns> Results of Encryption </returns>
public static string Decrypt (string prestr, String key, Encoding Encoding = null)
{
encoding = encoding?? Encoding.GetEncoding ("UTF-8");
var IV = Encoding.ASCII.GetBytes (key);
var Keyarray = Encoding.ASCII.GetBytes (key);
Put the string in a byte array
byte[] datas = new Byte[prestr. LENGTH/2];
for (int x = 0; x < prestr. LENGTH/2; X + +)
{
int i = (Convert.ToInt32 (PRESTR). Substring (x * 2, 2), 16));
DATAS[X] = (byte) i;
}
var descryptoserviceprovider = new DESCryptoServiceProvider ();
using (var MemoryStream = new MemoryStream ())
{
using (var CryptoStream = new CryptoStream (MemoryStream, Descryptoserviceprovider.createdecryptor (iv, KeyArray), CryptoStreamMode.Write))
{
Cryptostream.write (datas, 0, Datas. Length);
Cryptostream.flushfinalblock ();
return encoding. GetString (Memorystream.toarray ());
}
}
}
}
Des plus decryption algorithm