. NET Common encryption algorithms

Source: Internet
Author: User

The. NET Framework supports a wide range of databases provided by CLR. Only a small amount of code is required to implement encryption algorithms that are hard to implement in old languages such as C. This class implements some common secret algorithms for your reference. The MD5 Algorithm returns the int tostring. For algorithms that return alphanumeric results, see previous blog articles.

Using system;
Using system. IO;
Using system. Data;
Using system. text;
Using system. diagnostics;
Using system. Security;
Using system. Security. cryptography;

Namespace com. quickline. Encrypt
{
/// <Summary>
/// Class Name: hashencrypt
/// Function: hash the input string and return the string encrypted by the hash algorithm.
/// Attribute: [none]
/// Parameters of the constructor:
/// Isreturnnum: whether to return the byte code of the encrypted character
/// Iscasesensitive: case sensitive.
/// Method: This class provides MD5, sha1, sha256, sha512, and other algorithms, the length of the encrypted string
Degrees increase sequentially.
/// </Summary>
Public class hashencrypt
{
// Private string strin;
Private bool isreturnnum;
Private bool iscasesensitive;

Public hashencrypt (bool iscasesensitive, bool isreturnnum)
{
This. isreturnnum = isreturnnum;
This. iscasesensitive = iscasesensitive;
}


Private string getstrin (string strin)
{
// String strin = strin;
If (strin. Length = 0)
{
Strin = "~ Null ~ ";
}
If (iscasesensitive = false)
{
Strin = strin. toupper ();
}
Return strin;
}
Public String md5encrypt (string strin)
{
// String strin = getstrin (strin );
Byte [] tmpbyte;
MD5 MD5 = new md5cryptoserviceprovider ();
Tmpbyte =
Md5.computehash (getkeybytearray (getstrin (strin )));
Md5.clear ();

Return getstringvalue (tmpbyte );

}

Public String sha1encrypt (string strin)
{
// String strin = getstrin (strin );
Byte [] tmpbyte;
Sha1 sha1 = new sha1cryptoserviceprovider ();

Tmpbyte = sha1.computehash (getkeybytearray (strin ));
Sha1.clear ();

Return getstringvalue (tmpbyte );

}

Public String sha256encrypt (string strin)
{
// String strin = getstrin (strin );
Byte [] tmpbyte;
Sha256 sha256 = new sha256managed ();

Tmpbyte =
Sha256.computehash (getkeybytearray (strin ));
Sha256.clear ();

Return getstringvalue (tmpbyte );

}

Public String sha512encrypt (string strin)
{
// String strin = getstrin (strin );
Byte [] tmpbyte;
Sha512 sha512 = new sha512managed ();

Tmpbyte =
Sha512.computehash (getkeybytearray (strin ));
Sha512.clear ();

Return getstringvalue (tmpbyte );

}

/// <Summary>
/// Use DES encryption (Added by niehl 2005-4-6)
/// </Summary>
/// <Param name = "originalvalue"> string to be encrypted </param>
/// <Param name = "key"> key (maximum length 8) </param>
/// <Param name = "IV"> initialization vector (maximum length 8) </param>
/// <Returns> encrypted string </returns>
Public String desencrypt (string originalvalue, string key, string IV)
{
// Process the key and IV into 8 characters
Key + = "12345678 ";
IV + = "12345678 ";
Key = key. substring (0, 8 );
IV = IV. substring (0, 8 );

Symmetricalgorithm SA;
Icryptotransform CT;
Memorystream MS;
Cryptostream Cs;
Byte [] BYT;

Sa = new descryptoserviceprovider ();
SA. Key = encoding. utf8.getbytes (key );
SA. IV = encoding. utf8.getbytes (IV );
Ct = sa. createencryptor ();

Byt = encoding. utf8.getbytes (originalvalue );

MS = new memorystream ();
Cs = new cryptostream (MS, CT,
Cryptostreammode. Write );
CS. Write (BYT, 0, byt. Length );
CS. flushfinalblock ();

CS. Close ();

Return convert. tobase64string (Ms. toarray ());

}

Public String desencrypt (string originalvalue, string key)
{
Return desencrypt (originalvalue, key, key );
}

/// <Summary>
/// Use des for decryption (Added by niehl 2005-4-6)
/// </Summary>
/// <Param name = "encryptedvalue"> string to be decrypted </param>
/// <Param name = "key"> key (maximum length 8) </param>
/// <Param name = "IV"> M initialization vector (maximum length 8) </param>
/// <Returns> decrypted string </returns>
Public String desdecrypt (string encryptedvalue, string key, string IV)
{
// Process the key and IV into 8 characters
Key + = "12345678 ";
IV + = "12345678 ";
Key = key. substring (0, 8 );
IV = IV. substring (0, 8 );

Symmetricalgorithm SA;
Icryptotransform CT;
Memorystream MS;
Cryptostream Cs;
Byte [] BYT;

Sa = new descryptoserviceprovider ();
SA. Key = encoding. utf8.getbytes (key );
SA. IV = encoding. utf8.getbytes (IV );
Ct = sa. createdecryptor ();

Byt = convert. frombase64string (encryptedvalue );

MS = new memorystream ();
Cs = new cryptostream (MS, CT,
Cryptostreammode. Write );
CS. Write (BYT, 0, byt. Length );
CS. flushfinalblock ();

CS. Close ();

Return encoding. utf8.getstring (Ms. toarray ());

}

Public String desdecrypt (string encryptedvalue, string key)
{
Return desdecrypt (encryptedvalue, key, key );
}

Private string getstringvalue (byte [] Byte)
{
String tmpstring = "";

If (this. isreturnnum = false)
{
Asciiencoding ASC = new asciiencoding ();
Tmpstring = ASC. getstring (byte );
}
Else
{
Int icounter;

For
(Icounter = 0; icounter <byte. length; icounter ++)
{
Tmpstring = tmpstring +
Byte [icounter]. tostring ();
}

}

Return tmpstring;
}

Private byte [] getkeybytearray (string strkey)
{

Asciiencoding ASC = new asciiencoding ();

Int tmpstrlen = strkey. length;
Byte [] tmpbyte = new byte [tmpStrLen-1];

Tmpbyte = ASC. getbytes (strkey );

Return tmpbyte;

}

}
}

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.