質問編集履歴

1

2019.09.25 16:22 ご指摘ありがとうございます。 暗号化部分のコードと、復号化の際の読み込み部分を追加してみましたが、これで足りますでしょうか。 もし、まだ足りないなら再度ご指摘いただけ

2019/09/25 07:27

投稿

BinaryNumber
BinaryNumber

スコア13

test CHANGED
File without changes
test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  ### 概要
8
8
 
9
- 外部にあるcsvファイルを暗号化し、Resources下に置した
9
+ 外部にあるcsvファイルを暗号化し、Resources下に置いてい
10
10
 
11
11
  そして、Unity上で復号化しようとすると、エラーになってしまいます。
12
12
 
@@ -14,7 +14,9 @@
14
14
 
15
15
  ### 詳細
16
16
 
17
- 外部にあるcsvファイルを暗号化して、UnityのAssets/Resourcesの中に置きました。ファイルで書き出ときに、書き出す内容を復号化関数で確認すると、問題なく復号化されます
17
+ 外部にあるcsvファイル(UTF8)を暗号化して、UnityのAssets/Resourcesの中に置きます。
18
+
19
+ ファイルで書き出すときに、書き出す内容を復号化関数で確認すると、問題なく復号化されます。
18
20
 
19
21
  Unityで作成しているゲーム内で、最初のSceneでResources下のファイルを読み込みます。ところが、この、読み込むとき、書き出すときに使用した関数とまったく同じコードを使っているのに、エラーが出てしまい、復号ができません。
20
22
 
@@ -48,15 +50,331 @@
48
50
 
49
51
 
50
52
 
53
+ ### コード
54
+
55
+
56
+
57
+ 暗号化部分
58
+
51
59
  ```C#
52
60
 
61
+ using System.Collections;
62
+
63
+ using System.Collections.Generic;
64
+
65
+ using UnityEngine;
66
+
67
+ using System.Text;
68
+
69
+ using System.IO;
70
+
71
+ using System;
72
+
73
+ using System.Security.Cryptography;
74
+
75
+
76
+
77
+ public class * : MonoBehaviour {
78
+
79
+
80
+
81
+ private string guitxt = "";
82
+
83
+ private byte[] encrypted;
84
+
85
+ private string plain = "";
86
+
87
+
88
+
89
+
90
+
91
+ // Use this for initialization
92
+
93
+ void Start () {
94
+
95
+ string filename1 = Application.dataPath + "/../../test.csv";
96
+
97
+
98
+
99
+ if (File.Exists(filename1))
100
+
101
+ {
102
+
103
+ ReadFile(filename1);
104
+
105
+
106
+
107
+ string filename2 = Application.dataPath + "/Resources/test.csv";
108
+
109
+
110
+
111
+ if (File.Exists(filename2))
112
+
113
+ {
114
+
115
+ Encrypt();
116
+
117
+ File.WriteAllBytes(filename2, encrypted);
118
+
119
+ Debug.Log("暗号:" + encrypted);
120
+
121
+
122
+
123
+ Decode();
124
+
125
+ Debug.Log("復号:" + plain);
126
+
127
+ }
128
+
129
+ }
130
+
131
+ }
132
+
133
+
134
+
135
+ void ReadFile(string filename)
136
+
137
+ {
138
+
139
+ FileInfo fi = new FileInfo(filename);
140
+
141
+
142
+
143
+ try
144
+
145
+ {
146
+
147
+ // 一行毎読み込み
148
+
149
+ using (StreamReader sr = new StreamReader(fi.OpenRead(), Encoding.UTF8))
150
+
151
+ {
152
+
153
+ guitxt = sr.ReadToEnd();
154
+
155
+ }
156
+
157
+ }
158
+
159
+ catch (Exception e)
160
+
161
+ {
162
+
163
+ // 改行コード
164
+
165
+ guitxt += SetDefaultText();
166
+
167
+ }
168
+
169
+
170
+
171
+ }
172
+
173
+
174
+
175
+ // 改行コード処理
176
+
177
+ string SetDefaultText()
178
+
179
+ {
180
+
181
+ return "C#あ\n";
182
+
183
+ }
184
+
185
+
186
+
187
+ // 暗号化処理
188
+
189
+ void Encrypt()
190
+
191
+ {
192
+
193
+ RijndaelManaged rijndael = new RijndaelManaged();
194
+
195
+ rijndael.KeySize = 128;
196
+
197
+ rijndael.BlockSize = 128;
198
+
199
+
200
+
201
+ // pw, vectorの内容はダミーで、実際には英数字で構成されています
202
+
203
+ string pw = "abcdefghijklmnop";
204
+
205
+ string vector = "0123456789012345";
206
+
207
+
208
+
209
+ byte[] bSalt = Encoding.UTF8.GetBytes(vector);
210
+
211
+ Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(pw, bSalt);
212
+
213
+ deriveBytes.IterationCount = 1000; // 反復回数
214
+
215
+
216
+
217
+ rijndael.Key = deriveBytes.GetBytes(rijndael.KeySize / 8);
218
+
219
+ rijndael.IV = deriveBytes.GetBytes(rijndael.BlockSize / 8);
220
+
221
+
222
+
223
+ // 暗号化
224
+
225
+ ICryptoTransform encryptor = rijndael.CreateEncryptor();
226
+
227
+ byte[] src = Encoding.UTF8.GetBytes(guitxt);
228
+
229
+ encrypted = encryptor.TransformFinalBlock(src, 0, src.Length);
230
+
231
+
232
+
233
+ encryptor.Dispose();
234
+
235
+ }
236
+
237
+
238
+
239
+ void Decode()
240
+
241
+ {
242
+
243
+ RijndaelManaged rijndael = new RijndaelManaged();
244
+
245
+ rijndael.KeySize = 128;
246
+
247
+ rijndael.BlockSize = 128;
248
+
249
+
250
+
251
+ // パスワードから共有キーと初期化ベクターを作成
252
+
253
+ string pw = "abcdefghijklmnop";
254
+
255
+ string vector = "0123456789012345";
256
+
257
+
258
+
259
+ byte[] bSalt = Encoding.UTF8.GetBytes(vector);
260
+
261
+ Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(pw, bSalt);
262
+
263
+ deriveBytes.IterationCount = 1000; // 反復回数
264
+
265
+
266
+
267
+ rijndael.Key = deriveBytes.GetBytes(rijndael.KeySize / 8);
268
+
269
+ rijndael.IV = deriveBytes.GetBytes(rijndael.BlockSize / 8);
270
+
271
+
272
+
273
+ // 復号化
274
+
275
+ ICryptoTransform decryptor = rijndael.CreateDecryptor();
276
+
277
+ byte[] src = decryptor.TransformFinalBlock(encrypted, 0, encrypted.Length);
278
+
279
+
280
+
281
+ Debug.Log("復号化: " + src);
282
+
283
+
284
+
285
+ plain = Encoding.UTF8.GetString(src);
286
+
287
+
288
+
289
+ decryptor.Dispose();
290
+
291
+
292
+
293
+ }
294
+
295
+ }
296
+
297
+ ```
298
+
299
+
300
+
301
+ 復号化部分
302
+
303
+ ```C#
304
+
305
+ using System.Collections;
306
+
307
+ using System.Collections.Generic;
308
+
309
+ using System.IO;
310
+
311
+ using System.Linq;
312
+
313
+ using System.Security.Cryptography;
314
+
315
+ using System.Text;
316
+
317
+ using UnityEngine;
318
+
319
+
320
+
321
+ public class * : MonoBehaviour
322
+
323
+ {
324
+
53
325
  private static byte[] encrypted;
54
326
 
55
327
  private static string plain = "";
56
328
 
57
329
 
58
330
 
331
+ // ファイル名
332
+
333
+ private const string FILENAME = "test";
334
+
335
+
336
+
337
+ // 問題文のcsvファイル
338
+
339
+ private static TextAsset csvFile;
340
+
341
+
342
+
343
+ [RuntimeInitializeOnLoadMethod()]
344
+
345
+ static void Init()
346
+
347
+ {
348
+
349
+ #if UNITY_EDITOR
350
+
351
+ if (UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode == false) { return; }
352
+
59
- (省略)
353
+ #endif
354
+
355
+
356
+
357
+ // ファイルを読み込む
358
+
359
+ csvFile = Resources.Load(FILENAME) as TextAsset;
360
+
361
+
362
+
363
+ // 復号化
364
+
365
+ encrypted = csvFile.bytes;
366
+
367
+ Decode();
368
+
369
+
370
+
371
+ // 以後の処理
372
+
373
+ // (省略)
374
+
375
+ }
376
+
377
+
60
378
 
61
379
 
62
380
 
@@ -74,8 +392,6 @@
74
392
 
75
393
  // パスワードから共有キーと初期化ベクターを作成
76
394
 
77
- // pw, vectorの内容はダミーで、実際には英数字で構成されています
78
-
79
395
  string pw = "abcdefghijklmnop";
80
396
 
81
397
  string vector = "0123456789012345";