为什么我不能在瞬变中保存加密数据?

时间:2016-02-04 作者:primetimejas

我试图在瞬间保存加密数据(在PHP中使用AES-256-CBC),但由于某些原因,它一直失败。瞬态键的长度低于45个字符的要求,数据量不足以超过长文本大小。我试着在phpmyadmin中手动执行,它可以工作,这与WordPress查看数据的方式有关。有什么想法吗?

function encrypt_text( $text ){
  $textToEncrypt = $text;
  $encryptionMethod = \'AES-256-CBC\';  // AES is used by the U.S. gov\'t to encrypt top secret documents.
  $secretHash = \'secrethashgoeshere\';
  $iv_size = openssl_cipher_iv_length( $encryptionMethod );
  $iv = openssl_random_pseudo_bytes( $iv_size );
  $encryptedMessage = openssl_encrypt( $textToEncrypt, $encryptionMethod, $secretHash, 0, $iv );
  $encryptedMessage = $iv.$encryptedMessage; // Add the IV to the beginning of the encrypted string.
  return $encryptedMessage;
}

function decrypt_text( $encryptedValue ){
    $encryptionMethod = \'AES-256-CBC\';  // AES is used by the U.S. gov\'t to encrypt top secret documents.
    $secretHash = \'secrethashgoeshere\';
    $iv_size = openssl_cipher_iv_length( $encryptionMethod );
    $iv = substr( $encryptedValue, 0, $iv_size ); // Retrieve the IV that was appended to the begining of the encrypted string.
    $decryptedMessage = openssl_decrypt( substr( $encryptedValue, $iv_size ), $encryptionMethod, $secretHash, 0, $iv );
    return $decryptedMessage;
} 
我使用如下瞬态API进行设置:

$json_contact_info = json_encode( $contact_info );
$transient_data =  encrypt_text( $json_contact_info );
$transient_array = array( \'data\' => $transient_data );
$transient_set = set_transient( $transient_name, $transient_array, 60 * 60 * 24 * 7 );
然后像这样得到它:

$decrypted_transient_data = decrypt_text( $transient_data );
$cr_json = json_decode( $decrypted_transient_data );

1 个回复
SO网友:primetimejas

所以答案是使用base64\\u编码和base64\\u解码。

所以基本上是这样做来设置瞬态:

$json_contact_info = json_encode( $contact_info );
$transient_data =  encrypt_text( $json_contact_info );
$transient_array = base64_encode( $transient_data );
$transient_set = set_transient( $transient_name, $transient_array, 60 * 60 * 24 * 7 );
然后得到瞬态:

$decrypted_transient_data = decrypt_text( base64_decode( $transient_data ) );
$cr_json = json_decode( $decrypted_transient_data );

相关推荐

Clear Transients

我已经为我的一个客户构建了一个自定义主题,我正在使用瞬态,以使站点运行更快。一周后,我在wp\\U选项中看到了大量瞬态记录(约360000条记录)。这使我怀疑瞬态不会从数据库中删除。如何删除该记录,以及如何删除?有什么好的教程吗?亲切的问候