Skip to content

Instantly share code, notes, and snippets.

@k-holy
Created June 28, 2011 10:45
Show Gist options
  • Save k-holy/1050894 to your computer and use it in GitHub Desktop.
Save k-holy/1050894 to your computer and use it in GitHub Desktop.
Mcrypt
<?php
namespace Phanda;
use Phanda;
class Util {
public static function encrypt($source, $key, $iv, $toBase64Encode=true)
{
$mcrypt_resource = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
$mcrypt_key_size = mcrypt_enc_get_key_size($mcrypt_resource);
if (strlen($key) > $mcrypt_key_size) {
throw new RuntimeException(
sprintf('Key must be less than %d characters (bytes).', $mcrypt_key_size));
}
$mcrypt_iv_size = mcrypt_enc_get_iv_size($mcrypt_resource);
if ($mcrypt_iv_size != 0 && strlen($iv) != $mcrypt_iv_size) {
throw new RuntimeException(
sprintf('IV must be %d characters (bytes).', $mcrypt_iv_size));
}
mcrypt_generic_init($mcrypt_resource, $key, $iv);
$encrypted = mcrypt_generic($mcrypt_resource, $source);
mcrypt_generic_deinit($mcrypt_resource);
mcrypt_module_close($mcrypt_resource);
return ($toBase64Encode) ? base64_encode($encrypted) : $encrypted;
}
public static function decrypt($encrypted, $key, $iv, $fromBase64Encoded=true)
{
if ($fromBase64Encoded) {
$encrypted = base64_decode($encrypted);
}
$mcrypt_resource = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
$mcrypt_key_size = mcrypt_enc_get_key_size($mcrypt_resource);
if (strlen($key) > $mcrypt_key_size) {
throw new RuntimeException(
sprintf('Key must be less than %d characters (bytes).', $mcrypt_key_size));
}
$mcrypt_iv_size = mcrypt_enc_get_iv_size($mcrypt_resource);
if ($mcrypt_iv_size != 0 && strlen($iv) != $mcrypt_iv_size) {
throw new RuntimeException(
sprintf('IV must be %d characters (bytes).', $mcrypt_iv_size));
}
mcrypt_generic_init($mcrypt_resource, $key, $iv);
$decrypted = mdecrypt_generic($mcrypt_resource, $encrypted);
mcrypt_generic_deinit($mcrypt_resource);
mcrypt_module_close($mcrypt_resource);
return ($fromBase64Encoded) ? rtrim($decrypted, "\0") : $decrypted;
}
public static function buildRandomString($length, $chars=null) {
if (!isset($chars)) {
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789#$-+=?@[]_.';
}
$string = '';
$max = strlen($chars) - 1;
for ($i = 0; $i < $length; $i++) {
$string .= $chars[mt_rand(0, $max)];
}
return $string;
}
public static function NL($data) {
return str_replace("\r", "\n", str_replace("\r\n", "\n", $data));
}
public static function H($data, $default=null, $empty=null, $null=null) {
$var = $default;
if (isset($data)) {
if (strcmp($data, '') != 0) {
$var = htmlspecialchars(self::NL($data), ENT_QUOTES, 'UTF-8');
} elseif (isset($empty)) {
$var = $empty;
}
} elseif (isset($null)) {
$var = $null;
}
return $var;
}
}
use Phanda\Util as U;
$data['source' ] = (isset($_POST['source']) && strlen($_POST['source']) >= 1) ? $_POST['source'] : null;
$data['encrypted'] = null;
$data['decrypted'] = null;
if (isset($_POST['exec'])) {
$key = U::buildRandomString(56);
$iv = U::buildRandomString(8);
$data['encrypted'] = U::encrypt($data['source' ], $key, $iv, true);
$data['decrypted'] = U::decrypt($data['encrypted'], $key, $iv, true);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Mcrypt暗号化(Blowfish, CBC)</title>
</head>
<body>
<form method="post" action="<?php print U::H($_SERVER['SCRIPT_NAME']) ?>">
<p>暗号化する文字列<input type="text" name="source" value="<?php print U::H($data['source']) ?>" /></p>
<p><input type="submit" name="exec" value="実行" /></p>
<p>暗号化結果:<?php print U::H($data['encrypted']) ?></p>
<p>復号結果:<?php print U::H($data['decrypted']) ?></p>
<hr />
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment