openssl_encrypt

example1

https://www.w3schools.com/php/phptryit.asp?filename=tryphp_func_string_echo

把内容替换掉

<?php

$encrypt_algo = 'AES-256-CBC';
$encrypt_key = 'SEC_KEY_r65VPa61';
$encrypt_iv = 'SEC_IV_MSh65s6Ba';

// Datas :
$inputDatas = json_encode([
    'codeClient' => '123456',
    'codeSociete' => '01'
]);

echo ' JSON Datas input : ' . $inputDatas;
// ouput :
// JSON Datas input : {"codeClient":"123456","codeSociete":"01"}

// Coding datas
$encryptedDatas = openssl_encrypt($inputDatas, $encrypt_algo, $encrypt_key, 0, $encrypt_iv);

echo '<br>encrypted  datas : ' . $encryptedDatas;
// output :
// encrypted  datas : DD40QpQA7bvTILU2WHjnn81C1xXUX5h4tadZXrl6OoQsZdL0gY8fwjQTYD23/N2k

$encryptedUrlDatas = urlencode($encryptedDatas);
echo '<br>encrypted  datas for URL : ' . $encryptedUrlDatas;
// output :
// encrypted datas for URL :  DD40QpQA7bvTILU2WHjnn81C1xXUX5h4tadZXrl6OoQsZdL0gY8fwjQTYD23%2FN2k


// Company's action :

// Décodage des données
$decryptedDatas = openssl_decrypt(urldecode($encryptedUrlDatas), $encrypt_algo, $encrypt_key, 0, $encrypt_iv);

echo '<br>Decoded data : ' . $decryptedDatas;
// output :
// Decoded data : {"codeClient":"123456","codeSociete":"01"}
?>

打印出来的结果是

JSON Datas input : {"codeClient":"123456","codeSociete":"01"}
encrypted datas : DD40QpQA7bvTILU2WHjnn81C1xXUX5h4tadZXrl6OoQsZdL0gY8fwjQTYD23/N2k
encrypted datas for URL : DD40QpQA7bvTILU2WHjnn81C1xXUX5h4tadZXrl6OoQsZdL0gY8fwjQTYD23%2FN2k
Decoded data : {"codeClient":"123456","codeSociete":"01"} 

 

example2

github上的一个例子https://gist.github.com/Oranzh/2520823f9d1cea603e60b8e8f3fe1d36  

<?php

$plaintext = 'My secret message 1234';
$password = '3sc3RLrpd17';
$method = 'aes-256-cbc';

$key = password_hash($password, PASSWORD_BCRYPT, ['cost' => 12]);
echo "Key:" . $key . "\n";

// IV must be exact 16 chars (128 bit)
$iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);

// av3DYGLkwBsErphcyYp+imUW4QKs19hUnFyyYcXwURU=
$encrypted = base64_encode(openssl_encrypt($plaintext, $method, $key, OPENSSL_RAW_DATA, $iv));

// My secret message 1234
$decrypted = openssl_decrypt(base64_decode($encrypted), $method, $key, OPENSSL_RAW_DATA, $iv);

echo 'plaintext=' . $plaintext . "\n";
echo 'cipher=' . $method . "\n";
echo 'encrypted to: ' . $encrypted . "\n";
echo 'decrypted to: ' . $decrypted . "\n\n";
?>

输出结果为

Key:$2y$12$7HIdt7ntKpwskP3xlFloEebJWSGDVnmlWGhcNSIogQFzm165fuLue

plaintext=My secret message 1234

cipher=aes-256-cbc

encrypted to: n6XOsJy3MjPh+JGo2tP1I2Y0f5/LZggoJHhCPSXgPtQ=

decrypted to: My secret message 1234 

 

example3

$encrypt_algo = 'AES-256-CBC';
$encrypt_key = 'SEC_KEY_r65VPa61';
$encrypt_iv = 'SEC_IV_MSh65s6B';  这里少了1位

JSON Datas input : {"codeClient":"123456","codeSociete":"01"}
encrypted datas : 6Vj8qBQSC8Y20XBQgcZf3iwbwLdwJph4B6nJGY8KhVgVNS2z77kVX18xrULrofdp
encrypted datas for URL : 6Vj8qBQSC8Y20XBQgcZf3iwbwLdwJph4B6nJGY8KhVgVNS2z77kVX18xrULrofdp
Decoded data : {"codeClient":"123456","codeSociete":"01"} 

 

在线 AES 128 加密 | 在线 AES 256 加密

{"codeClient":"123456","codeSociete":"01"}

iv:SEC_IV_MSh65s6Ba

passphrase:SEC_KEY_r65VPa61

加密结果:

DD40QpQA7bvTILU2WHjnn81C1xXUX5h4tadZXrl6OoQsZdL0gY8fwjQTYD23/N2k

 

posted @ 2022-11-30 19:35  ChuckLu  阅读(33)  评论(0编辑  收藏  举报