■ RSACryptoServiceProvider 클래스를 사용해 비대칭키 암호화하는 방법을 보여준다.
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
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 } } |