下記のコードをmyenc.cs というファイル名で保存
using System; using System.IO; using System.Text; using System.Security.Cryptography; public class MyEncrypt { static void DoIt(string dir) { Directory.CreateDirectory(@"result"); string[] files = Directory.GetFiles(dir); foreach (string fromFile in files) { FileStream infs = new FileStream(fromFile, FileMode.Open); StreamReader reader = new StreamReader(infs); string input = reader.ReadToEnd(); reader.Close(); infs.Close(); FileStream outfs = new FileStream(fromFile + ".enc", FileMode.Create); StreamWriter writer = new StreamWriter(outfs); writer.WriteLine(Encrypt(input)); writer.Close(); outfs.Close(); FileStream fstream = new FileStream(fromFile + ".enc", FileMode.Open); StreamReader freader = new StreamReader(fstream); string result = freader.ReadToEnd(); Console.WriteLine(Decrypt(result)); freader.Close(); fstream.Close(); } } static void Main(string[] args) { DoIt(@".\source"); } private const string AesIV = @"PBAErCkB4N5tnJ4="; private const string AesKey = @"oSgbUTlzlR5Rsza="; static string Encrypt(string text) { AesCryptoServiceProvider aes = new AesCryptoServiceProvider(); aes.BlockSize = 128; aes.KeySize = 128; aes.IV = Encoding.UTF8.GetBytes(AesIV); aes.Key = Encoding.UTF8.GetBytes(AesKey); aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; byte[] src = Encoding.Unicode.GetBytes(text); using (ICryptoTransform encrypt = aes.CreateEncryptor()) { byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length); return Convert.ToBase64String(dest); } } static string Decrypt(string text) { AesCryptoServiceProvider aes = new AesCryptoServiceProvider(); aes.BlockSize = 128; aes.KeySize = 128; aes.IV = Encoding.UTF8.GetBytes(AesIV); aes.Key = Encoding.UTF8.GetBytes(AesKey); aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; byte[] src = System.Convert.FromBase64String(text); using (ICryptoTransform decrypt = aes.CreateDecryptor()) { byte[] dest = decrypt.TransformFinalBlock(src, 0, src.Length); return Encoding.Unicode.GetString(dest); } } }
Windows7にインストールされているCSCでコンパイル
set PATH=C:\Windows\Microsoft.NET\Framework\v3.5;%PATH% csc myenc.cs