using System;
using System.Text;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Paddings;
using Org.BouncyCastle.Crypto.Parameters;
class Program
{
static void Main()
{
string plainText = "Hello, ARIA 256!";
string keyHex = "00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF"; // 256-bit 키 (32바이트)
byte[] key = HexStringToByteArray(keyHex);
byte[] encrypted = AriaEncrypt(plainText, key);
string encryptedHex = BitConverter.ToString(encrypted).Replace("-", "");
Console.WriteLine($"Encrypted: {encryptedHex}");
byte[] decrypted = AriaDecrypt(encrypted, key);
string decryptedText = Encoding.UTF8.GetString(decrypted);
Console.WriteLine($"Decrypted: {decryptedText}");
}
static byte[] AriaEncrypt(string plainText, byte[] key)
{
IBlockCipher engine = new AriaEngine();
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(engine);
KeyParameter keyParam = new KeyParameter(key);
cipher.Init(true, keyParam);
byte[] inputBytes = Encoding.UTF8.GetBytes(plainText);
return ProcessCipher(cipher, inputBytes);
}
static byte[] AriaDecrypt(byte[] cipherText, byte[] key)
{
IBlockCipher engine = new AriaEngine();
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(engine);
KeyParameter keyParam = new KeyParameter(key);
cipher.Init(false, keyParam);
return ProcessCipher(cipher, cipherText);
}
static byte[] ProcessCipher(PaddedBufferedBlockCipher cipher, byte[] input)
{
byte[] output = new byte[cipher.GetOutputSize(input.Length)];
int length = cipher.ProcessBytes(input, 0, input.Length, output, 0);
length += cipher.DoFinal(output, length);
Array.Resize(ref output, length);
return output;
}
static byte[] HexStringToByteArray(string hex)
{
int length = hex.Length / 2;
byte[] bytes = new byte[length];
for (int i = 0; i < length; i++)
{
bytes[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
}
return bytes;
}
}