. Net Common Cryptographic algorithm classes

Source: Internet
Author: User
Tags bool hash implement in degrees md5 sha1 tostring
Encryption | algorithm

. NET Framework because of the rich library support provided by the CLR, only a small amount of code can implement the encryption algorithm that was previously difficult to implement with old languages such as C. This class implements some commonly used secret algorithm, for reference. Where the MD5 algorithm returns the ToString string for int. An algorithm that returns the result of a numeric letter. See previous blog article.

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 incoming string, and return a string that has been encrypted by the hash algorithm.
Properties: [None]
Constructor Amount Parameter:
Isreturnnum: Returns the byte code for the encrypted character
Iscasesensitive: is case sensitive.
Method: This class provides four kinds of algorithms such as md5,sha1,sha256,sha512, and the length of the cipher string
Increase in degrees in turn.
</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>
Using DES encryption (Added by Niehl 2005-4-6)
</summary>
<param name= "OriginalValue" > Strings to be encrypted </param>
<param name= "key" > key (max length 8) </param>
<param name= "IV" > Initialization vector (max length 8) </param>
<returns> the encrypted string </returns>
public string Desencrypt (string originalvalue,string key,string IV)
{
Process 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>
Using des decryption (Added by Niehl 2005-4-6)
</summary>
<param name= "Encryptedvalue" > Strings to be decrypted </param>
<param name= "key" > key (max length 8) </param>
<param name= "IV" >m initialization vector (max length 8) </param>
<returns> after decryption of the string </returns>
public string Desdecrypt (string encryptedvalue,string key,string IV)
{
Process 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.