Skip to content

Instantly share code, notes, and snippets.

@t-kashima
Created June 5, 2013 14:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t-kashima/5714358 to your computer and use it in GitHub Desktop.
Save t-kashima/5714358 to your computer and use it in GitHub Desktop.
UnityとPHPでデータを暗号化して通信をする
using System;
using System.IO;
using System.Security.Cryptography;
public static class AES {
private static string KEY = "********************************";
private static string IV = "********************************";
public static string encrypt(string clearText) {
return EncryptRJ256(KEY, IV, clearText);
}
public static string decrypt(string encryptText) {
return DecryptRJ256(KEY, IV, encryptText);
}
public static string EncryptRJ256(string pkey, string piv, string clearText) {
RijndaelManaged myRijndael = new RijndaelManaged();
myRijndael.Padding = PaddingMode.Zeros;
myRijndael.Mode = CipherMode.CBC;
myRijndael.KeySize = 256;
myRijndael.BlockSize = 256;
byte[] key = System.Text.Encoding.UTF8.GetBytes(pkey);
byte[] IV = System.Text.Encoding.UTF8.GetBytes(piv);
ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, IV);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);
byte[] encryptByte = System.Text.Encoding.UTF8.GetBytes(clearText);
cs.Write (encryptByte, 0, encryptByte.Length);
cs.FlushFinalBlock();
byte[] encrypted = ms.ToArray ();
return Convert.ToBase64String (encrypted);
}
public static string DecryptRJ256(string pkey, string piv, string encryptedText) {
RijndaelManaged myRijndael = new RijndaelManaged();
myRijndael.Padding = PaddingMode.Zeros;
myRijndael.Mode = CipherMode.CBC;
myRijndael.KeySize = 256;
myRijndael.BlockSize = 256;
byte[] key = System.Text.Encoding.UTF8.GetBytes(pkey);
byte[] iv = System.Text.Encoding.UTF8.GetBytes(piv);
ICryptoTransform decryptor = myRijndael.CreateDecryptor(key, iv);
byte[] encryptByte = Convert.FromBase64String(encryptedText);
byte[] encrypted = new byte[encryptByte.Length];
MemoryStream ms = new MemoryStream(encryptByte);
CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read);
cs.Read(encrypted, 0, encrypted.Length);
return (System.Text.Encoding.UTF8.GetString(encrypted));
}
}
<?php
define('key', '********************************');
define('iv', '********************************');
$data = $_GET['data'];
$clearText = decryptRJ256(key, iv, $data);
header('Access-Control-Allow-Origin: *');
header('Content-Type: text/html; charset=utf-8');
echo $clearText;
function decryptRJ256($key, $iv, $decryptedText) {
$decrypted = base64_decode($decryptedText);
$clearText = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $decrypted, MCRYPT_MODE_CBC, $iv);
return $clearText;
}
<?php
define('key', '********************************');
define('iv', '********************************');
$data = $_GET['data'];
$encrypted = encryptRJ256(key, iv, $data);
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json; charset=utf-8');
echo json_encode(array('status' => 200, 'response' => $encrypted));
function encryptRJ256($key, $iv, $text) {
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $iv);
return base64_encode($encrypted);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment