반응형
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Threading; using System.Security.Cryptography; namespace AESENC { public class AES { //AES_256 암호화 public String AESEncrypt256(String Input, String key) { RijndaelManaged aes = new RijndaelManaged(); aes.KeySize = 256; aes.BlockSize = 128; aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; aes.Key = Encoding.UTF8.GetBytes(key); aes.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; var encrypt = aes.CreateEncryptor(aes.Key, aes.IV); byte[] xBuff = null; using (var ms = new MemoryStream()) { using (var cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write)) { byte[] xXml = Encoding.UTF8.GetBytes(Input); cs.Write(xXml, 0, xXml.Length); } xBuff = ms.ToArray(); } String Output = Convert.ToBase64String(xBuff); return Output; } //AES_256 복호화 public String AESDecrypt256(String Input, String key) { RijndaelManaged aes = new RijndaelManaged(); aes.KeySize = 256; aes.BlockSize = 128; aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; aes.Key = Encoding.UTF8.GetBytes(key); aes.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; var decrypt = aes.CreateDecryptor(); byte[] xBuff = null; using (var ms = new MemoryStream()) { using (var cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write)) { byte[] xXml = Convert.FromBase64String(Input); cs.Write(xXml, 0, xXml.Length); } xBuff = ms.ToArray(); } String Output = Encoding.UTF8.GetString(xBuff); return Output; } //AES_128 암호화 public String AESEncrypt128(String Input, String key) { RijndaelManaged RijndaelCipher = new RijndaelManaged(); byte[] PlainText = System.Text.Encoding.Unicode.GetBytes(Input); byte[] Salt = Encoding.ASCII.GetBytes(key.Length.ToString()); PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(key, Salt); ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16)); MemoryStream memoryStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write); cryptoStream.Write(PlainText, 0, PlainText.Length); cryptoStream.FlushFinalBlock(); byte[] CipherBytes = memoryStream.ToArray(); memoryStream.Close(); cryptoStream.Close(); string EncryptedData = Convert.ToBase64String(CipherBytes); return EncryptedData; } //AE_S128 복호화 public String AESDecrypt128(String Input, String key) { RijndaelManaged RijndaelCipher = new RijndaelManaged(); byte[] EncryptedData = Convert.FromBase64String(Input); byte[] Salt = Encoding.ASCII.GetBytes(key.Length.ToString()); PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(key, Salt); ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16)); MemoryStream memoryStream = new MemoryStream(EncryptedData); CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read); byte[] PlainText = new byte[EncryptedData.Length]; int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length); memoryStream.Close(); cryptoStream.Close(); string DecryptedData = Encoding.Unicode.GetString(PlainText, 0, DecryptedCount); return DecryptedData; } } }
반응형
'Language > C#' 카테고리의 다른 글
C# DataGridView excel export 시 HRESULT: 0x800A03EC 에러 (2) | 2014.01.14 |
---|---|
ASP.NET(C#) 오라클(Oracle) 연결 시 BadImageFormatException 에러 발생 (0) | 2013.09.16 |
C# 링큐(LINQ) 사용 예제 (0) | 2013.08.08 |
C# exe 파일(Release 버전) 생성 (6) | 2013.08.08 |
C# XML 파싱 예제(How to parse XML in C#) (2) | 2013.07.10 |
[C#] Microsoft.ACE.OLEDB.12.0' 공급자는 로컬 컴퓨터에 등록할 수 없습니다. (0) | 2013.03.23 |
ASP.NET 쿠키(cookie), ViewState 사용 방법 (1) | 2013.01.31 |
ASP.NET Page.IsPostBack (0) | 2013.01.31 |
ASP.NET 그리드뷰(GridView) (0) | 2013.01.30 |
ASP.NET 아작스/에이잭스 (AJAX) 예제(XMLHTTP) (0) | 2013.01.30 |