Skip to content

Instantly share code, notes, and snippets.

@TakaakiIchijo
Created November 26, 2016 14:48
Show Gist options
  • Save TakaakiIchijo/3d64caf572c8593ff8dbe4b4643e4067 to your computer and use it in GitHub Desktop.
Save TakaakiIchijo/3d64caf572c8593ff8dbe4b4643e4067 to your computer and use it in GitHub Desktop.
Unityでstringまたはシリアライズ可能なクラスをJSONに変換してAES暗号化して保存・読み込みする
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;
/// <summary>
/// stringまたはシリアライズ可能なクラスをJSONに変換してAES暗号化して保存・読み込みする//
/// 参考: http://qiita.com/tempura/items/ad154d1269882ceda0f4
/// </summary>
public class FileAESCrypter
{
private static readonly string EncryptKey = "0123456789012345";
public static void EncryptJsonToFile<T>(T data, string savePath)
{
string jsondata = JsonUtility.ToJson(data);
Debug.Log("jsondata" + jsondata);
EncryptToFile(jsondata, savePath);
}
public static void EncryptToFile(string data, string savePath)
{
{
// 暗号化
byte[] bytes = Encoding.UTF8.GetBytes(data);
string initialVector;
byte[] bodyBytes;
EncryptAes(bytes, out initialVector, out bodyBytes);
// 保存
byte[] initialVectorBytes = Encoding.UTF8.GetBytes(initialVector);
if (!File.Exists(savePath))
{
Debug.Log("can't find file path");
Directory.CreateDirectory(Path.GetDirectoryName(savePath));
}
using (FileStream fs = new FileStream(savePath, FileMode.Create, FileAccess.Write))
using (BinaryWriter bw = new BinaryWriter(fs))
{
bw.Write(initialVectorBytes.Length);
bw.Write(initialVectorBytes);
bw.Write(bodyBytes.Length);
bw.Write(bodyBytes);
bw.Close();
}
}
}
public static bool DecryptJsonFromFile<T>(out T data, string savePath)
{
string strdata;
bool value = DecryptFromFile(out strdata, savePath);
data = value ? JsonUtility.FromJson<T>(strdata) : default(T);
return value;
}
public static bool DecryptFromFile(out string data, string savePath)
{
// 読み込み
byte[] initialVectorBytes = null;
byte[] bodyBytes = null;
if (!File.Exists(savePath))
{
Debug.LogError("cant find decrpt file path:" + savePath);
data = string.Empty;
return false;
}
using (FileStream fs = new FileStream(savePath, FileMode.Open, FileAccess.Read))
using (BinaryReader br = new BinaryReader(fs))
{
int length = br.ReadInt32();
initialVectorBytes = br.ReadBytes(length);
length = br.ReadInt32();
bodyBytes = br.ReadBytes(length);
}
// 複合化
byte[] bytes;
string initialVector = Encoding.UTF8.GetString(initialVectorBytes);
DecryptAes(bodyBytes, initialVector, out bytes);
data = Encoding.UTF8.GetString(bytes);
return true;
}
public static void EncryptAes(byte[] src, out string initialVector, out byte[] dst)
{
initialVector = Guid.NewGuid().ToString("N").Substring(0, EncryptKey.Length);
dst = null;
using (RijndaelManaged rijndael = new RijndaelManaged())
{
rijndael.Padding = PaddingMode.PKCS7;
rijndael.Mode = CipherMode.CBC;
rijndael.KeySize = 256;
rijndael.BlockSize = 128;
byte[] key = Encoding.UTF8.GetBytes(EncryptKey);
byte[] vec = Encoding.UTF8.GetBytes(initialVector);
using (ICryptoTransform encryptor = rijndael.CreateEncryptor(key, vec))
using (MemoryStream ms = new MemoryStream())
using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
cs.Write(src, 0, src.Length);
cs.FlushFinalBlock();
dst = ms.ToArray();
}
}
}
public static void DecryptAes(byte[] src, string initialVector, out byte[] dst)
{
dst = new byte[src.Length];
using (RijndaelManaged rijndael = new RijndaelManaged())
{
rijndael.Padding = PaddingMode.PKCS7;
rijndael.Mode = CipherMode.CBC;
rijndael.KeySize = 256;
rijndael.BlockSize = 128;
byte[] key = Encoding.UTF8.GetBytes(EncryptKey);
byte[] vec = Encoding.UTF8.GetBytes(initialVector);
using (ICryptoTransform decryptor = rijndael.CreateDecryptor(key, vec))
using (MemoryStream ms = new MemoryStream(src))
using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
{
cs.Read(dst, 0, dst.Length);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment