質問編集履歴
1
暗号化のソースを追記しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -22,17 +22,121 @@
|
|
22
22
|
|
23
23
|
{
|
24
24
|
|
25
|
+
string sfile = @"C:\test\enc.txt"; //暗号化前ファイル
|
26
|
+
|
25
27
|
string dfile = @"C:\test\dec.txt"; //暗号化ファイル
|
26
28
|
|
27
29
|
string efile = @"C:\test\out.txt"; //復号ファイル
|
28
30
|
|
29
31
|
string key = @"hoge"; //パスワード
|
30
32
|
|
33
|
+
AesEnc(sfile, dfile, key);
|
34
|
+
|
31
|
-
|
35
|
+
AesDec(dfile, efile, key);
|
32
36
|
|
33
37
|
}
|
34
38
|
|
35
|
-
|
39
|
+
//暗号化
|
40
|
+
|
41
|
+
static void AesEnc(string sFile, string dFile, string key)
|
42
|
+
|
43
|
+
{
|
44
|
+
|
45
|
+
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
aes.BlockSize = 128;
|
50
|
+
|
51
|
+
aes.KeySize = 128;
|
52
|
+
|
53
|
+
aes.Mode = CipherMode.CBC;
|
54
|
+
|
55
|
+
aes.Padding = PaddingMode.PKCS7;
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
aes.GenerateIV();
|
60
|
+
|
61
|
+
byte[] byteIV = aes.IV;
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
byte[] bytesPassword = Encoding.UTF8.GetBytes(key);
|
66
|
+
|
67
|
+
byte[] bytesKey = new byte[16];
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
for (int i = 0; i < 16; i++)
|
72
|
+
|
73
|
+
{
|
74
|
+
|
75
|
+
if (i < bytesPassword.Length)
|
76
|
+
|
77
|
+
{
|
78
|
+
|
79
|
+
bytesKey[i] = bytesPassword[i];
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
else
|
84
|
+
|
85
|
+
{
|
86
|
+
|
87
|
+
bytesKey[i] = 0; }
|
88
|
+
|
89
|
+
}
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
aes.Key = bytesKey;
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
ICryptoTransform encrypt = aes.CreateEncryptor();
|
98
|
+
|
99
|
+
FileStream outfs = new FileStream(dFile, FileMode.Create, FileAccess.Write);
|
100
|
+
|
101
|
+
outfs.Write(byteIV, 0, 16);
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
CryptoStream cs = new CryptoStream(outfs, encrypt, CryptoStreamMode.Write);
|
106
|
+
|
107
|
+
FileStream fs = new FileStream(sFile, FileMode.Open, FileAccess.Read);
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
byte[] buf = new byte[1024];
|
112
|
+
|
113
|
+
int len;
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
while ((len = fs.Read(buf, 0, buf.Length)) > 0)
|
118
|
+
|
119
|
+
{
|
120
|
+
|
121
|
+
cs.Write(buf, 0, len);
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
fs.Close();
|
128
|
+
|
129
|
+
cs.Close();
|
130
|
+
|
131
|
+
encrypt.Dispose();
|
132
|
+
|
133
|
+
outfs.Close();
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
//復号
|
36
140
|
|
37
141
|
static void AesDec(string sFile, string dFile, string key)
|
38
142
|
|