PHP 7.1 Aes/rsa Encryption Instance

Source: Internet
Author: User
Tags openssl openssl rsa

Aes:

<?php
Header (' Content-type:text/plain;charset=utf-8 ');
$data = ' phpbest ';
$key = ' oscgu3fj8m/tdcyvsbehwi91m1fcwvqqwufppodhlfk= '; echo Base64_encode (openssl_random_pseudo_bytes);
$iv = ' w2wjcncteg09danppi7sxq== '; echo Base64_encode (openssl_random_pseudo_bytes);
Echo ' content: '. $data. ' \ n ";

$encrypted = Openssl_encrypt ($data, ' AES-256-CBC ', Base64_decode ($key), Openssl_raw_data, Base64_decode ($IV));
Echo ' encrypt: '. 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 ";
? >

Generate an RSA key pair (private key/public key) with OpenSSL:

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 cryptography: '. 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 "; 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.