using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.IO;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, System.EventArgs e)
{
// 암호화 개체 생성
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
// 개인키 생성
RSAParameters privateKey = RSA.Create().ExportParameters(true);
rsa.ImportParameters(privateKey);
string privateKeyText = rsa.ToXmlString(true);
// 공개키 생성
RSAParameters publicKey = new RSAParameters();
publicKey.Modulus = privateKey.Modulus;
publicKey.Exponent = privateKey.Exponent;
rsa.ImportParameters(publicKey);
string publicKeyText = rsa.ToXmlString(false);
Label1.Text = RSAEncrypt(TextBox1.Text, publicKeyText);
Label2.Text = RSADecrypt(Label1.Text, privateKeyText);
TextBox2.Text = Label2.Text;
}
// RSA 암호화
public string RSAEncrypt(string getValue, string pubKey)
{
System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); //암호화
rsa.FromXmlString(pubKey);
//암호화할 문자열을 UFT8인코딩
byte[] inbuf = (new UTF8Encoding()).GetBytes(getValue);
//암호화
byte[] encbuf = rsa.Encrypt(inbuf, false);
//암호화된 문자열 Base64인코딩
return Convert.ToBase64String(encbuf);
}
// RSA 복호화
public static string RSADecrypt(string getValue, string priKey)
{
//RSA객체생성
System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); //복호화
rsa.FromXmlString(priKey);
//sValue문자열을 바이트배열로 변환
byte[] srcbuf = Convert.FromBase64String(getValue);
//바이트배열 복호화
byte[] decbuf = rsa.Decrypt(srcbuf, false);
//복호화 바이트배열을 문자열로 변환
string sDec = (new UTF8Encoding()).GetString(decbuf, 0, decbuf.Length);
return sDec;
}
}
}
'C# > 암호화' 카테고리의 다른 글
[C#] AES (고급 암호화 표준) (0) | 2020.09.11 |
---|