teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

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

2019/09/25 07:27

投稿

BinaryNumber
BinaryNumber

スコア13

title CHANGED
File without changes
body CHANGED
@@ -2,11 +2,12 @@
2
2
  どうしても自分で解決できなかったので、質問させてください。
3
3
 
4
4
  ### 概要
5
- 外部にあるcsvファイルを暗号化し、Resources下に置した
5
+ 外部にあるcsvファイルを暗号化し、Resources下に置いてい
6
6
  そして、Unity上で復号化しようとすると、エラーになってしまいます。
7
7
 
8
8
  ### 詳細
9
- 外部にあるcsvファイルを暗号化して、UnityのAssets/Resourcesの中に置きました。ファイルで書き出ときに、書き出す内容を復号化関数で確認すると、問題なく復号化されます
9
+ 外部にあるcsvファイル(UTF8)を暗号化して、UnityのAssets/Resourcesの中に置きます。
10
+ ファイルで書き出すときに、書き出す内容を復号化関数で確認すると、問題なく復号化されます。
10
11
  Unityで作成しているゲーム内で、最初のSceneでResources下のファイルを読み込みます。ところが、この、読み込むとき、書き出すときに使用した関数とまったく同じコードを使っているのに、エラーが出てしまい、復号ができません。
11
12
  読み込みが失敗しているのかと内容を確認しましたが、読み込みはできているようでした(以下のコード内の encrypted を確認したらちゃんと読めているようです)。
12
13
 
@@ -23,12 +24,170 @@
23
24
  Visual Studio : 2019 Version 16.0.2
24
25
  これもまた、以前はVisual Studio2017を使っていたので、影響している可能性はあります。
25
26
 
27
+ ### コード
28
+
29
+ 暗号化部分
26
30
  ```C#
31
+ using System.Collections;
32
+ using System.Collections.Generic;
33
+ using UnityEngine;
34
+ using System.Text;
35
+ using System.IO;
36
+ using System;
37
+ using System.Security.Cryptography;
38
+
39
+ public class * : MonoBehaviour {
40
+
41
+ private string guitxt = "";
42
+ private byte[] encrypted;
43
+ private string plain = "";
44
+
45
+
46
+ // Use this for initialization
47
+ void Start () {
48
+ string filename1 = Application.dataPath + "/../../test.csv";
49
+
50
+ if (File.Exists(filename1))
51
+ {
52
+ ReadFile(filename1);
53
+
54
+ string filename2 = Application.dataPath + "/Resources/test.csv";
55
+
56
+ if (File.Exists(filename2))
57
+ {
58
+ Encrypt();
59
+ File.WriteAllBytes(filename2, encrypted);
60
+ Debug.Log("暗号:" + encrypted);
61
+
62
+ Decode();
63
+ Debug.Log("復号:" + plain);
64
+ }
65
+ }
66
+ }
67
+
68
+ void ReadFile(string filename)
69
+ {
70
+ FileInfo fi = new FileInfo(filename);
71
+
72
+ try
73
+ {
74
+ // 一行毎読み込み
75
+ using (StreamReader sr = new StreamReader(fi.OpenRead(), Encoding.UTF8))
76
+ {
77
+ guitxt = sr.ReadToEnd();
78
+ }
79
+ }
80
+ catch (Exception e)
81
+ {
82
+ // 改行コード
83
+ guitxt += SetDefaultText();
84
+ }
85
+
86
+ }
87
+
88
+ // 改行コード処理
89
+ string SetDefaultText()
90
+ {
91
+ return "C#あ\n";
92
+ }
93
+
94
+ // 暗号化処理
95
+ void Encrypt()
96
+ {
97
+ RijndaelManaged rijndael = new RijndaelManaged();
98
+ rijndael.KeySize = 128;
99
+ rijndael.BlockSize = 128;
100
+
101
+ // pw, vectorの内容はダミーで、実際には英数字で構成されています
102
+ string pw = "abcdefghijklmnop";
103
+ string vector = "0123456789012345";
104
+
105
+ byte[] bSalt = Encoding.UTF8.GetBytes(vector);
106
+ Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(pw, bSalt);
107
+ deriveBytes.IterationCount = 1000; // 反復回数
108
+
109
+ rijndael.Key = deriveBytes.GetBytes(rijndael.KeySize / 8);
110
+ rijndael.IV = deriveBytes.GetBytes(rijndael.BlockSize / 8);
111
+
112
+ // 暗号化
113
+ ICryptoTransform encryptor = rijndael.CreateEncryptor();
114
+ byte[] src = Encoding.UTF8.GetBytes(guitxt);
115
+ encrypted = encryptor.TransformFinalBlock(src, 0, src.Length);
116
+
117
+ encryptor.Dispose();
118
+ }
119
+
120
+ void Decode()
121
+ {
122
+ RijndaelManaged rijndael = new RijndaelManaged();
123
+ rijndael.KeySize = 128;
124
+ rijndael.BlockSize = 128;
125
+
126
+ // パスワードから共有キーと初期化ベクターを作成
127
+ string pw = "abcdefghijklmnop";
128
+ string vector = "0123456789012345";
129
+
130
+ byte[] bSalt = Encoding.UTF8.GetBytes(vector);
131
+ Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(pw, bSalt);
132
+ deriveBytes.IterationCount = 1000; // 反復回数
133
+
134
+ rijndael.Key = deriveBytes.GetBytes(rijndael.KeySize / 8);
135
+ rijndael.IV = deriveBytes.GetBytes(rijndael.BlockSize / 8);
136
+
137
+ // 復号化
138
+ ICryptoTransform decryptor = rijndael.CreateDecryptor();
139
+ byte[] src = decryptor.TransformFinalBlock(encrypted, 0, encrypted.Length);
140
+
141
+ Debug.Log("復号化: " + src);
142
+
143
+ plain = Encoding.UTF8.GetString(src);
144
+
145
+ decryptor.Dispose();
146
+
147
+ }
148
+ }
149
+ ```
150
+
151
+ 復号化部分
152
+ ```C#
153
+ using System.Collections;
154
+ using System.Collections.Generic;
155
+ using System.IO;
156
+ using System.Linq;
157
+ using System.Security.Cryptography;
158
+ using System.Text;
159
+ using UnityEngine;
160
+
161
+ public class * : MonoBehaviour
162
+ {
27
163
  private static byte[] encrypted;
28
164
  private static string plain = "";
29
165
 
30
- (省略)
166
+ // ファイル名
167
+ private const string FILENAME = "test";
31
168
 
169
+ // 問題文のcsvファイル
170
+ private static TextAsset csvFile;
171
+
172
+ [RuntimeInitializeOnLoadMethod()]
173
+ static void Init()
174
+ {
175
+ #if UNITY_EDITOR
176
+ if (UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode == false) { return; }
177
+ #endif
178
+
179
+ // ファイルを読み込む
180
+ csvFile = Resources.Load(FILENAME) as TextAsset;
181
+
182
+ // 復号化
183
+ encrypted = csvFile.bytes;
184
+ Decode();
185
+
186
+ // 以後の処理
187
+ // (省略)
188
+ }
189
+
190
+
32
191
  static void Decode()
33
192
  {
34
193
  RijndaelManaged rijndael = new RijndaelManaged();
@@ -36,7 +195,6 @@
36
195
  rijndael.BlockSize = 128;
37
196
 
38
197
  // パスワードから共有キーと初期化ベクターを作成
39
- // pw, vectorの内容はダミーで、実際には英数字で構成されています
40
198
  string pw = "abcdefghijklmnop";
41
199
  string vector = "0123456789012345";
42
200