php接口數(shù)據(jù)加密、解密、驗(yàn)證簽名
來源:易賢網(wǎng) 閱讀:1843 次 日期:2015-03-16 11:12:00
溫馨提示:易賢網(wǎng)小編為您整理了“php接口數(shù)據(jù)加密、解密、驗(yàn)證簽名”,方便廣大網(wǎng)友查閱!

代碼很簡(jiǎn)單,這里就不多廢話了,直接奉上代碼

代碼如下:

<?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);

//設(shè)置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();

//設(shè)置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公鑰驗(yàn)證簽名

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私鑰加密數(shù)據(jù)

$rsa->loadKey($privatekey);

$ciphertext = $rsa->encrypt($plaintext);

//使用RSA公鑰解密數(shù)據(jù)

$rsa->loadKey($publickey);

echo $rsa->decrypt($ciphertext);

以上就是本文所述的全部?jī)?nèi)容了,希望本代碼能給大家學(xué)習(xí)php加密解密能夠帶來些幫助。

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:php接口數(shù)據(jù)加密、解密、驗(yàn)證簽名
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)