我试图在瞬间保存加密数据(在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 );