C#
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
// 클래스 호출
Security AES = new Security();
// 키32자리
string key = "12345678901234567890123456789012";
// 암호화 문자열
string str = "암호화할 문자열입니다.";
// 암호화된 문자열
string result1 = AES.AES_encrypt(str, key);
// 암호화된 문자열 출력
Console.WriteLine(result1);
// 복호화된 문자열
string result2 = AES.AES_decrypt(result1, key);
// 복호화된 문자열 출력
Console.WriteLine(result2);
}
}
public class Security
{
public string AES_encrypt(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 (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write))
{
Byte[] xXml = Encoding.UTF8.GetBytes(input);
cs.Write(xXml, 0, xXml.Length);
}
xBuff = ms.ToArray();
}
return Convert.ToBase64String(xBuff);
}
public string AES_decrypt(string input, string key)
{
if (input == "") { return ""; }
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 (MemoryStream ms = new MemoryStream())
{
try
{
using (CryptoStream cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write))
{
Byte[] xXml = Convert.FromBase64String(input);
cs.Write(xXml, 0, xXml.Length);
}
}
catch
{
return "";
}
xBuff = ms.ToArray();
}
if (xBuff.Length > 0)
{
string Output = Encoding.UTF8.GetString(xBuff);
return Output.Trim();
}
else
{
return "";
}
}
}
}
Visual Basic
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text
Module Module1
Sub Main()
'클래스 호출
Dim AES As Security = New Security()
'키32자리
Dim key As String = "12345678901234567890123456789012"
'암호화 문자열
Dim str As String = "암호화할 문자열입니다."
'암호화된 문자열
Dim result1 As String = AES.AES_encrypt(str, key)
'암호화된 문자열 출력
Console.WriteLine(result1)
'복호화된 문자열
Dim result2 As String = AES.AES_decrypt(result1, key)
'복호화된 문자열 출력
Console.WriteLine(result2)
End Sub
Private Class Security
Function AES_encrypt(ByVal input As String, ByVal key As String) As String
Dim aes As RijndaelManaged = 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}
Dim encrypt = aes.CreateEncryptor(aes.Key, aes.IV)
Dim xBuff As Byte() = Nothing
Using ms As MemoryStream = New MemoryStream()
Using cs As CryptoStream = New CryptoStream(ms, encrypt, CryptoStreamMode.Write)
Dim xXml As Byte() = Encoding.UTF8.GetBytes(input)
cs.Write(xXml, 0, xXml.Length)
End Using
xBuff = ms.ToArray()
End Using
Dim Output As String = Convert.ToBase64String(xBuff)
Return Output
End Function
Function AES_decrypt(ByVal input As String, ByVal key As String) As String
If input = "" Then Return ""
Dim aes As RijndaelManaged = 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}
Dim decrypt = aes.CreateDecryptor()
Dim xBuff As Byte() = Nothing
Using ms As MemoryStream = New MemoryStream()
Try
Using cs As CryptoStream = New CryptoStream(ms, decrypt, CryptoStreamMode.Write)
Dim xXml As Byte() = Convert.FromBase64String(input)
cs.Write(xXml, 0, xXml.Length)
End Using
Catch ex As Exception
End Try
xBuff = ms.ToArray()
End Using
If IsDBNull(xBuff) Then
Return ""
Else
Dim Output As String = Encoding.UTF8.GetString(xBuff)
Return Output.Trim()
End If
End Function
End Class
End Module