Common Tip/보안(Security)2009. 8. 13. 09:56

민범규님의 글(Devpia 펌)
RijndaelManaged을 사용하여 암호화 복호화 하는 예제을

MSDN의 내용을 최대한 간단히 만들어 본것입니다.

다음에는 XML서명에 관하여 간단한 예제를 올릴려고 합니다.

대칭형 암호화

1.     ACE

2.     DES

3.     DSE

4.     RC2

5.     Rijndael

6.     TripleDES


RijndaelManaged

현재 표준임.


테스트 예제

1 미리보기


private
void RijndaelManagedDemo_Load(object sender, EventArgs e)

        {

            textBox1.Text = "가나다라마바사아자차카타파하";

            textBox1.Text += "\r\n1234567890";

            textBox1.Text += "\r\nABCDEFGHIJKLMNOPQRSTUVWXYZ";

            textBox1.Text += "\r\nabcdefghijklmnopqrstuvwxyz";

            txtKey.Text = "12345678901234567890123456789012";//16,24,32 1

            txtIV.Text = "1234567890123456";// 16자리

    

        }

 

        private void btn암호화_Click(object sender, EventArgs e)

        {           

            MemoryStream msEncrypt = null;

            CryptoStream csEncrypt = null;

            StreamWriter swEncrypt = null;

            RijndaelManaged aesAlg = null;

 

            aesAlg = new RijndaelManaged();

            aesAlg.Key = Encoding.Default.GetBytes(txtKey.Text);

            aesAlg.IV = Encoding.Default.GetBytes(txtIV.Text);

 

            ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

 

            msEncrypt = new MemoryStream();

            csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);

            swEncrypt = new StreamWriter(csEncrypt);

 

            swEncrypt.Write(textBox1.Text);

 

            if (swEncrypt != null) swEncrypt.Close();

            if (csEncrypt != null) csEncrypt.Close();

            if (msEncrypt != null) msEncrypt.Close();

            if (aesAlg != null) aesAlg.Clear();

 

            textBox2.Text = Convert.ToBase64String(msEncrypt.ToArray());

        }

 

        private void btn복호화_Click(object sender, EventArgs e)

        {

            MemoryStream msDecrypt = null;

            CryptoStream csDecrypt = null;

            StreamReader srDecrypt = null;

            RijndaelManaged aesAlg = null;

            string plaintext = null;

 

            aesAlg = new RijndaelManaged();

            aesAlg.Key = Encoding.Default.GetBytes(txtKey.Text);

            aesAlg.IV = Encoding.Default.GetBytes(txtIV.Text);

 

            ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

 

            byte[] cipherText = Convert.FromBase64String(textBox2.Text);

            msDecrypt = new MemoryStream(cipherText);

            csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);

            srDecrypt = new StreamReader(csDecrypt);

 

            plaintext = srDecrypt.ReadToEnd();

 

            if (srDecrypt != null)  srDecrypt.Close();

            if (csDecrypt != null)  csDecrypt.Close();

            if (msDecrypt != null)  msDecrypt.Close();

            if (aesAlg != null)    aesAlg.Clear();

 

            textBox3.Text = plaintext;

       }


Posted by penguindori