Des plus decryption algorithm

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.