8
9

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.

phpとunityの間での暗号化と復号化

Posted at
  • 文字コードはUTF-8です。
  • 試してみて暗号/復号できたものを書いただけです。

PHPで暗号化

function Aes128CBCEncode(String $input, String $key, String $IV) {
    return mcrypt_encrypt('rijndael-128', $key, $input, 'cbc', $IV);
}

PHPで復号化

function Aes128CBCDecode(String $input, String $key, String $IV) {
    $data = mcrypt_decrypt('rijndael-128', $key, $input, 'cbc', $IV);
    // 暗号時にパディングした \n を削除する
    $data = rtrim($data, "\0");
    return $data;
}

Unityで暗号化

byte[] Aes128CBCEncode(byte[] input, string Key, string IV)
{
    System.Security.Cryptography.AesManaged Aes = new System.Security.Cryptography.AesManaged();
    Aes.KeySize = 128;
    Aes.BlockSize = 128;
    Aes.Mode = System.Security.Cryptography.CipherMode.CBC;
    Aes.IV = System.Text.Encoding.UTF8.GetBytes(Key);
    Aes.Key = System.Text.Encoding.UTF8.GetBytes(IV);
    Aes.Padding = System.Security.Cryptography.PaddingMode.Zeros;
    return Aes.CreateEncryptor().TransformFinalBlock(input, 0, input.Length);
}

Unityで復号化

byte[] Aes128CBCDecode(byte[] input, string Key,string IV)
{
    System.Security.Cryptography.AesManaged Aes = new em.Security.Cryptography.AesManaged();
    Aes.KeySize = 128;
    Aes.BlockSize = 128;
    Aes.Mode = System.Security.Cryptography.CipherMode.CBC;
    Aes.IV = System.Text.Encoding.UTF8.GetBytes(Key);
    Aes.Key = System.Text.Encoding.UTF8.GetBytes(IV);
    Aes.Padding = System.Security.Cryptography.PaddingMode.Zeros;
    return Aes.CreateDecryptor().TransformFinalBlock(input, 0, input.Length);
}
8
9
1

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
8
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?