php 7.1 AES/RSA加密執行個體

來源:互聯網
上載者:User

AES:

<?phpheader('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 '內容: '.$data."\n";$encrypted = openssl_encrypt($data, 'aes-256-cbc', base64_decode($key), OPENSSL_RAW_DATA, base64_decode($iv));echo '加密: '.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 '解密: '.$decrypted."\n";?>

用openssl產生rsa金鑰組(私密金鑰/公開金鑰):

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

RSA:

<?phpheader('Content-Type: text/plain;charset=utf-8');$data = 'phpbest';echo '原始內容: '.$data."\n";openssl_public_encrypt($data, $encrypted, file_get_contents(dirname(__FILE__).'/rsa_public_key.pem'));echo '公開金鑰加密: '.base64_encode($encrypted)."\n";$encrypted = base64_decode('nMD7Yrx37U5AZRpXukingESUNYiSUHWThekrmRA0oD0=');openssl_private_decrypt($encrypted, $decrypted, file_get_contents(dirname(__FILE__).'/rsa_private_key.pem'));echo '私密金鑰解密: '.$decrypted."\n";?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.