. NET password encryption (reversible)

Source: Internet
Author: User

The user login password encrypted, in order to see the original password, can be decrypted. The main purpose is to prevent someone from knowing the database user name and password using Query Analyzer to enter the database to view the password

Without the use of MD5, MD5 cannot be decrypted.

<summary>
Encryption
</summary>
<param name= "RS" ></param>
<returns></returns>
public static string Desencryptmethod (String rs)
{
byte[] Deskey = new byte[] {0x16, 0x09, 0x14, 0x15, 0x07, 0x01, 0x05, 0x08};
byte[] Desiv = new byte[] {0x16, 0x09, 0x14, 0x15, 0x07, 0x01, 0x05, 0x08};

DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
Try
{
byte[] Inputbytearray = Encoding.Default.GetBytes (RS);
Byte[] Inputbytearray=encoding.unicode.getbytes (RS);

Des.  Key = Deskey; ASCIIEncoding.ASCII.GetBytes (SKey);
DES.IV = Desiv; ASCIIEncoding.ASCII.GetBytes (SKey);
MemoryStream ms = new MemoryStream ();
CryptoStream cs = new CryptoStream (MS, Des. CreateEncryptor (),
CryptoStreamMode.Write);
Write the byte array into the crypto stream
(It'll end up in the memory stream)
Cs. Write (Inputbytearray, 0, inputbytearray.length);
Cs. FlushFinalBlock ();

Get the data back from the memory stream, and into a string
StringBuilder ret = new StringBuilder ();
foreach (Byte b in Ms. ToArray ())
{
Format as Hex
Ret. AppendFormat ("{0:x2}", b);
}
Ret. ToString ();
return ret. ToString ();
}
Catch
{
Return RS;
}
Finally
{
des = null;
}
}

<summary>
Decrypt
</summary>
<param name= "RS" ></param>
<returns></returns>
public static string Desdecryptmethod (String rs)
{
byte[] Deskey = new byte[] {0x16, 0x09, 0x14, 0x15, 0x07, 0x01, 0x05, 0x08};
byte[] Desiv = new byte[] {0x16, 0x09, 0x14, 0x15, 0x07, 0x01, 0x05, 0x08};

DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
Try
{
Put the input string into the byte array
byte[] Inputbytearray = new Byte[rs. LENGTH/2];
for (int x = 0; x < Rs. LENGTH/2; X + +)
{
int i = (Convert.ToInt32 (Rs. Substring (x * 2, 2), 16));
INPUTBYTEARRAY[X] = (byte) i;
}

Des.   Key = Deskey; ASCIIEncoding.ASCII.GetBytes (SKey);
DES.IV = Desiv; ASCIIEncoding.ASCII.GetBytes (SKey);
MemoryStream ms = new MemoryStream ();
CryptoStream cs = new CryptoStream (MS, Des. CreateDecryptor (), cryptostreammode.write);
Flush the data through the crypto stream into the memory stream
Cs. Write (Inputbytearray, 0, inputbytearray.length);
Cs. FlushFinalBlock ();

                //get the decrypted data back from the memory stream
      & nbsp;         StringBuilder ret = new StringBuilder ();

                 return System.Text.Encoding.Default.GetString (Ms. ToArray ());
           }
            Catch
             {
                 return RS;
           }
            finally
             {
                 des = null;
           }
       }

can be used directly.

. NET password encryption (reversible)

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.