簡述php介面資料加密、解密、驗證簽名的用法

來源:互聯網
上載者:User
這篇文章主要介紹了php介面資料加密、解密、驗證簽名的相關資料,需要的朋友可以參考下

代碼很簡單,這裡就不多廢話了,直接奉上代碼

代碼如下:

<?php//header('Content-Type: text/xml; charset=utf-8');include_once(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'phpsec'.DIRECTORY_SEPARATOR.'Math'.DIRECTORY_SEPARATOR.'BigInteger.php');include_once(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'phpsec'.DIRECTORY_SEPARATOR.'Crypt'.DIRECTORY_SEPARATOR.'AES.php');include_once(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'phpsec'.DIRECTORY_SEPARATOR.'Crypt'.DIRECTORY_SEPARATOR.'RSA.php');//密文$crypttext = 'v66YKULHFld2JElhm/J9qik2Edr1JHdZIc/k/OesU2GbTX2usXyvF4jGvzvoihrrE8FsfKmllmjsMIjO5fdrS/FD20bYFii4JW3BO3bzshXmz6AEs2DWwG4sK9mNojfOC0IsMoV311X5/JlgUoQXkDy4F5HHpYE9d/xGb0g2XE/hnGSSy2cpQcvQtBlBmixwSckNhsEG92lovlOz8ULwkqG5o7x+qB7P/EMII/WaFAXBJXDXvZX7lmGcOgon6wLhKJLGXorP6BIxOg6LGc6Ux7BAt3i9+0lujNgxIq/sDsl23hsr3yOUpV5C5a813nrHx4HJyd/hBT1UvIUml+eTmJwWCpSfs2cvxIUr0CE57JAZVyXjK13shK3IsZHLPPsm/JcDCrdy0Co/d5uIGJAdzXdsQ56xsju+tlvnA1J6yq2tDIfYK/x6k911A5WXLKYxztD1nq+bTYN3Gv/WFfrzVtgWQBrh06ihS2cwvna0S9EV/YPmhnAjJmrX4trNr9NXQ9xaZaW4lGRg87U5QDV+nQjj1THk0XHFc69N9g2+DsAGyEs9tK6U0ZQ72hJZqZhBCDH1UKw0PLyIhJdxpgPPOWGp8/QVVU2julTeKunvgAAEc3n+GoZfqjsCDi1S6T2MTnjWYWNoFRBhvEZFD/revgpasTOzDQa5NqR1B+mUF70r6uw6MWLJ7cT9Tz3jq+CA';$aeskey = base64_decode('qZe60QZFxuirub2ey4+7+Q==');//AES解密,採用ECB模式$aes = new Crypt_AES(CRYPT_MODE_ECB);//設定AES密鑰$aes->setKey($aeskey);//解密AES密文$plaintext = $aes->decrypt(base64_decode($crypttext));echo $plaintext;echo '<hr />';//AES加密明文//echo $aes->encrypt($plaintext);//rsa公開金鑰$publickey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCffOeIVYut9jW5w1L5uKX4aDvd837a8JhaWm5S8YqNQfgEmfD9T+rDknXLqMT+DXeQAqGo4hBmcbej1aoMzn6hIJHk3/TfTAToNN8fgwDotHewsTCBbVkQWtDTby3GouWToVsRi1i/A0Vfb0+xM8MnF46DdhhrnZrycERBSbyrcwIDAQAB';//echo base64_decode($publickey);//rsa簽名$signature = 'XHin4uUFqrKDEhKBD/hQisXLFFSxM6EZCvCPqnWCQJq3uEp3ayxmFuUgVE0Xoh4AIWjIIsOWdnaToL1bXvAFKwjCtXnkaRwUpvWrk+Q0eqwsoAdywsVQDEceG5stas1CkPtrznAIW2eBGXCWspOj+aumEAcPyYDxLhDN646Krzw=';//echo base64_decode($signature);$rsa = new Crypt_RSA();//設定RSA簽名模式 CRYPT_RSA_SIGNATURE_PSS or CRYPT_RSA_SIGNATURE_PKCS1$rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);//var_dump($rsa->createKey());//產生RSA公開金鑰、私密金鑰//extract($rsa->createKey());//使用RSA私密金鑰產生簽名//$rsa->loadKey($privatekey);//$signature = $rsa->sign($plaintext);//使用RSA公開金鑰驗證簽名echo $plaintext;$rsa->loadKey(base64_decode($publickey));echo $rsa->verify($plaintext, base64_decode($signature)) ? 'verified' : 'unverified';echo '<hr />';//產生RSA公開金鑰、私密金鑰//var_dump($rsa->createKey());extract($rsa->createKey());//使用RSA私密金鑰加密資料$rsa->loadKey($privatekey);$ciphertext = $rsa->encrypt($plaintext);//使用RSA公開金鑰解密資料$rsa->loadKey($publickey);echo $rsa->decrypt($ciphertext);

總結:以上就是本篇文的全部內容,希望能對大家的學習有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.