寻找简单的PHP多方式的加密方法(looking for simple PHP multi way

2019-06-24 07:44发布

我需要解密加密的POST值的字符串与秘密静态密钥的帮助。

我有一个直视的crypt()(但它只能单向)和这个Mcrypt,GnuPG的,......但我不是很满意。 我认为他们是对复杂,我简单的问题。 我应该怎么使用? 我不需要IV形状或超安全算法。 有没有基本的PHP函数,我还不知道?

我只是需要从用户的隐藏图像pathes这样的:ImageJPEG(ImageCreateFromJPEG(解码($ _ REQUEST [ 'encryptedImage'] '密钥')));

Answer 1:

更新(27/09/17):

由于mcrypt_encrypt已废弃PHP 7.1.0的。 IVE使用添加了一个简单的加密/解密的OpenSSL 。

function encrypt($string, $key = 'PrivateKey', $secret = 'SecretKey', $method = 'AES-256-CBC') {
    // hash
    $key = hash('sha256', $key);
    // create iv - encrypt method AES-256-CBC expects 16 bytes
    $iv = substr(hash('sha256', $secret), 0, 16);
    // encrypt
    $output = openssl_encrypt($string, $method, $key, 0, $iv);
    // encode
    return base64_encode($output);
}

function decrypt($string, $key = 'PrivateKey', $secret = 'SecretKey', $method = 'AES-256-CBC') {
    // hash
    $key = hash('sha256', $key);
    // create iv - encrypt method AES-256-CBC expects 16 bytes
    $iv = substr(hash('sha256', $secret), 0, 16);
    // decode
    $string = base64_decode($string);
    // decrypt
    return openssl_decrypt($string, $method, $key, 0, $iv);
}

$str = 'Encrypt this text';
echo "Plain: " .$str. "\n";

// encrypt
$encrypted_str = encrypt($str);
echo "Encrypted: " .$encrypted_str. "\n";

// decrypt
$decrypted_str = decrypt($encrypted_str);
echo "Decrypted: " .$decrypted_str. "\n";

原来的答案:

不能让简单那么这(PHP <7.1.0):

<?php 
define('SECRET',md5('Some secret key'));

function encrypt($value){
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    return mcrypt_encrypt(MCRYPT_RIJNDAEL_256, SECRET, $value, MCRYPT_MODE_ECB, $iv);
}

function decrypt($value){
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, SECRET, $value, MCRYPT_MODE_ECB, $iv));
}

?>


Answer 2:

你可以只包内置的功能,使之更友好。 就像在文档页面上的第二个用户后mcrypt_cbc :

<?php
$stuff="String to enc/enc/dec/dec =,=,";
$key="XiTo74dOO09N48YeUmuvbL0E";

function nl() {
    echo "<br/> \n";
}
$iv = mcrypt_create_iv (mcrypt_get_block_size (MCRYPT_TripleDES, MCRYPT_MODE_CBC), MCRYPT_DEV_RANDOM);

// Encrypting
function encrypt($string, $key) {
    $enc = "";
    global $iv;
    $enc=mcrypt_cbc (MCRYPT_TripleDES, $key, $string, MCRYPT_ENCRYPT, $iv);

  return base64_encode($enc);
}

// Decrypting
function decrypt($string, $key) {
    $dec = "";
    $string = trim(base64_decode($string));
    global $iv;
    $dec = mcrypt_cbc (MCRYPT_TripleDES, $key, $string, MCRYPT_DECRYPT, $iv);
  return $dec;
}

$encrypted = encrypt($stuff, $key);
$decrypted = decrypt($encrypted, $key);

echo "Encrypted is ".$encrypted . nl();
echo "Decrypted is ".$decrypted . nl();
?>


Answer 3:

这是一幢建于功能,我知道的只是基本。

$string = "/path/img.jpg";

$scramble = str_rot13($string);

echo "<p>Scrambled: ".$scramble;

echo "<p>Unscrambled: ".str_rot13($scramble);


文章来源: looking for simple PHP multi way encryption method