mirror of
https://git.code.sf.net/p/seeddms/code
synced 2024-11-26 15:32:13 +00:00
use openssl to encrypt and decrypt data with key
This commit is contained in:
parent
63ee505c0f
commit
a43a09a0e2
|
@ -582,11 +582,28 @@ function checkQuota($user) { /* {{{ */
|
||||||
* @return string encrypted data
|
* @return string encrypted data
|
||||||
*/
|
*/
|
||||||
function encryptData($key, $value) { /* {{{ */
|
function encryptData($key, $value) { /* {{{ */
|
||||||
|
if(function_exists('openssl_cipher_iv_length')) {
|
||||||
|
$nonceSize = openssl_cipher_iv_length('aes-256-ctr');
|
||||||
|
$nonce = openssl_random_pseudo_bytes($nonceSize);
|
||||||
|
|
||||||
|
$ciphertext = openssl_encrypt(
|
||||||
|
$value,
|
||||||
|
'aes-256-ctr',
|
||||||
|
$key,
|
||||||
|
OPENSSL_RAW_DATA,
|
||||||
|
$nonce
|
||||||
|
);
|
||||||
|
|
||||||
|
// Now let's pack the IV and the ciphertext together
|
||||||
|
// Naively, we can just concatenate
|
||||||
|
return $nonce.$ciphertext;
|
||||||
|
} else {
|
||||||
$text = $value;
|
$text = $value;
|
||||||
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
|
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
|
||||||
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
|
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
|
||||||
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
|
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
|
||||||
return $crypttext;
|
return $crypttext;
|
||||||
|
}
|
||||||
} /* }}} */
|
} /* }}} */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -597,11 +614,26 @@ function encryptData($key, $value) { /* {{{ */
|
||||||
* @return string plain text data
|
* @return string plain text data
|
||||||
*/
|
*/
|
||||||
function decryptData($key, $value) { /* {{{ */
|
function decryptData($key, $value) { /* {{{ */
|
||||||
|
if(function_exists('openssl_cipher_iv_length')) {
|
||||||
|
$nonceSize = openssl_cipher_iv_length('aes-256-ctr');
|
||||||
|
$nonce = mb_substr($value, 0, $nonceSize, '8bit');
|
||||||
|
$ciphertext = mb_substr($value, $nonceSize, null, '8bit');
|
||||||
|
|
||||||
|
$plaintext = openssl_decrypt(
|
||||||
|
$ciphertext,
|
||||||
|
'aes-256-ctr',
|
||||||
|
$key,
|
||||||
|
OPENSSL_RAW_DATA,
|
||||||
|
$nonce
|
||||||
|
);
|
||||||
|
return $plaintext;
|
||||||
|
} else {
|
||||||
$crypttext = $value;
|
$crypttext = $value;
|
||||||
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
|
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
|
||||||
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
|
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
|
||||||
$decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $crypttext, MCRYPT_MODE_ECB, $iv);
|
$decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $crypttext, MCRYPT_MODE_ECB, $iv);
|
||||||
return trim($decrypttext);
|
return trim($decrypttext);
|
||||||
|
}
|
||||||
} /* }}} */
|
} /* }}} */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue
Block a user