C#에서는 RijndaelManaged 객체와 CryptoStream 객체를 이용해 파일을 암호화 할 수 있습니다.
using System.IO;
using System.Security.Cryptography;
namespace test
{
class Program
{
static void Main()
{
int count, blockSizeBytes;
byte[] data;
RijndaelManaged aes = new RijndaelManaged();
ICryptoTransform encryptor;
CryptoStream cryptoStream;
FileStream inFs, outFs;
aes.KeySize = 256;
aes.BlockSize = 128;
aes.Padding = PaddingMode.PKCS7;
aes.Mode = CipherMode.CBC;
aes.GenerateKey();
aes.GenerateIV();
encryptor = aes.CreateEncryptor();
inFs = new FileStream("test.jpg", FileMode.Open);
outFs = new FileStream("test.jpg.encrypted", FileMode.Create);
cryptoStream = new CryptoStream(outFs, encryptor, CryptoStreamMode.Write);
blockSizeBytes = aes.BlockSize / 8;
data = new byte[blockSizeBytes];
do
{
count = inFs.Read(data, 0, blockSizeBytes);
cryptoStream.Write(data, 0, count);
}
while (count > 0);
inFs.Close();
cryptoStream.FlushFinalBlock();
cryptoStream.Close();
outFs.Close()
}
}
.
.
.
cryptoStream = new CryptoStream(inFs, encryptor, CryptoStreamMode.Read);
blockSizeBytes = aes.BlockSize / 8;
data = new byte[blockSizeBytes];
do
{
count = cryptoStream.Read(data, 0, blockSizeBytes);
outFs.Write(data, 0, count);
}
while (count > 0);
.
.
.
using System.IO;
using System.Security.Cryptography;
namespace test
{
class Program
{
static void Main()
{
int count, blockSizeBytes;
byte[] data;
RijndaelManaged aes = new RijndaelManaged();
ICryptoTransform decryptor;
CryptoStream cryptoStream;
FileStream inFs, outFs;
aes.KeySize = 256;
aes.BlockSize = 128;
aes.Padding = PaddingMode.PKCS7;
aes.Mode = CipherMode.CBC;
aes.key = [AES Key Bytes];
aes.iv = [AES IV Bytes];
encryptor = aes.CreateDecryptor();
inFs = new FileStream("test.jpg.encrypted", FileMode.Open);
outFs = new FileStream("test.jpg", FileMode.Create);
cryptoStream = new CryptoStream(outFs, decryptor, CryptoStreamMode.Write);
blockSizeBytes = aes.BlockSize / 8;
data = new byte[blockSizeBytes];
do
{
count = inFs.Read(data, 0, blockSizeBytes);
cryptoStream.Write(data, 0, count);
}
while (count > 0);
inFs.Close();
cryptoStream.FlushFinalBlock();
cryptoStream.Close();
outFs.Close()
}
}
using System.IO;
using System.Security.Cryptography;
namespace test
{
class Program
{
static void Main()
{
int count, blockSizeBytes;
byte[] data;
RijndaelManaged aes = new RijndaelManaged();
ICryptoTransform decryptor;
CryptoStream cryptoStream;
FileStream inFs, outFs;
aes.KeySize = 256;
aes.BlockSize = 128;
aes.Padding = PaddingMode.PKCS7;
aes.Mode = CipherMode.CBC;
key = ~~~
iv = ~~~
encryptor = aes.CreateDecryptor(key, iv);
inFs = new FileStream("test.jpg.encrypted", FileMode.Open);
outFs = new FileStream("test.jpg", FileMode.Create);
cryptoStream = new CryptoStream(outFs, decryptor, CryptoStreamMode.Write);
blockSizeBytes = aes.BlockSize / 8;
data = new byte[blockSizeBytes];
do
{
count = inFs.Read(data, 0, blockSizeBytes);
cryptoStream.Write(data, 0, count);
}
while (count > 0);
inFs.Close();
cryptoStream.FlushFinalBlock();
cryptoStream.Close();
outFs.Close()
}
}