我的意思是:

Original String + Salt or Key --> Encrypted String
Encrypted String + Salt or Key --> Decrypted (Original String)

也许是这样的:

"hello world!" + "ABCD1234" --> Encrypt --> "2a2ffa8f13220befbe30819047e23b2c" (may be, for e.g)
"2a2ffa8f13220befbe30819047e23b2c" --> Decrypt with "ABCD1234" --> "hello world!"

在PHP中,如何做到这一点?

尝试使用Crypt_Blowfish,但它不适合我。


当前回答

不要做什么

警告: 这个答案使用了欧洲央行。ECB不是一个加密模式,它只是一个构建模块。使用这个答案中演示的ECB实际上并不能安全地加密字符串。不要在代码中使用ECB。请参阅Scott的回答以获得一个好的解决方案。

我自己弄到的。实际上我在谷歌上找到了一些答案,只是修改了一些东西。然而,结果是完全不安全的。

<?php
define("ENCRYPTION_KEY", "!@#$%^&*");
$string = "This is the original data string!";

echo $encrypted = encrypt($string, ENCRYPTION_KEY);
echo "<br />";
echo $decrypted = decrypt($encrypted, ENCRYPTION_KEY);

/**
 * Returns an encrypted & utf8-encoded
 */
function encrypt($pure_string, $encryption_key) {
    $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $encrypted_string = mcrypt_encrypt(MCRYPT_BLOWFISH, $encryption_key, utf8_encode($pure_string), MCRYPT_MODE_ECB, $iv);
    return $encrypted_string;
}

/**
 * Returns decrypted original string
 */
function decrypt($encrypted_string, $encryption_key) {
    $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $decrypted_string = mcrypt_decrypt(MCRYPT_BLOWFISH, $encryption_key, $encrypted_string, MCRYPT_MODE_ECB, $iv);
    return $decrypted_string;
}
?>

其他回答

我迟到了,但在寻找正确的方法时,我看到了这个页面,它是谷歌搜索结果最高的页面之一,所以我想分享我对这个问题的看法,我认为在写这篇文章时(2017年初)它是最新的。从PHP 7.1.0开始,mcrypt_decrypt和mcrypt_encrypt将被弃用,因此构建未来的证明代码应该使用openssl_encrypt和openssl_decrypt

你可以这样做:

$string_to_encrypt="Test";
$password="password";
$encrypted_string=openssl_encrypt($string_to_encrypt,"AES-128-ECB",$password);
$decrypted_string=openssl_decrypt($encrypted_string,"AES-128-ECB",$password);

重要:这使用ECB模式,这是不安全的。如果你想要一个简单的解决方案,而不需要参加密码学工程的速成班,不要自己编写,只需使用一个库。

您也可以使用任何其他芯片方法,这取决于您的安全需求。要找到可用的chipper方法,请参阅openssl_get_cipher_methods函数。

不要做什么

警告: 这个答案使用了欧洲央行。ECB不是一个加密模式,它只是一个构建模块。使用这个答案中演示的ECB实际上并不能安全地加密字符串。不要在代码中使用ECB。请参阅Scott的回答以获得一个好的解决方案。

我自己弄到的。实际上我在谷歌上找到了一些答案,只是修改了一些东西。然而,结果是完全不安全的。

<?php
define("ENCRYPTION_KEY", "!@#$%^&*");
$string = "This is the original data string!";

echo $encrypted = encrypt($string, ENCRYPTION_KEY);
echo "<br />";
echo $decrypted = decrypt($encrypted, ENCRYPTION_KEY);

/**
 * Returns an encrypted & utf8-encoded
 */
function encrypt($pure_string, $encryption_key) {
    $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $encrypted_string = mcrypt_encrypt(MCRYPT_BLOWFISH, $encryption_key, utf8_encode($pure_string), MCRYPT_MODE_ECB, $iv);
    return $encrypted_string;
}

/**
 * Returns decrypted original string
 */
function decrypt($encrypted_string, $encryption_key) {
    $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $decrypted_string = mcrypt_decrypt(MCRYPT_BLOWFISH, $encryption_key, $encrypted_string, MCRYPT_MODE_ECB, $iv);
    return $decrypted_string;
}
?>

下面的代码适用于php中所有具有特殊字符的字符串

   // Encrypt text --

    $token = "9611222007552";

      $cipher_method = 'aes-128-ctr';
      $enc_key = openssl_digest(php_uname(), 'SHA256', TRUE);  
      $enc_iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipher_method));  
      $crypted_token = openssl_encrypt($token, $cipher_method, $enc_key, 0, $enc_iv) . "::" . bin2hex($enc_iv);
    echo    $crypted_token;
    //unset($token, $cipher_method, $enc_key, $enc_iv);

    // Decrypt text  -- 

    list($crypted_token, $enc_iv) = explode("::", $crypted_token);  
      $cipher_method = 'aes-128-ctr';
      $enc_key = openssl_digest(php_uname(), 'SHA256', TRUE);
      $token = openssl_decrypt($crypted_token, $cipher_method, $enc_key, 0, hex2bin($enc_iv));
    echo   $token;
    //unset($crypted_token, $cipher_method, $enc_key, $enc_iv);

在PHP中,可以使用一种名为OpenSSL的加密扩展函数对字符串进行加密和解密。

openssl_encrypt()函数:openssl_encrypt()函数用于加密数据。

语法如下:

openssl_encrypt(字符串$data,字符串$method,字符串$key, $options = 0,字符串$iv,字符串$tag= NULL,字符串$aad, int $tag_length = 16)

参数说明如下:

$data:存放需要加密的字符串或数据。

$method:使用openssl_get_cipher_methods()函数使用cipher方法。

$key:保存加密密钥。

$options:它保存OPENSSL_RAW_DATA和OPENSSL_ZERO_PADDING标志的位分离。

$iv:它保存的初始化向量不是NULL。

$tag:它保存在使用AEAD密码模式(GCM或CCM)时通过引用传递的身份验证标记。

$aad:它保存额外的身份验证数据。

$tag_length:它保存身份验证标记的长度。对于GCM模式,认证标签的长度在4到16之间。

返回值:成功时返回加密字符串,失败时返回FALSE。

openssl_decrypt()函数openssl_decrypt()用于解密数据。

语法如下:

openssl_decrypt(字符串$data,字符串$method,字符串$key, int $options = 0,字符串$iv,字符串$tag,字符串$aad)

参数说明如下:

$data:存放需要加密的字符串或数据。

$method:使用openssl_get_cipher_methods()函数使用cipher方法。

$key:保存加密密钥。

$options:它保存OPENSSL_RAW_DATA和OPENSSL_ZERO_PADDING标志的位分离。

$iv:它保存的初始化向量不是NULL。

$tag:它保存使用AEAD密码模式(GCM或CCM)的身份验证标记。当身份验证失败时,openssl_decryption()返回FALSE。

$aad:它保存额外的身份验证数据。

返回值:成功时返回解密字符串,失败时返回FALSE。

方法:首先声明一个字符串并将其存储到变量中,然后使用openssl_encrypt()函数对给定的字符串进行加密,使用openssl_decrypt()函数对给定的字符串进行解密。

您可以在https://www.geeksforgeeks.org/how-to-encrypt-and-decrypt-a-php-string/上找到示例

如果你不想使用库(你应该),那么使用这样的东西(PHP 7):

function sign($message, $key) {
    return hash_hmac('sha256', $message, $key) . $message;
}

function verify($bundle, $key) {
    return hash_equals(
      hash_hmac('sha256', mb_substr($bundle, 64, null, '8bit'), $key),
      mb_substr($bundle, 0, 64, '8bit')
    );
}

function getKey($password, $keysize = 16) {
    return hash_pbkdf2('sha256',$password,'some_token',100000,$keysize,true);
}

function encrypt($message, $password) {
    $iv = random_bytes(16);
    $key = getKey($password);
    $result = sign(openssl_encrypt($message,'aes-256-ctr',$key,OPENSSL_RAW_DATA,$iv), $key);
    return bin2hex($iv).bin2hex($result);
}

function decrypt($hash, $password) {
    $iv = hex2bin(substr($hash, 0, 32));
    $data = hex2bin(substr($hash, 32));
    $key = getKey($password);
    if (!verify($data, $key)) {
      return null;
    }
    return openssl_decrypt(mb_substr($data, 64, null, '8bit'),'aes-256-ctr',$key,OPENSSL_RAW_DATA,$iv);
}

$string_to_encrypt='John Smith';
$password='password';
$encrypted_string=encrypt($string_to_encrypt, $password);
$decrypted_string=decrypt($encrypted_string, $password);