6
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Unity C# データを暗号化・復号化(AESとXOR)

Posted at

保存対象のクラス

C#UserData.cs
[Serializable]
public class UserData {
	public string name;
	public int age;
}

暗号化・復号化処理のサンプル

C#EncryptSample.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;

public class EncryptSample : MonoBehaviour {
	void Start() {
		// 保存データクラスをインスタンス化
		var userData = new UserData() { name = "HOGE", age = 20 };
		// クラスをJSON文字列に変換
		string json = JsonUtility.ToJson(userData);
		// byte配列に変換
		byte[] arr = System.Text.Encoding.UTF8.GetBytes(json);

		// AES暗号化サンプル処理
		AesEncryptSample(arr);

		// XOR暗号化サンプル処理
		XorEncrypt(arr);
	}

	/// <summary>
	/// AES暗号化サンプル
	/// </summary>
	private void AesEncryptSample (byte[] arr) {
		// AES設定値
		//===================================
		int aesKeySize = 128;
		int aesBlockSize = 128;
		string aesIv = "1234567890123456";
		string aesKey = "1234567890123456";
		//===================================

		// AES暗号化
		byte[] arrEncrypted = AesEncrypt(arr, aesKeySize, aesBlockSize, aesIv, aesKey);

		// ファイル書き込み
		string path = System.IO.Path.Combine(Application.temporaryCachePath, "UserDataAES");
		System.IO.File.WriteAllBytes(path, arrEncrypted);

		// ファイル読み込み
		byte[] arrRead = System.IO.File.ReadAllBytes(path);

		// 復号化
		byte[] arrDecrypt = AesDecrypt(arrRead, aesKeySize, aesBlockSize, aesIv, aesKey);

		// byte配列を文字列に変換
		string decryptStr = System.Text.Encoding.UTF8.GetString(arrDecrypt);

		Debug.Log("AES : " + decryptStr);
	}

	/// <summary>
	/// XORサンプル
	/// </summary>
	private void XorEncrypt (byte[] arr) {
		// 暗号化文字列
		string keyString = "123456789";

		// XOR
		byte[] keyArr = System.Text.Encoding.UTF8.GetBytes(keyString);
		byte[] arrEncrypted = Xor(arr, keyArr);

		// ファイル書き込み
		string path = System.IO.Path.Combine(Application.temporaryCachePath, "UserDataXOR");
		System.IO.File.WriteAllBytes(path, arrEncrypted);

		// ファイル読み込み
		byte[] arrRead = System.IO.File.ReadAllBytes(path);

		// XOR
		byte[] arrDecrypt = Xor(arrRead, keyArr);

		// byte配列を文字列に変換
		string decryptStr = System.Text.Encoding.UTF8.GetString(arrDecrypt);

		Debug.Log("XOR : " + decryptStr);
	}



	/// <summary>
	/// AES暗号化
	/// </summary>
	public byte[] AesEncrypt (byte[] byteText, int aesKeySize, int aesBlockSize, string aesIv, string aesKey) {
		// AESマネージャー取得
		var aes = GetAesManager(aesKeySize, aesBlockSize, aesIv, aesKey);
		// 暗号化
		byte[] encryptText = aes.CreateEncryptor().TransformFinalBlock(byteText, 0, byteText.Length);

		return encryptText;
	}

	/// <summary>
	/// AES復号化
	/// </summary>
	public byte[] AesDecrypt (byte[] byteText, int aesKeySize, int aesBlockSize, string aesIv, string aesKey) {
		// AESマネージャー取得
		var aes = GetAesManager(aesKeySize, aesBlockSize, aesIv, aesKey);
		// 復号化
		byte[] decryptText = aes.CreateDecryptor().TransformFinalBlock(byteText, 0, byteText.Length);

		return decryptText;
	}

	/// <summary>
	/// AesManagedを取得
	/// </summary>
	/// <param name="keySize">暗号化鍵の長さ</param>
	/// <param name="blockSize">ブロックサイズ</param>
	/// <param name="iv">初期化ベクトル(半角X文字(8bit * X = [keySize]bit))</param>
	/// <param name="key">暗号化鍵 (半X文字(8bit * X文字 = [keySize]bit))</param>
	private AesManaged GetAesManager (int keySize, int blockSize, string iv, string key) {
		AesManaged aes = new AesManaged();
		aes.KeySize = keySize;
		aes.BlockSize = blockSize;
		aes.Mode = CipherMode.CBC;
		aes.IV = Encoding.UTF8.GetBytes(iv);
		aes.Key = Encoding.UTF8.GetBytes(key);
		aes.Padding = PaddingMode.PKCS7;
		return aes;
	}


	/// <summary>
	/// XOR
	/// </summary>
	public byte[] Xor(byte[] a, byte[] b){
		int j = 0;
		for (int i = 0; i < a.Length; i++) {
			if (j < b.Length){
				j++;
			} else {
				j = 1;
			}
			a[i] = (byte)(a[i] ^ b[j - 1]);
		}
		return a;
	}
}

▼実行結果
スクリーンショット 2019-01-30 22.47.09.png

6
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?