Encrypt ve Decrypt İle String Metini Şifreleme ve Şifre Çözme İşlemi

 

Merhaba arkadaşlar bu makalemizde Encrypt ile TextBoxt a girilen stringi şifreleyeceğiz. Decrypt ile de şifrelediğimiz stringin şifresini çözeceğiz.


Screenshot

 



 

 

 

 

 

 

Şekil 1

 

Default.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.Security.Cryptography;

using System.Text;

using System.IO;

 

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

{

 

    string hash = "abf0xle@rn";

    //hash key olusturuyoruz

  

     protected void btnSifrele_Click(object sender, EventArgs e)

    {

        byte[] veri = UTF8Encoding.UTF8.GetBytes(txtGiris.Text);

        using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())

        {

            byte[] keys = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(hash));

            //hash key i aliyoruz.

            //hash key ile encrypt veriyi sifreliyoruz

            using (TripleDESCryptoServiceProvider tripDes = new TripleDESCryptoServiceProvider() { Key = keys, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 })

            {

                ICryptoTransform transform = tripDes.CreateEncryptor();

                byte[] sonuc = transform.TransformFinalBlock(veri, 0, veri.Length);

                txtSifre.Text = Convert.ToBase64String(sonuc, 0, sonuc.Length);

            }

        }

    }

 

    protected void btnsifreCoz_Click(object sender, EventArgs e)

    {

       

        //stringi byte a ceviriyoruz.

        byte[] veri = Convert.FromBase64String(txtSifre.Text);

        using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())

        {

            byte[] keys = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(hash));

            // hash key i aliyoruz

           // ve hash key ile decrypt veriyi cozuyoruz

            using (TripleDESCryptoServiceProvider tripDes = new TripleDESCryptoServiceProvider() { Key = keys, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 })

            {

                ICryptoTransform transform = tripDes.CreateDecryptor();

                byte[] sonuc = transform.TransformFinalBlock(veri, 0, veri.Length);

                txtsifreCoz.Text = UTF8Encoding.UTF8.GetString(sonuc);

            }

        }

    }

 

}

  

Default.aspx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html>

 

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

<head runat="server">

    <title></title>

</head>

<body>

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

        <div>

            <br />

            &nbsp;<asp:Label ID="Label1" runat="server" Font-Size="Medium" Text="Şifrelenecek Metin:"></asp:Label>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

            <asp:TextBox ID="txtGiris" runat="server" Height="31px" Width="298px" Font-Size="Medium"></asp:TextBox>

            <br />

            <br />

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

            <asp:Button ID="btnSifrele" runat="server" OnClick="btnSifrele_Click" Text="Şifrele" BackColor="#FF9900" Font-Size="Medium" Height="43px" Width="183px" />

            <br />

            <br />

        </div>

        &nbsp;<asp:Label ID="Label2" runat="server" Font-Size="Medium" Text="Şifrelenen Metin:"></asp:Label>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

            <asp:TextBox ID="txtSifre" runat="server" Height="31px" Width="291px" Font-Size="Medium"></asp:TextBox>

            &nbsp;

        <br />

        <br />

&nbsp;<asp:Label ID="Label3" runat="server" Font-Size="Medium" Text="Şifresi Çözülmüş Metin Metin:"></asp:Label>

            <asp:TextBox ID="txtsifreCoz" runat="server" Height="31px" Width="291px" Font-Size="Medium"></asp:TextBox>

            <br />

        <br />

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

            <asp:Button ID="btnsifreCoz" runat="server" OnClick="btnsifreCoz_Click" Text="Şifre Çöz" BackColor="Lime" Font-Size="Medium" Height="45px" Width="184px" />

            <br />

        <br />

    </form>

</body>

</html>

 

Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN

About Bahadır Şahin

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Öne Çıkan Yayın

GridView da Seçili Satırı DetailsView da Göstermek

Merhaba arkadaşlar bu makalemizde GridView nesnesi ile birlikte DetailsView nesanesini birlikte kullanacağız. GridView da seçili satırın de...