울어라휘파람새야

ASP.NET(C#) MD5, SHA256 암호화 예제 본문

밥줄/.NET or C#

ASP.NET(C#) MD5, SHA256 암호화 예제

꼬꼬리코 2014. 2. 19. 16:04

* WebForm2.aspx

---------------------------------------------------------------------------------------------------------

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebForm2" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <label>값 입력</label>

        <input type="text" name="testvalue" />

        <br /><br />

        <label>MD5 결과</label>

        <asp:Label ID="md5Result" runat="server"></asp:Label>

        <br />

    </div>

    <br /><br /><br /><br />

    <div>

        <br /><br />

        <label>SHA256 결과</label>

        <asp:Label ID="shaResult" runat="server"></asp:Label>

    </div>

    <br /><br />

    <div>

        <br /><br />

        <label>MD5 -> SHA256 결과</label>

        <asp:Label ID="md5ChSha" runat="server"></asp:Label>

    </div>

    <br /><br /><br /><br />

    <div>

        <input type="submit" name="subBtn"/>

    </div>

    </form>

</body>

</html>

---------------------------------------------------------------------------------------------------------

* WebForm2.aspx.cs

---------------------------------------------------------------------------------------------------------

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Text;

using System.Security.Cryptography;


public partial class WebForm2 : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (IsPostBack)

        {

            string testvalue;


            testvalue = Request.Form["testvalue"];


            md5Result.Text = MD5Hash(testvalue);

            shaResult.Text = SHA256Hash(testvalue);


            md5ChSha.Text = SHA256Hash(MD5Hash(testvalue));

        }       

    }

    // MD5 암호화 128bit 암호화

    public static string MD5Hash(string Data)

    {

        MD5 md5 = new MD5CryptoServiceProvider();

        byte[] hash = md5.ComputeHash(Encoding.ASCII.GetBytes(Data));


        StringBuilder stringBuilder = new StringBuilder();

        foreach (byte b in hash)

        {

            stringBuilder.AppendFormat("{0:x2}", b);

        }

        return stringBuilder.ToString();

    } 


    // SHA256  256bit 암호화

    public static string SHA256Hash(string Data)

    {

        SHA256 sha = new SHA256Managed();

        byte[] hash = sha.ComputeHash(Encoding.ASCII.GetBytes(Data));


        StringBuilder stringBuilder = new StringBuilder();

        foreach (byte b in hash)

        {

            stringBuilder.AppendFormat("{0:x2}", b);

        }

        return stringBuilder.ToString();

    } 

}



Comments