暗号化についての検索がありましたので いくつかサンプルを掲載しました。
暗号化の基本的なやり方ですが適当な文字列テーブルを作成して 独自のやり方で文字列操作をすることで暗号化をおこないます。
というのではイメージが湧かないと思いますので サンプルコードをいくつか抽出してみました。
セーブデータなどをPlayerPrefsなどに保存したい場合 簡単に中が見れてしまうので難読化する必要が出て来るかと思います。 PlayerPrefs 暗号化のサンプルは簡単なテーブル作成による暗号化です。
MD5暗号化はネット上で広く使用されている暗号化です (簡単ではないですが解析できなくはない)
AES暗号化は最近よく名前の挙がるTORにも使用されている暗号化技術です。
■PlayerPrefs 暗号化のサンプル
http://forum.unity3d.com/threads/26437-PlayerPrefs-Encryption
●使用方法
void Start () {
// this array should be filled before you can use EncryptedPlayerPrefs :
EncryptedPlayerPrefs.keys=new string[5];
EncryptedPlayerPrefs.keys[0]="23Wrudre";
EncryptedPlayerPrefs.keys[1]="SP9DupHa";
EncryptedPlayerPrefs.keys[2]="frA5rAS3";
EncryptedPlayerPrefs.keys[3]="tHat2epr";
EncryptedPlayerPrefs.keys[4]="jaw3eDAs";
EncryptedPlayerPrefs.SetString("test_string", "Hello World");
Debug.Log(EncryptedPlayerPrefs.GetString("test_string", "default"));
EncryptedPlayerPrefs.SetInt("test_int", 500);
Debug.Log(EncryptedPlayerPrefs.GetInt("test_int", -1));
EncryptedPlayerPrefs.SetFloat("test_float", 500.456);
Debug.Log(EncryptedPlayerPrefs.GetFloat("test_float", -1f));
}
●メソッドの本体
using UnityEngine;
using System.Collections;
using System.Security.Cryptography;
using System.Text;
public class EncryptedPlayerPrefs {
// Encrypted PlayerPrefs
// Written by Sven Magnus
// MD5 code by Matthew Wegner (from [url]http://www.unifycommunity.com/wiki/index.php?title=MD5[/url])
// Modify this key in this file :
private static string privateKey="9ETrEsWaFRach3gexaDr";
// Add some values to this array before using EncryptedPlayerPrefs
public static string[] keys;
public static string Md5(string strToEncrypt) {
UTF8Encoding ue = new UTF8Encoding();
byte[] bytes = ue.GetBytes(strToEncrypt);
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] hashBytes = md5.ComputeHash(bytes);
string hashString = "";
for (int i = 0; i < hashBytes.Length; i++) {
hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
}
return hashString.PadLeft(32, '0');
}
public static void SaveEncryption(string key, string type, string value) {
int keyIndex = (int)Mathf.Floor(Random.value * keys.Length);
string secretKey = keys[keyIndex];
string check = Md5(type + "_" + privateKey + "_" + secretKey + "_" + value);
PlayerPrefs.SetString(key + "_encryption_check", check);
PlayerPrefs.SetInt(key + "_used_key", keyIndex);
}
public static bool CheckEncryption(string key, string type, string value) {
int keyIndex = PlayerPrefs.GetInt(key + "_used_key");
string secretKey = keys[keyIndex];
string check = Md5(type + "_" + privateKey + "_" + secretKey + "_" + value);
if(!PlayerPrefs.HasKey(key + "_encryption_check")) return false;
string storedCheck = PlayerPrefs.GetString(key + "_encryption_check");
return storedCheck == check;
}
public static void SetInt(string key, int value) {
PlayerPrefs.SetInt(key, value);
SaveEncryption(key, "int", value.ToString());
}
public static void SetFloat(string key, float value) {
PlayerPrefs.SetFloat(key, value);
SaveEncryption(key, "float", Mathf.Floor(value*1000).ToString());
}
public static void SetString(string key, string value) {
PlayerPrefs.SetString(key, value);
SaveEncryption(key, "string", value);
}
public static int GetInt(string key) {
return GetInt(key, 0);
}
public static float GetFloat(string key) {
return GetFloat(key, 0f);
}
public static string GetString(string key) {
return GetString(key, "");
}
public static int GetInt(string key,int defaultValue) {
int value = PlayerPrefs.GetInt(key);
if(!CheckEncryption(key, "int", value.ToString())) return defaultValue;
return value;
}
public static float GetFloat(string key, float defaultValue) {
float value = PlayerPrefs.GetFloat(key);
if(!CheckEncryption(key, "float", Mathf.Floor(value*1000).ToString())) return defaultValue;
return value;
}
public static string GetString(string key, string defaultValue) {
string value = PlayerPrefs.GetString(key);
if(!CheckEncryption(key, "string", value)) return defaultValue;
return value;
}
public static bool HasKey(string key) {
return PlayerPrefs.HasKey(key);
}
public static void DeleteKey(string key) {
PlayerPrefs.DeleteKey(key);
PlayerPrefs.DeleteKey(key + "_encryption_check");
PlayerPrefs.DeleteKey(key + "_used_key");
}
}
■MD5 暗号化のサンプル
●UnityのフォーラムWIkiのサンプル
http://wiki.unity3d.com/index.php?title=MD5
public string Md5Sum(string strToEncrypt)
{
System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
byte[] bytes = ue.GetBytes(strToEncrypt);
// encrypt bytes
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hashBytes = md5.ComputeHash(bytes);
// Convert the encrypted bytes back to a string (base 16)
string hashString = "";
for (int i = 0; i < hashBytes.Length; i++)
{
hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
}
return hashString.PadLeft(32, '0');
}
●Unityフォーラムのサンプル
http://forum.unity3d.com/threads/114688-Encryption-In-Unity-Programs
using UnityEngine;
using System.Collections;
using System.Security.Cryptography;
using System.Text;
using System;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Paddings;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Parameters;
public class Encoding : MonoBehaviour
{
public class BCEngine
{
private readonly Encoding _encoding;
private readonly IBlockCipher _blockCipher;
private PaddedBufferedBlockCipher _cipher;
private IBlockCipherPadding _padding;
public BCEngine(IBlockCipher blockCipher)
{
_blockCipher = blockCipher;
}
public void SetPadding(IBlockCipherPadding padding)
{
if(padding != null)
_padding = padding;
}
public string Encrypt(string plain, string key)
{
byte[] result = BouncyCastleCrypto(true, System.Text.Encoding.ASCII.GetBytes(plain), key);
return Convert.ToBase64String(result);
}
public string Decrypt(string cipher, string key)
{
byte[] result = BouncyCastleCrypto(false, Convert.FromBase64String(cipher), key);
return System.Text.Encoding.ASCII.GetString(result);
}
///
///
///
///
///
///
///
///
private byte[] BouncyCastleCrypto(bool forEncrypt, byte[] input, string key)
{
try
{
_cipher = _padding == null ? new PaddedBufferedBlockCipher(_blockCipher) : new PaddedBufferedBlockCipher(_blockCipher, _padding);
byte[] keyByte = System.Text.Encoding.ASCII.GetBytes(key);
_cipher.Init(forEncrypt, new KeyParameter(keyByte));
return _cipher.DoFinal(input);
}
catch (Org.BouncyCastle.Crypto.CryptoException ex)
{
throw new CryptoException(ex.ToString());
}
}
}
BCEngine bc = new BCEngine(new BlowfishEngine());
public string EncodePassword(string input, string salt = "$2a$13$AcDc1326./qZrrFpoXx0$")
{
return bc.Encrypt(input, salt);
}
public string CalculateMD5Hash(string input)
{
// step 1, calculate MD5 hash from input
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
return Convert.ToBase64String(hash);
}
}
■AES暗号化のサンプル
●C#のサンプルいろいろ
http://stackoverflow.com/questions/273452/using-aes-encryption-in-c-sharp
http://www.gutgames.com/post/AES-Encryption-in-C.aspx
http://www.c-sharpcorner.com/UploadFile/ahsanshakir/EncryptFile12212006042816AM/EncryptFile.aspx
あとココらへん?↓
http://msdn.microsoft.com/en-us/library/53tyfkaw(v=vs.100).aspx
http://www.mono-project.com/Cryptography
●Assetファイルの暗号化に質問
http://answers.unity3d.com/questions/8855/how-to-encrypt-asset-bundles.html
●C#コードの難読化に関するスレッド
http://forum.unity3d.com/threads/175054-Obfuscation-Community-requested