LAMP環境で暗号化を検討する場合、PHPでやるかMySQLでやるかということになるが、汎用性という意味ではPHP側でやるほうが、DBに蓄積しない値も暗号化の対象にできるし、MySQL以外のDBの場合にも使える。
PHPの場合、Mcryptを使うか、opensslを使うかという選択肢になるかと思うが、PHP5.3以降なら記述が簡単なopensslで。
crypt.php
<?php
$key = "hogehoge";
$str = "Hello World.";
//暗号化
$encrypt = openssl_encrypt($str,'aes-256-ecb',$key);
echo $encrypt;
echo "<br>";
//復号化
$decrypt = openssl_decrypt($encrypt,'aes-256-ecb',$key);
echo $decrypt;
echo "<br>";
//methodの種類の確認
print_r(openssl_get_cipher_methods());
必要に応じてoption, ivを渡すこともできる。詳しくはここ。
使えるMethodは。
[0] => AES-128-CBC
[1] => AES-128-CFB
[2] => AES-128-CFB1
[3] => AES-128-CFB8
[4] => AES-128-ECB
[5] => AES-128-OFB
[6] => AES-192-CBC
[7] => AES-192-CFB
[8] => AES-192-CFB1
[9] => AES-192-CFB8
[10] => AES-192-ECB
[11] => AES-192-OFB
[12] => AES-256-CBC
[13] => AES-256-CFB
[14] => AES-256-CFB1
[15] => AES-256-CFB8
[16] => AES-256-ECB
[17] => AES-256-OFB
[18] => BF-CBC
[19] => BF-CFB
[20] => BF-ECB
[21] => BF-OFB
[22] => CAST5-CBC
[23] => CAST5-CFB
[24] => CAST5-ECB
[25] => CAST5-OFB
[26] => DES-CBC
[27] => DES-CFB
[28] => DES-CFB1
[29] => DES-CFB8
[30] => DES-ECB
[31] => DES-EDE
[32] => DES-EDE-CBC
[33] => DES-EDE-CFB
[34] => DES-EDE-OFB
[35] => DES-EDE3
[36] => DES-EDE3-CBC
[37] => DES-EDE3-CFB
[38] => DES-EDE3-CFB1
[39] => DES-EDE3-CFB8
[40] => DES-EDE3-OFB
[41] => DES-OFB
[42] => DESX-CBC
[43] => RC2-40-CBC
[44] => RC2-64-CBC
[45] => RC2-CBC
[46] => RC2-CFB
[47] => RC2-ECB
[48] => RC2-OFB
[49] => RC4
[50] => RC4-40
[51] => RC5-CBC
[52] => RC5-CFB
[53] => RC5-ECB
[54] => RC5-OFB
[55] => SEED-CBC
[56] => SEED-CFB
[57] => SEED-ECB
[58] => SEED-OFB
[59] => aes-128-cbc
[60] => aes-128-cfb
[61] => aes-128-cfb1
[62] => aes-128-cfb8
[63] => aes-128-ecb
[64] => aes-128-ofb
[65] => aes-192-cbc
[66] => aes-192-cfb
[67] => aes-192-cfb1
[68] => aes-192-cfb8
[69] => aes-192-ecb
[70] => aes-192-ofb
[71] => aes-256-cbc
[72] => aes-256-cfb
[73] => aes-256-cfb1
[74] => aes-256-cfb8
[75] => aes-256-ecb
[76] => aes-256-ofb
[77] => bf-cbc
[78] => bf-cfb
[79] => bf-ecb
[80] => bf-ofb
[81] => cast5-cbc
[82] => cast5-cfb
[83] => cast5-ecb
[84] => cast5-ofb
[85] => des-cbc
[86] => des-cfb
[87] => des-cfb1
[88] => des-cfb8
[89] => des-ecb
[90] => des-ede
[91] => des-ede-cbc
[92] => des-ede-cfb
[93] => des-ede-ofb
[94] => des-ede3
[95] => des-ede3-cbc
[96] => des-ede3-cfb
[97] => des-ede3-cfb1
[98] => des-ede3-cfb8
[99] => des-ede3-ofb
[100] => des-ofb
[101] => desx-cbc
[102] => rc2-40-cbc
[103] => rc2-64-cbc
[104] => rc2-cbc
[105] => rc2-cfb
[106] => rc2-ecb
[107] => rc2-ofb
[108] => rc4
[109] => rc4-40
[110] => rc5-cbc
[111] => rc5-cfb
[112] => rc5-ecb
[113] => rc5-ofb
[114] => seed-cbc
[115] => seed-cfb
[116] => seed-ecb
[117] => seed-ofb