When I was writing a project, I used MD5 encryption in the background. One day, people in the group asked, are there any other encryption methods besides MD5? At that time, I only knew that there was a sha, but I was not clear about how to implement anything. So I sorted out several common encryption methods and wrote them using winform. Program ,:
KeyCode
Using system. Security;
Using system. Security. cryptography;
Using system. Web;
Using system. IO;
// MD5 case insensitive
// Type. The value is 16-bit or 32-bit. The value of 16-bit indicates that the value ranges from 8th to 16-bit.
Public String domd5encode (string PWD, string type)
{
Byte [] result = encoding. Default. getbytes (PWD );
Md5cryptoserviceprovider MD5 = new md5cryptoserviceprovider ();
Byte [] Output = md5.computehash (result );
If (type = "16 ")
Return bitconverter. tostring (output). Replace ("-", ""). tolower (). substring (8, 16 );
Else
Return bitconverter. tostring (output). Replace ("-", ""). tolower ();
}
/// <Summary>
/// Encrypt the string using sha1
/// </Summary>
/// <Param name = "strin"> string to be encrypted </param>
/// <Returns> ciphertext </returns>
Public String sha1_encrypt (string source_string)
{
Byte [] strres = encoding. Default. getbytes (source_string );
Hashalgorithm Isha = new sha1cryptoserviceprovider ();
Strres = Isha. computehash (strres );
Stringbuilder entext = new stringbuilder ();
Foreach (byte ibyte in strres)
{
Entext. appendformat ("{0: X2}", ibyte );
}
Return entext. tostring ();
}
/// <Summary>
/// Sha256 encryption, irreversible
/// </Summary>
/// <Param name = "str"> string STR: Encrypted string </param>
/// <Returns> returns the encrypted string </returns>
Private string sha256encrypt (string Str)
{
System. Security. cryptography. sha256 s256 = new system. Security. cryptography. sha256managed ();
Byte [] byte1;
Byte1 = s256.computehash (encoding. Default. getbytes (STR ));
S256.clear ();
Return convert. tobase64string (byte1 );
}
/// <Summary>
/// Sha384 encryption, irreversible
/// </Summary>
/// <Param name = "str"> string STR: Encrypted string </param>
/// <Returns> returns the encrypted string </returns>
Private string sha384encrypt (string Str)
{
System. Security. cryptography. sha384 s384 = new system. Security. cryptography. sha384managed ();
Byte [] byte1;
Byte1 = s384.computehash (encoding. Default. getbytes (STR ));
S384.clear ();
Return convert. tobase64string (byte1 );
}
/// <Summary>
/// Sha512 encryption, irreversible
/// </Summary>
/// <Param name = "str"> string STR: Encrypted string </param>
/// <Returns> returns the encrypted string </returns>
Private string sha512encrypt (string Str)
{
System. Security. cryptography. sha512 S512 = new system. Security. cryptography. sha512managed ();
Byte [] byte1;
Byte1 = s512.computehash (encoding. Default. getbytes (STR ));
S512.clear ();
Return convert. tobase64string (byte1 );
}
// Default key vector
Private byte [] keys = {0xef, 0xab, 0x56, 0x78, 0x90, 0x34, 0xcd, 0x12 };
/// <Summary>
/// DES encrypted string
/// </Summary>
/// <Param name = "encryptstring"> string to be encrypted </param>
/// <Param name = "encryptkey"> encryption key, which must be 8-bit </param>
/// <Returns> the encrypted string is returned successfully. If the source string fails to be returned, </returns>
Public String encryptdes (string encryptstring, string encryptkey)
{
Try
{
Byte [] rgbkey = encoding. utf8.getbytes (encryptkey. substring (0, 8 ));
Byte [] rgbiv = keys;
Byte [] inputbytearray = encoding. utf8.getbytes (encryptstring );
Descryptoserviceprovider dcsp = new descryptoserviceprovider ();
Memorystream mstream = new memorystream ();
Cryptostream cstream = new cryptostream (mstream, dcsp. createencryptor (rgbkey, rgbiv), cryptostreammode. Write );
Cstream. Write (inputbytearray, 0, inputbytearray. Length );
Cstream. flushfinalblock ();
Return convert. tobase64string (mstream. toarray ());
}
Catch
{
Return encryptstring;
}
}
/// <Summary>
/// Des decryption string
/// </Summary>
/// <Param name = "decryptstring"> string to be decrypted </param>
/// <Param name = "decryptkey"> the decryption key must be 8 bits, which is the same as the encryption key </param>
/// <Returns> the decrypted string is returned successfully, and the source string fails to be returned. </returns>
Public String decryptdes (string decryptstring, string decryptkey)
{
Try
{
Byte [] rgbkey = encoding. utf8.getbytes (decryptkey. substring (0, 8 ));
Byte [] rgbiv = keys;
Byte [] inputbytearray = convert. frombase64string (decryptstring );
Descryptoserviceprovider dcsp = new descryptoserviceprovider ();
Memorystream mstream = new memorystream ();
Cryptostream cstream = new cryptostream (mstream, dcsp. createdecryptor (rgbkey, rgbiv), cryptostreammode. Write );
Cstream. Write (inputbytearray, 0, inputbytearray. Length );
Cstream. flushfinalblock ();
Return encoding. utf8.getstring (mstream. toarray ());
}
Catch
{
Return decryptstring;
}
}