質問編集履歴

2

codeラベルを使用して、PHPとC#のソースを記述しました。

2018/10/11 01:20

投稿

fukkun0412
fukkun0412

スコア37

test CHANGED
File without changes
test CHANGED
@@ -100,6 +100,342 @@
100
100
 
101
101
 
102
102
 
103
+ C#でのXOR暗号化のソースは以下の物になります。
104
+
105
+ ```C#
106
+
107
+ using System;
108
+
109
+ using System.Collections.Generic;
110
+
111
+ using System.IO;
112
+
113
+ using System.Linq;
114
+
115
+ using System.Text;
116
+
117
+ using System.Threading.Tasks;
118
+
119
+ using System.Windows.Forms;
120
+
121
+
122
+
123
+ namespace updatebinfile
124
+
125
+ {
126
+
127
+ class Program
128
+
129
+ {
130
+
131
+ static string SQLPath = "/home/sql/";
132
+
133
+ static string binPath = "/home/sql/binfile/";
134
+
135
+ static private byte[] Byte_Key = { 0xB5, 0x1A, 0x3, 0xF3, 0x7F, 0x42, 0xD3 };
136
+
137
+ static int offset = 0;
138
+
139
+ static void Main(string[] args)
140
+
141
+ {
142
+
143
+ String[] FileName = Directory.GetFiles(SQLPath);
144
+
145
+ foreach(String file in FileName)
146
+
147
+ {
148
+
149
+ if (!file.Contains(".txt"))
150
+
151
+ {
152
+
153
+ String UpdateFile = Encrypt(file);
154
+
155
+ if(File.Exists(binPath + Path.GetFileName(UpdateFile)))
156
+
157
+ {
158
+
159
+ File.Delete(binPath + Path.GetFileName(UpdateFile));
160
+
161
+ }
162
+
163
+ File.Move(UpdateFile, binPath + Path.GetFileName(UpdateFile));
164
+
165
+ }
166
+
167
+ }
168
+
169
+ }
170
+
171
+
172
+
173
+ static private void CreateBinFile(String FileName)
174
+
175
+ {
176
+
177
+
178
+
179
+
180
+
181
+ }
182
+
183
+
184
+
185
+ static private String Encrypt(String FileName)
186
+
187
+ {
188
+
189
+ String file = System.String.Empty;
190
+
191
+ offset = 0;
192
+
193
+ //拡張子を判定
194
+
195
+ {
196
+
197
+ String[] split = FileName.Split('.');
198
+
199
+ String extension = "." + split[split.Length - 1];
200
+
201
+ file = ReplaceLast(FileName, extension, ".bin");
202
+
203
+ }
204
+
205
+
206
+
207
+ //読み込み開始
208
+
209
+ {
210
+
211
+ List<String> list = new List<String>();
212
+
213
+
214
+
215
+ using (System.IO.StreamReader cReader = new System.IO.StreamReader(FileName, new System.Text.UTF8Encoding(false)))
216
+
217
+ {
218
+
219
+
220
+
221
+ while (!cReader.EndOfStream)
222
+
223
+ {
224
+
225
+ list.Add(cReader.ReadLine());
226
+
227
+ }
228
+
229
+ }
230
+
231
+
232
+
233
+ {
234
+
235
+ //既に出力先に同名のファイルがある場合は削除しておく
236
+
237
+ System.IO.FileInfo cFileInfo = new System.IO.FileInfo(file);
238
+
239
+ // ファイルを削除する
240
+
241
+ cFileInfo.Delete();
242
+
243
+ }
244
+
245
+
246
+
247
+ using (System.IO.BinaryWriter write = new BinaryWriter(File.OpenWrite(file), new System.Text.UTF8Encoding(false)))
248
+
249
+ {
250
+
251
+ foreach (String str in list)
252
+
253
+ {
254
+
255
+ write.Write(XOR_EnCrypt(str));
256
+
257
+ write.Write(XOR_EnCrypt("\n"));
258
+
259
+ }
260
+
261
+ }
262
+
263
+
264
+
265
+ //書き込み終わったので、復号化してlistに入ってある要素と違いが無いかを確認する
266
+
267
+ {
268
+
269
+ String str = XOR_getDecryptString(file);
270
+
271
+ String[] split = str.Split('\n');
272
+
273
+ int str_offset = 0;
274
+
275
+ foreach (String line in list)
276
+
277
+ {
278
+
279
+ if (!line.Equals(split[str_offset++]))
280
+
281
+ {
282
+
283
+ MessageBox.Show("XOR暗号化エラー\n出力ファイルと元ファイルで差異があります\n" + line + "\n" + split[str_offset], "NotCheckUploadServer", MessageBoxButtons.OK, MessageBoxIcon.Error);
284
+
285
+ }
286
+
287
+ }
288
+
289
+ while (split.Length > str_offset)
290
+
291
+ {
292
+
293
+ String checkString = split[str_offset++];
294
+
295
+ if (checkString.Length != 0)
296
+
297
+ {
298
+
299
+ MessageBox.Show("XOR暗号化エラー\n出力ファイルと元ファイルで差異があります\n" + "[" + checkString + "]", "NotCheckUploadServer", MessageBoxButtons.OK, MessageBoxIcon.Error);
300
+
301
+ }
302
+
303
+ }
304
+
305
+ }
306
+
307
+
308
+
309
+ }
310
+
311
+ return file;
312
+
313
+
314
+
315
+ }
316
+
317
+
318
+
319
+ private static String ReplaceLast(String str, String key, String val)
320
+
321
+ {
322
+
323
+ int last = str.LastIndexOf(key);
324
+
325
+
326
+
327
+ if (last != -1)
328
+
329
+ {
330
+
331
+ //StringBuilderを作成する
332
+
333
+ System.Text.StringBuilder sb = new System.Text.StringBuilder(str);
334
+
335
+
336
+
337
+ sb.Replace(key, val, last, str.Length - last);
338
+
339
+
340
+
341
+ return sb.ToString();
342
+
343
+ }
344
+
345
+
346
+
347
+ return str;
348
+
349
+ }
350
+
351
+
352
+
353
+ private static byte[] XOR_EnCrypt(String text)
354
+
355
+ {
356
+
357
+ byte[] byte_text = new System.Text.UTF8Encoding(false).GetBytes(text);
358
+
359
+
360
+
361
+ for (int i = 0; i < byte_text.Length; i++)
362
+
363
+ {
364
+
365
+ byte_text[i] ^= Byte_Key[offset++ % Byte_Key.Length];
366
+
367
+ }
368
+
369
+ return byte_text;
370
+
371
+ }
372
+
373
+
374
+
375
+ private static string XOR_getDecryptString(String FileName)
376
+
377
+ {
378
+
379
+ offset = 0;
380
+
381
+ List<byte> list = new List<byte>();
382
+
383
+ try
384
+
385
+ {
386
+
387
+ using (System.IO.BinaryReader read = new BinaryReader(File.OpenRead(FileName)))
388
+
389
+ {
390
+
391
+ long length = read.BaseStream.Length;
392
+
393
+
394
+
395
+ for (long i = 0; i < length; ++i)
396
+
397
+ {
398
+
399
+ list.Add(XOR_Decrypt(read.ReadByte()));
400
+
401
+ }
402
+
403
+ }
404
+
405
+ }
406
+
407
+ catch (Exception e)
408
+
409
+ {
410
+
411
+ MessageBox.Show("エラーA:\n" + e.Message, "ERR");
412
+
413
+ }
414
+
415
+ return new System.Text.UTF8Encoding(false).GetString(list.ToArray());
416
+
417
+ }
418
+
419
+
420
+
421
+ private static byte XOR_Decrypt(byte text)
422
+
423
+ {
424
+
425
+ return (text ^= Byte_Key[offset++ % Byte_Key.Length]);
426
+
427
+ }
428
+
429
+ }
430
+
431
+ }
432
+
433
+
434
+
435
+ ```
436
+
437
+
438
+
103
439
  上記のPHPを動作させるとbinファイルは正常に作成され、データも同じ容量なのですが、WinMergeなどで見てみるとバイナリデータに大量の差分がありました。
104
440
 
105
441
 

1

2018/10/11 01:20

投稿

fukkun0412
fukkun0412

スコア37

test CHANGED
File without changes
test CHANGED
@@ -15,6 +15,10 @@
15
15
  上記のサイトを参考にして、以下のように実装いたしました。
16
16
 
17
17
 
18
+
19
+
20
+
21
+ ```PHP
18
22
 
19
23
  <?php
20
24
 
@@ -82,8 +86,6 @@
82
86
 
83
87
  $string[$i] = ($string[$i] ^ $key[$i % strlen($key)]);
84
88
 
85
- $string[$i] = ($string[$i] ^ $key[$i % strlen($key)]);
86
-
87
89
  }
88
90
 
89
91
  return $string;
@@ -92,7 +94,9 @@
92
94
 
93
95
 
94
96
 
97
+ ?
98
+
95
- ?>
99
+ ```
96
100
 
97
101
 
98
102