テラシュールブログ

旧テラシュールウェアブログUnity記事。主にUnityのTipsやAR・VR、ニコニコ動画についてのメモを残します。

【Unity】UnityでAES暗号を使うアセット AesEncryptor

何やら暗号化の話でTLが盛り上がってたので、すこしAssetStoreを探した処、面白いアセットが見つかりました。

f:id:tsubaki_t1:20150806004118p:plain

AES Encryptor

 

機能は凄い単純で、AvoEx.AesEncryptor.Encryptで暗号化、 AvoEx.AesEncryptor.Decryptで復号化です。

string orgText = "コナン君しめやかに爆発四散";

// 暗号化

string encText = AvoEx.AesEncryptor.Encrypt(orgText);

Debug.Log("encText = " + encText);

// 復号化

string decText = AvoEx.AesEncryptor.Decrypt(encText);

Debug.Log("decText = " + decText);

f:id:tsubaki_t1:20150806004740p:plain

IV(Initial Vector:初期ベクトル)を使用したい場合、AesEncryptor.GenerateIV(); が用意されているので、そこから取得して暗号化・復号化時に指定してやればOKみたいです。

var inputValue = "たたフたーリたたンたカザたン"; // たぬき; 

var customVector = AesEncryptor.GenerateIV();  string withIV = AesEncryptor.EncryptIV(inputValue, customVector);

string withoutIV = AesEncryptor.Encrypt(inputValue); 

Debug.LogFormat("WITHIV  : {0}", withIV);

Debug.LogFormat("WITHOUT : {0}", withoutIV);

f:id:tsubaki_t1:20150806005934p:plain

 IVの長さや、キーサイズ、あとキー文字列はAesEncryptor.csで書き換えるみたいです。この辺りの項目は、暗号を使用する上では必ず書き換えたほうが良いです。ただ、リリース後に変更出来ないので慎重にとの事。

 

なお、この機能の中々に良い所は、色々なフォーマットに対応している点です。サイトによると、以下のフォーマットに対応しているみたいです。スクリプトからの呼び出しも簡単で、キャスト要らずの便利加減です。

- byte[]
- bool
- char
- double
- float
- int
- long
- short
- uint
- ulong
- ushort

f:id:tsubaki_t1:20150806010615p:plain

参考

AES暗号のまとめ - Web就活日記