Encrypt and decrypt string
This task use php to encrypt and decrypt string.
Encrypt and decrypt string using RIJNDAEL_256
- Create crypto.php as following
- Call encrypt and decrypt method as following
$key = uniqid();
$src = "Hello world!";
require_once('crypto.php');
$cp = new crypto();
$tag1 = $cp->encrypt($src, $key);
$tag2 = $cp->decrypt($tag1, $key);
print($src . "<br />" . $tag1 . "<br />" . $tag2);
crypto.php
<?php
class crypto {
public function encrypt($text, $key) {
$src = uniqid() . $text;
$tag = trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $src, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
return $tag;
}
public function decrypt($text, $key) {
$src = $text;
$tag = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($src), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
$tag = substr($tag, 13);
return $tag;
}
}
?>
No comments:
Post a Comment