ファイルを暗号化するライブラリを自作してみました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
namespace NamespaceEncodeFiles { public class EncodeFiles { private const int KEY_SIZE = 256; private const int BLOCK_SIZE = 128; private const int BUFFER_SIZE = BLOCK_SIZE * 32; // バッファーサイズはBlockSizeの倍数にする private const int SALT_SIZE = 16; // 8以上 static private byte[] GetSalt() { Point pt = Control.MousePosition; long nowTicks = DateTime.Now.Ticks; string str = String.Format("{0}{1}{2}", nowTicks, pt.X, pt.Y); Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(str, SALT_SIZE); return deriveBytes.Salt; } //パスワードから暗号キーを作成する static private byte[] GetKeyFromPassword(string password, byte[] salt) { //Rfc2898DeriveBytesオブジェクトを作成する Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(password, salt); //反復処理回数を指定する 1000回 deriveBytes.IterationCount = 1000; //キーを生成する return deriveBytes.GetBytes(KEY_SIZE / 8); // KEY_SIZEはビット数なので8で割る } // 前処理用のソルト static private byte[] GetAnotherSalt() { long nowTicks = DateTime.Now.Ticks; Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(nowTicks.ToString(), SALT_SIZE); return deriveBytes.Salt; } //パスワードから前処理用の暗号キーを作成する static private byte[] GetAnotherKeyFromPassword(string password, byte[] salt) { //Rfc2898DeriveBytesオブジェクトを作成する Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(password, salt); //反復処理回数を指定する 1000回 deriveBytes.IterationCount = 1000; //キーを生成する return deriveBytes.GetBytes(BUFFER_SIZE); } //パスワードから初期化ベクトルを作成する static private byte[] GetIVFromPassword(string password) { Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(password, GetSalt()); //反復処理回数を不定にする 1000回+α deriveBytes.IterationCount = 1000 + DateTime.Now.Millisecond; return deriveBytes.GetBytes(BLOCK_SIZE / 8); } static byte[] GetEncodeFileInfo(string srcFilePath, byte[] key) { // ファイル情報を取得する FileInfo info = new FileInfo(srcFilePath); long creationTime = info.CreationTime.Ticks; long lastAccessTime = info.LastAccessTime.Ticks; long lastWriteTime = info.LastWriteTime.Ticks; string name = info.Name; byte[] vs = Encoding.UTF8.GetBytes(name); int len = vs.Length; // ファイル情報を書き込む MemoryStream ms = new MemoryStream(); ms.Write(BitConverter.GetBytes(creationTime), 0, sizeof(long)); ms.Write(BitConverter.GetBytes(lastAccessTime), 0, sizeof(long)); ms.Write(BitConverter.GetBytes(lastWriteTime), 0, sizeof(long)); ms.Write(BitConverter.GetBytes(len), 0, sizeof(int)); ms.Write(vs, 0, len); byte[] fileInfoBuf = ms.ToArray(); for(int i = 0; i < fileInfoBuf.Length; i++) fileInfoBuf[i] ^= key[i]; return fileInfoBuf; } static public void EncodeFile(string srcFilePath, string destFilePath, string password) { // Saltを生成する byte[] salt = GetSalt(); byte[] key = GetKeyFromPassword(password, salt); byte[] iv = GetIVFromPassword(password); using(AesManaged aes = new AesManaged()) { // AESインスタンスのパラメータ設定 aes.KeySize = KEY_SIZE; aes.BlockSize = BLOCK_SIZE; aes.Mode = CipherMode.CBC; aes.Key = key; aes.IV = iv; aes.Padding = PaddingMode.ISO10126; // 暗号化オブジェクト生成 ICryptoTransform ct = aes.CreateEncryptor(aes.Key, aes.IV); // 出力ファイルストリーム using(FileStream outFileStream = new FileStream(destFilePath, FileMode.Create, FileAccess.Write)) { // Saltの書き込み outFileStream.Write(salt, 0, SALT_SIZE); // 初期化ベクトル 書き込み outFileStream.Write(aes.IV, 0, BLOCK_SIZE / 8); using(CryptoStream cryptoStream = new CryptoStream(outFileStream, ct, CryptoStreamMode.Write)) { using(FileStream inFileStream = new FileStream(srcFilePath, FileMode.Open, FileAccess.Read)) { byte[] anotherSolt = GetAnotherSalt(); byte[] anotherKey = GetAnotherKeyFromPassword(password, anotherSolt); cryptoStream.Write(anotherSolt, 0, anotherSolt.Length); // ファイル情報を暗号化して書き込む byte[] fileInfoBuf = GetEncodeFileInfo(srcFilePath, anotherKey); cryptoStream.Write(fileInfoBuf, 0, fileInfoBuf.Length); byte[] buf = new byte[BUFFER_SIZE]; while(true) { int size = inFileStream.Read(buf, 0, buf.Length); // ストリーム暗号で一旦暗号化 for(int i = 0; i < size; i++) buf[i] ^= anotherKey[i]; if(size == 0) break; // 出力ファイルへ書き込み(AESで暗号化される) cryptoStream.Write(buf, 0, size); } } } } } } public class DecodeFileInfo { public long CreationTime = 0; public long LastAccessTime = 0; public long LastWriteTime = 0; public string FileName = ""; } static DecodeFileInfo GetDeccodeFileInfo(CryptoStream cryptoStream, byte[] key) { DecodeFileInfo decodeFileInfo = new DecodeFileInfo(); MemoryStream ms = new MemoryStream(); // ファイル情報部分を読み込む byte[] longbs = new byte[sizeof(long)]; byte[] intbs = new byte[sizeof(int)]; int readByte = 0; // CryptoStreamからの読み出し // CreationTime readByte += cryptoStream.Read(longbs, 0, sizeof(long)); ms.Write(longbs, 0, sizeof(long)); // LastAccessTime readByte += cryptoStream.Read(longbs, 0, sizeof(long)); ms.Write(longbs, 0, sizeof(long)); // LastWriteTime readByte += cryptoStream.Read(longbs, 0, sizeof(long)); ms.Write(longbs, 0, sizeof(long)); // ファイル名の長さ readByte += cryptoStream.Read(intbs, 0, sizeof(int)); ms.Write(intbs, 0, sizeof(int)); // 読み出したデータを復号 byte[] fileInfoBuf = ms.ToArray(); for(int i = 0; i < fileInfoBuf.Length; i++) fileInfoBuf[i] ^= key[i]; ms = new MemoryStream(fileInfoBuf); ms.Read(longbs, 0, sizeof(long)); decodeFileInfo.CreationTime = BitConverter.ToInt64(longbs, 0); ms.Read(longbs, 0, sizeof(long)); decodeFileInfo.LastAccessTime = BitConverter.ToInt64(longbs, 0); ms.Read(longbs, 0, sizeof(long)); decodeFileInfo.LastWriteTime = BitConverter.ToInt64(longbs, 0); ms.Read(intbs, 0, sizeof(int)); int len = BitConverter.ToInt32(intbs, 0); // ファイル名はここで復号する byte[] vs = new byte[len]; cryptoStream.Read(vs, 0, len); ms = new MemoryStream(vs); byte[] fileName = ms.ToArray(); for(int i = 0; i < fileName.Length; i++) fileName[i] ^= key[i + readByte]; decodeFileInfo.FileName = Encoding.UTF8.GetString(fileName); return decodeFileInfo; } static public bool DecodeFile(string srcFilePath, string destFolderPath, string password) { try { DecodeFile2(srcFilePath, destFolderPath, password); return true; } catch { return false; } } static void DecodeFile2(string srcFilePath, string destFolderPath, string password) { using(AesManaged aes = new AesManaged()) { // AESインスタンスのパラメータ設定 aes.KeySize = KEY_SIZE; aes.BlockSize = BLOCK_SIZE; aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.ISO10126; using(FileStream inFileStream = new FileStream(srcFilePath, FileMode.Open, FileAccess.Read)) { // Saltを読み込む byte[] salt = GetSalt(); inFileStream.Read(salt, 0, SALT_SIZE); byte[] key = GetKeyFromPassword(password, salt); aes.Key = key; // 初期化ベクトル読込 byte[] iv = new byte[BLOCK_SIZE / 8]; inFileStream.Read(iv, 0, iv.Length); aes.IV = iv; // 復号化オブジェクト生成 ICryptoTransform ct = aes.CreateDecryptor(aes.Key, aes.IV); using(CryptoStream cryptoStream = new CryptoStream(inFileStream, ct, CryptoStreamMode.Read)) { if(!Directory.Exists(destFolderPath)) Directory.CreateDirectory(destFolderPath); string tempFilePath = destFolderPath + "\\temp.temp"; string destFilePath = ""; DecodeFileInfo info1 = null; using(FileStream outFs = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write)) { byte[] anotherSolt = new byte[SALT_SIZE]; cryptoStream.Read(anotherSolt, 0, SALT_SIZE); byte[] anotherKey = GetAnotherKeyFromPassword(password, anotherSolt); // ファイル情報部分を読み込む info1 = GetDeccodeFileInfo(cryptoStream, anotherKey); destFilePath = destFolderPath + "\\" + info1.FileName; if(File.Exists(destFilePath)) { MessageBox.Show("復号しようとしているファイルと同名のファイルがあるので処理ができません。"); return; } byte[] buf = new byte[BUFFER_SIZE]; while(true) { int size = cryptoStream.Read(buf, 0, buf.Length); for(int i = 0; i < size; i++) buf[i] ^= anotherKey[i]; if(size == 0) break; outFs.Write(buf, 0, size); } } // ファイル情報を復元する File.Move(tempFilePath, destFilePath); FileInfo info = new FileInfo(destFilePath); info.CreationTime = new DateTime(info1.CreationTime); info.LastAccessTime = new DateTime(info1.LastAccessTime); info.LastWriteTime = new DateTime(info1.LastWriteTime); } } } } } } |