Last active
August 29, 2015 14:08
-
-
Save kankikuchi/b92a534c3ac99fa8d342 to your computer and use it in GitHub Desktop.
Unity、ZDatabase改良案
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public static class Encryption { | |
//================================================================================= | |
//保存 | |
//================================================================================= | |
public static void SaveInt(string key, int value){ | |
ZDatabase.instance.WriteEnc (EncryptKey(key), value); | |
} | |
public static void SaveFloat(string key, float value){ | |
ZDatabase.instance.WriteEnc (EncryptKey(key), value); | |
} | |
public static void SaveBool(string key, bool value){ | |
ZDatabase.instance.WriteEnc (EncryptKey(key), value); | |
} | |
public static void SaveString(string key, string value){ | |
ZDatabase.instance.WriteEnc (EncryptKey(key), value); | |
} | |
public static void SaveVector3(string key, Vector3 value){ | |
ZDatabase.instance.WriteEnc (EncryptKey(key), value); | |
} | |
public static void SaveQuaternion(string key, Quaternion value){ | |
Vector3 vec3 = value.eulerAngles; | |
ZDatabase.instance.WriteEnc (EncryptKey(key), vec3); | |
} | |
//================================================================================= | |
//読み込み | |
//================================================================================= | |
public static int LoadInt(string key, int defult){ | |
return ZDatabase.instance.ReadEncInt (EncryptKey(key), defult); | |
} | |
public static float LoadFloat(string key, float defult){ | |
return ZDatabase.instance.ReadEncFloat (EncryptKey(key), defult); | |
} | |
public static bool LoadBool(string key, bool defult){ | |
return ZDatabase.instance.ReadEncBool (EncryptKey(key), defult); | |
} | |
public static string LoadString(string key, string defult){ | |
string loadStr = ZDatabase.instance.ReadEncString (EncryptKey(key)); | |
if(string.IsNullOrEmpty(loadStr)){ | |
return defult; | |
} | |
return loadStr; | |
} | |
public static Vector3 LoadVector3(string key, Vector3 defult){ | |
Vector3 loadVector3 = ZDatabase.instance.ReadEncVector3 (EncryptKey(key)); | |
if(Vector3.Distance(loadVector3, Vector3.zero) == 0){ | |
return defult; | |
} | |
return loadVector3; | |
} | |
public static Quaternion LoadQuaternion(string key, Quaternion defult){ | |
Vector3 loadVector3 = ZDatabase.instance.ReadEncVector3 (EncryptKey(key)); | |
if(Vector3.Distance(loadVector3, Vector3.zero) == 0){ | |
return defult; | |
} | |
Quaternion qua = Quaternion.Euler (loadVector3); | |
return qua; | |
} | |
//================================================================================= | |
//Key暗号化 | |
//================================================================================= | |
private const string PASS = "t9w5z3r5ggbuytzbghca"; | |
private static string EncryptKey(string sourceString) | |
{ | |
//RijndaelManagedオブジェクトを作成 | |
System.Security.Cryptography.RijndaelManaged rijndael = | |
new System.Security.Cryptography.RijndaelManaged(); | |
//パスワードから共有キーと初期化ベクタを作成 | |
byte[] key, iv; | |
GenerateKeyFromPassword( | |
rijndael.KeySize, out key, rijndael.BlockSize, out iv); | |
rijndael.Key = key; | |
rijndael.IV = iv; | |
//文字列をバイト型配列に変換する | |
byte[] strBytes = System.Text.Encoding.UTF8.GetBytes(sourceString); | |
//対称暗号化オブジェクトの作成 | |
System.Security.Cryptography.ICryptoTransform encryptor = | |
rijndael.CreateEncryptor(); | |
//バイト型配列を暗号化する | |
byte[] encBytes = encryptor.TransformFinalBlock(strBytes, 0, strBytes.Length); | |
//閉じる | |
encryptor.Dispose(); | |
//バイト型配列を文字列に変換して返す | |
return System.Convert.ToBase64String(encBytes); | |
} | |
private static void GenerateKeyFromPassword( int keySize, out byte[] key, int blockSize, out byte[] iv) | |
{ | |
//パスワードから共有キーと初期化ベクタを作成する | |
//saltを決める | |
byte[] salt = System.Text.Encoding.UTF8.GetBytes("saltは必ず8バイト以上"); | |
//Rfc2898DeriveBytesオブジェクトを作成する | |
System.Security.Cryptography.Rfc2898DeriveBytes deriveBytes = | |
new System.Security.Cryptography.Rfc2898DeriveBytes(PASS, salt); | |
//反復処理回数を指定する デフォルトで1000回 | |
deriveBytes.IterationCount = 1000; | |
//共有キーと初期化ベクタを生成する | |
key = deriveBytes.GetBytes(keySize / 8); | |
iv = deriveBytes.GetBytes(blockSize / 8); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment