MD5, SHA256, SHA512 encryption algorithms, and reversible algorithms

Source: Internet
Author: User
Tags md5 md5 encryption static class
using System using System.Collections.Generic; using System.Linq; using
System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Security.Cryptography;
Using System.Text;    
public static string Getmd5password (string password) {MD5CryptoServiceProvider crypto = new MD5CR
    Yptoserviceprovider ();
    byte[] bytes = Encoding.UTF7.GetBytes (password);
    bytes = Crypto.computehash (bytes);
    StringBuilder sb = new StringBuilder (); foreach (Byte num in bytes) {sb.
    AppendFormat ("{0:x2}", num); Return SB.
ToString ();
    public static string Getsha512password (string password) {byte[] bytes = Encoding.UTF7.GetBytes (password);
    Byte[] result;
    SHA512 sham = new sha512managed ();
    result = Sham.computehash (bytes);
    StringBuilder sb = new StringBuilder (); foreach (byte num in result) {sb.
    AppendFormat ("{0:x2}", num); Return SB.
ToString (); }
<pre class= "CSharp" name= "code" > private static string HashString256 (String Stringtohash, String hachkey)
        { c1/>utf8encoding Myencoder = new UTF8Encoding ();
            byte[] key = Myencoder.getbytes (Hachkey);
            byte[] Text = myencoder.getbytes (stringtohash);

            System.Security.Cryptography.HMACSHA256 myHMACSHA256 = new System.Security.Cryptography.HMACSHA256 (key);
            byte[] Hashcode = Myhmacsha256.computehash (text);
            String hash = bitconverter.tostring (hashcode). Replace ("-", "");

            return hash. ToLower ();
        }


Baidu's MD5 encryption, unlike the one above. Public
static string MD5 (string password)
{
    byte[] textbytes = System.Text.Encoding.Default.GetBytes (password);
    Try
    {
        System.Security.Cryptography.MD5CryptoServiceProvider crypthandler;
        Crypthandler = new System.Security.Cryptography.MD5CryptoServiceProvider ();
        Byte[] hash = Crypthandler.computehash (textbytes);
        string ret = "";
        foreach (Byte A in hash)
        {
            if (a <) ret + +
                "0" + a.tostring ("x");
            else
                ret = a.tostring ("x");
        }
        return ret;
    }
    Catch
    {
        throw;
    }
} 


Note: MD5 generates 32-bit passwords

SHA512 generates a 128 password

SHA256 generates a 64 password

Reversible Cryptographic algorithm: the number of digits produced is determined by the length of the password itself

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Security.Cryptography;

Using System.IO;

        namespace Glacierv2.common {public static class Global {private static int _newdbprimarykeyseed = 0;
        #region encryption and decryption algorithm private static symmetricalgorithm Mobjcryptoservice = new RijndaelManaged ();   
        <summary>///obtain key///</summary>///<returns> key </returns>
            private static byte[] Getlegalkey () {String stemp = "XFSDFGSFGSDGSDFGSDFG";
            Mobjcryptoservice.generatekey ();
            byte[] Byttemp = Mobjcryptoservice.key;
            int keylength = Byttemp.length;
            if (Stemp.length > keylength) stemp = stemp.substring (0, keylength);
            else if (Stemp.length < keylength) Stemp = Stemp.padright (Keylength, ""); Return ASCIIEncoding.ASCII.GetBytes(stemp); ///<summary>///Get initial vector iv///</summary>///<returns> First Vector IV </returns> private static byte[] Getlegaliv () {String stemp = ' Swetwerehetyeryertyer
            Ty ";
            Mobjcryptoservice.generateiv ();
            byte[] Byttemp = MOBJCRYPTOSERVICE.IV;
            int ivlength = Byttemp.length;
            if (Stemp.length > ivlength) stemp = stemp.substring (0, ivlength);
            else if (Stemp.length < ivlength) Stemp = Stemp.padright (Ivlength, "");
        Return ASCIIEncoding.ASCII.GetBytes (stemp); ///<summary>///Encryption method///</summary>///<param name= "Source" &G  T; strings to encrypt </param>///<returns> encrypted string </returns> public static string Encrypto (string
    SOURCE) {byte[] Bytin = UTF8Encoding.UTF8.GetBytes (source);        MemoryStream ms = new MemoryStream ();
            Mobjcryptoservice.key = Getlegalkey ();
            MOBJCRYPTOSERVICE.IV = Getlegaliv ();
            ICryptoTransform encrypto = Mobjcryptoservice.createencryptor ();
            CryptoStream cs = new CryptoStream (MS, Encrypto, CryptoStreamMode.Write); Cs.
            Write (bytin, 0, bytin.length); Cs.
            FlushFinalBlock (); Ms.
            Close (); Byte[] Bytout = Ms.
            ToArray ();
        Return convert.tobase64string (bytout); ///<summary>///Decryption method///</summary>///<param name= "Source" &G  t; to be decrypted string </param>///<returns> decrypted string </returns> public static string Decrypto (string
            SOURCE) {byte[] Bytin = convert.frombase64string (source);
            MemoryStream ms = new MemoryStream (bytin, 0, bytin.length);
            Mobjcryptoservice.key = Getlegalkey (); MOBJCRYPTOSERVICE.IV = GetlEgaliv ();
            ICryptoTransform encrypto = Mobjcryptoservice.createdecryptor ();
            CryptoStream cs = new CryptoStream (MS, Encrypto, CryptoStreamMode.Read);
            StreamReader sr = new StreamReader (CS); return Sr.
        ReadToEnd ();
 } #endregion}}


 

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.