Compatibility of common cryptographic algorithms with other languages in C #

Source: Internet
Author: User
Tags md5 md5 encryption tostring
Encryption | algorithm

The simple implementation of ASP version Md5.perl des algorithm on the Internet in C #


1:md5

Previously in the ASP era commonly used MD5 algorithm seems to be from the dynamic network flow out, and then we all use it, basically there are two kinds, the difference in the end of the md5.asp part

MD5 = LCase (Wordtohex (a) & Wordtohex (b) & Wordtohex (c) & Wordtohex (d))

Md5=lcase (Wordtohex (b) & Wordtohex (c))

Corresponding 32-bit and 16-bit encryption respectively

In C #, the corresponding implementation is

<summary>
16-bit MD5 encryption method, used by previous Dvbbs
</summary>
<param name= "strsource" > To encrypt Strings </param>
<returns> Encrypted String </returns>
public string Md5encrypt (string strsource)
{
Return Md5encrypt (strsource, 16);
}
<summary>
MD5 encryption, with the same 16/32-bit MD5 encryption results on the web
</summary>
<param name= "strsource" > To encrypt Strings </param>
<param name= "Length" >16 or one of 32 values, others adopt the. NET default MD5 encryption algorithm </param>
<returns> Encrypted String </returns>
public string Md5encrypt (string strsource, int length)
{
byte[] bytes = Encoding.ASCII.GetBytes (strsource);
Byte[] HashValue = ((System.Security.Cryptography.HashAlgorithm) System.Security.Cryptography.CryptoConfig.CreateFromName ("MD5")). ComputeHash (bytes);
StringBuilder sb = new StringBuilder ();
Switch (length)
{
Case 16:
for (int i = 4; i < i++)
Sb. Append (Hashvalue[i]. ToString ("X2"));
Break
Case 32:
for (int i = 0; i < i++)
{
Sb. Append (Hashvalue[i]. ToString ("X2"));
}
Break
Default
for (int i = 0; i < hashvalue.length; i++)
{
Sb. Append (Hashvalue[i]. ToString ("X2"));
}
Break
}

The same other language implements DES encryption and the. NET Framework des Foundation implementation is not the same, the more depressing is that when I first started using the. NET Framework, I actually rewrote the Perl version of DES, only to find that there is a simpler way, Because the internet spread of the Perl/c/java version of the DES algorithm is block encryption, set Ciphermode for the ECB is good, depressed ing.

The source code is as follows

public static byte[] Deskey = new byte[] {0x82, 0xBC, 0xa1, 0x6a, 0xf5, 0x87, 0x3b, 0xe6, 0x59, 0x6a, 0x32, 0x64, 0x7F, 0x 3 A, 0x2a, 0xBB, 0x2b, 0x68, 0xe2, 0x5f, 0x06, 0xFB, 0xb8, 0x2d, 0x67, 0xb3, 0x55, 0x19, 0x4e, 0xb8, 0xBF, 0xDD};
<summary>
DES encryption
</summary>
<param name= "strsource" > To encrypt Strings </param>
<param name= "key" >32 bit key value </param>
<returns> the encrypted string </returns>
public string Desencrypt (string strsource) {
Return Desencrypt (strsource, Deskey);
}
public string Desencrypt (string strsource,byte[] key)
{
SymmetricAlgorithm sa = rijndael.create ();
Sa. key = key;
Sa. Mode= CIPHERMODE.ECB;
Sa. Padding = Paddingmode.zeros;
MemoryStream ms = new MemoryStream ();
CryptoStream cs = new CryptoStream (MS, SA. CreateEncryptor (), cryptostreammode.write);
byte[] byt = Encoding.Unicode.GetBytes (strsource);
Cs. Write (byt, 0, Byt. Length);
Cs. FlushFinalBlock ();
Cs. Close ();
Return convert.tobase64string (Ms. ToArray ());
}
<summary>
Des decryption
</summary>
<param name= "strsource" > Strings to be decrypted </param>
<param name= "key" >32 bit key value </param>
<returns> after decryption of the string </returns>
public string Desdecrypt (string strsource) {
Return Desdecrypt (strsource, Deskey);
}
public string Desdecrypt (string strsource,byte[] key)
{
SymmetricAlgorithm sa = rijndael.create ();
Sa. key = key;
Sa. Mode = CIPHERMODE.ECB;
Sa. Padding = Paddingmode.zeros;
ICryptoTransform ct = sa. CreateDecryptor ();
byte[] byt = convert.frombase64string (strsource);
MemoryStream ms = new MemoryStream (byt);
CryptoStream cs = new CryptoStream (MS, CT, cryptostreammode.read);
StreamReader sr = new StreamReader (cs, Encoding.unicode);
return Sr. ReadToEnd ();
}




Related Article

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.