当前位置:网站首页>Examples of AES and RSA encryption operations implemented by php7.1

Examples of AES and RSA encryption operations implemented by php7.1

2022-06-24 12:17:00 PHP Development Engineer

This article gives an example of PHP7.1 Realized AES And RSA Encryption operation . Share with you for your reference , As follows :

AES:

<?php
header('Content-Type: text/plain;charset=utf-8');
$data = 'phpbest';
$key = 'oScGU3fj8m/tDCyvsbEhwI91M1FcwvQqWuFpPoDHlFk='; //echo base64_encode(openssl_random_pseudo_bytes(32));
$iv = 'w2wJCnctEG09danPPI7SxQ=='; //echo base64_encode(openssl_random_pseudo_bytes(16));
echo ' Content : '.$data."\n";
$encrypted = openssl_encrypt($data, 'aes-256-cbc', base64_decode($key), OPENSSL_RAW_DATA, base64_decode($iv));
echo ' encryption : '.base64_encode($encrypted)."\n";
$encrypted = base64_decode('To3QFfvGJNm84KbKG1PLzA==');
$decrypted = openssl_decrypt($encrypted, 'aes-256-cbc', base64_decode($key), OPENSSL_RAW_DATA, base64_decode($iv));
echo ' Decrypt : '.$decrypted."\n";
?>

use openssl Generate rsa Key pair ( Private key / Public key ):

openssl genrsa -out rsa_private_key.pem 2048
openssl rsa -pubout -in rsa_private_key.pem -out rsa_public_key.pem

RSA:

<?php
header('Content-Type: text/plain;charset=utf-8');
$data = 'phpbest';
echo ' Original content : '.$data."\n";
openssl_public_encrypt($data, $encrypted, file_get_contents(dirname(__FILE__).'/rsa_public_key.pem'));
echo ' Public key encryption : '.base64_encode($encrypted)."\n";
$encrypted = base64_decode('nMD7Yrx37U5AZRpXukingESUNYiSUHWThekrmRA0oD0=');
openssl_private_decrypt($encrypted, $decrypted, file_get_contents(dirname(__FILE__).'/rsa_private_key.pem'));
echo ' Private key decryption : '.$decrypted."\n";
?>

PS: Interested friends about encryption and decryption can also refer to the online tools of this site :

On-line RSA encryption / Decryption tools :http://tools.jb51.net/password/rsa_encode

Text online encryption and decryption tool ( contain AES、DES、RC4 etc. ):http://tools.jb51.net/password/txt_encode

Online hash / Hash algorithm encryption tool :http://tools.jb51.net/password/hash_encrypt

On-line MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 encryption tool :http://tools.jb51.net/password/hash_md5_sha

On-line sha1/sha224/sha256/sha384/sha512 encryption tool :http://tools.jb51.net/password/sha_encode

Complete example :http://github.crmeb.net/u/defu

come from “ Open source world ” , link :https://ym.baisou.ltd/post/677.html, If you want to reprint , Please indicate the source , Otherwise, the legal liability will be investigated .

原网站

版权声明
本文为[PHP Development Engineer ]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/06/20210603160936004s.html

随机推荐