■ RSACryptoServiceProvider 클래스 : 비대칭키 암호화 사용하기

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


TestProject.zip


MainForm.cs

 

 

using System;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// RSA 헬퍼

        /// </summary>

        private RSAHelper rsaHelper = null;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            Load += Form_Load;

            this.processButton.Click += processButton_Click;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

 

        /// <summary>

        /// 폼 로드시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void Form_Load(object sender, EventArgs e)

        {

            this.rsaHelper = new RSAHelper();

 

            this.privateKeyTextBox.Text = this.rsaHelper.PrivateKey;

            this.publicKeyTextBox.Text  = this.rsaHelper.PublicKey;

        }

 

        #endregion

        #region 암복호화 버튼 클릭시 처리하기 - processButton_Click(sender, e)

 

        /// <summary>

        /// 암복호화 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void processButton_Click(object sender, EventArgs e)

        {

            string cipherText = this.rsaHelper.Encrypt(this.plainTextTextBox.Text);

 

            this.cipherTextTextBox.Text = cipherText;

 

            string plainText = this.rsaHelper.Decrypt(cipherText);

 

            this.resultTextTextBox.Text = plainText;

        }

 

        #endregion

    }

}

 

 

RSAHelper.cs

 

 

using System;

using System.Security.Cryptography;

using System.Text;

 

namespace TestProject

{

    /// <summary>

    /// RSA 헬퍼

    /// </summary>

    public class RSAHelper

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// RSA 암호화 서비스 공급자

        /// </summary>

        private RSACryptoServiceProvider provider;

 

        /// <summary>

        /// 개인 키 RSA 매개 변수

        /// </summary>

        private RSAParameters privateKeyParameters;

 

        /// <summary>

        /// 개인 키

        /// </summary>

        private string privateKey;

 

        /// <summary>

        /// 공개 키 RSA 매개 변수

        /// </summary>

        private RSAParameters publicKeyParameters;

 

        /// <summary>

        /// 공개 키

        /// </summary>

        private string publicKey;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 개인 키 - PrivateKey

 

        /// <summary>

        /// 개인 키

        /// </summary>

        public string PrivateKey

        {

            get

            {

                return this.privateKey;

            }

        }

 

        #endregion

        #region 공개 키 - PublicKey

 

        /// <summary>

        /// 공개 키

        /// </summary>

        public string PublicKey

        {

            get

            {

                return this.publicKey;

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - RSAHelper()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public RSAHelper()

        {

            this.provider = new RSACryptoServiceProvider();

 

            this.privateKeyParameters = RSA.Create().ExportParameters(true);

 

            this.provider.ImportParameters(this.privateKeyParameters);

 

            this.privateKey = this.provider.ToXmlString(true);

 

 

            this.publicKeyParameters = new RSAParameters();

 

            this.publicKeyParameters.Modulus  = this.privateKeyParameters.Modulus;

            this.publicKeyParameters.Exponent = this.privateKeyParameters.Exponent;

 

            this.provider.ImportParameters(this.publicKeyParameters);

 

            this.publicKey = this.provider.ToXmlString(false);

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Public

 

        #region 암호화 하기 - Encrypt(plainText, publicKey)

 

        /// <summary>

        /// 암호화 하기

        /// </summary>

        /// <param name="plainText">평문 텍스트</param>

        /// <param name="publicKey">공개 키</param>

        /// <returns>암호화 텍스트</returns>

        public static string Encrypt(string plainText, string publicKey)

        {

            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();

 

            provider.FromXmlString(publicKey);

 

            byte[] plainTextArray = Encoding.UTF8.GetBytes(plainText);

 

            byte[] cipherTextArray = provider.Encrypt(plainTextArray, false);

 

            return Convert.ToBase64String(cipherTextArray);

        }

 

        #endregion

        #region 복호화 하기 - Decrypt(cipherText, privateKey)

 

        /// <summary>

        /// 복호화 하기

        /// </summary>

        /// <param name="cipherText">암호화 텍스트</param>

        /// <param name="privateKey">개인 키</param>

        /// <returns>평문 텍스트</returns>

        public static string Decrypt(string cipherText, string privateKey)

        {

            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();

 

            provider.FromXmlString(privateKey);

 

            byte[] cipherTextArray = Convert.FromBase64String(cipherText);

 

            byte[] plainTextArray = provider.Decrypt(cipherTextArray, false);

 

            string plainText = Encoding.UTF8.GetString(plainTextArray, 0, plainTextArray.Length);

 

            return plainText;

        }

 

        #endregion

 

        ////////////////////////////////////////////////////////////////////////////////////////// Instance

        //////////////////////////////////////////////////////////////////////////////// Public

 

        #region 암호화 하기 - Encrypt(plainText)

 

        /// <summary>

        /// 암호화 하기

        /// </summary>

        /// <param name="plainText">평문 텍스트</param>

        /// <returns>암호화 텍스트</returns>

        public string Encrypt(string plainText)

        {

            this.provider.FromXmlString(this.publicKey);

 

            byte[] plainTextArray = Encoding.UTF8.GetBytes(plainText);

 

            byte[] cipherTextArray = this.provider.Encrypt(plainTextArray, false);

 

            return Convert.ToBase64String(cipherTextArray);

        }

 

        #endregion

        #region 복호화 하기 - Decrypt(cipherText)

 

        /// <summary>

        /// 복호화 하기

        /// </summary>

        /// <param name="cipherText">암호화 텍스트</param>

        /// <returns>평문 텍스트</returns>

        public string Decrypt(string cipherText)

        {

            this.provider.FromXmlString(this.privateKey);

 

            byte[] cipherTextArray = Convert.FromBase64String(cipherText);

 

            byte[] plainTextArray = this.provider.Decrypt(cipherTextArray, false);

 

            string plainText = Encoding.UTF8.GetString(plainTextArray, 0, plainTextArray.Length);

 

            return plainText;

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG