PHP interface Data encryption, decryption, verification signature _php skills

Source: Internet
Author: User

The code is very simple, here is no more nonsense, directly to the code

Copy Code code as follows:

<?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 ');
Ciphertext
$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 decryption, using ECB mode
$aes = new Crypt_aes (CRYPT_MODE_ECB);
Set AES Key
$aes->setkey ($aeskey);
Decrypt AES Redaction
$plaintext = $aes->decrypt (Base64_decode ($crypttext));
Echo $plaintext;
echo ' AES Encrypted plaintext
echo $aes->encrypt ($plaintext);
RSA Public Key
$publickey = ' migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqcffoeivyut9jw5w1l5ukx4advd837a8jhawm5s8yqnqfgemfd9t+ rdknxlqmt+dxeqaqgo4hbmcbej1aomzn6hijhk3/tftatonn8fgwdothewstcbbvkqwtdtby3gouwtovsri1i/a0vfb0+ Xm8mnf46ddhhrnzrycerbsbyrcwidaqab ';
echo Base64_decode ($publickey);
RSA Signature
$signature = ' xhin4uufqrkdehkbd/ hqisxlffsxm6ezcvcpqnwcqjq3uep3ayxmfuugve0xoh4aiwjiisowdnatol1bxvafkwjctxnkarwupvwrk+ Q0eqwsoadywsvqdeceg5stas1ckptrznaiw2ebgxcwspoj+aumeacpyydxlhdn646krzw= ';
echo Base64_decode ($signature);
$rsa = new Crypt_rsa ();
Set the RSA signature mode CRYPT_RSA_SIGNATURE_PSS or CRYPT_RSA_SIGNATURE_PKCS1
$rsa->setsignaturemode (CRYPT_RSA_SIGNATURE_PKCS1);
Var_dump ($rsa->createkey ());
Generate RSA public key, private key
Extract ($rsa->createkey ());
To generate a signature using the RSA private key
$rsa->loadkey ($privatekey);
$signature = $rsa->sign ($plaintext);
Verifying signatures with RSA public key
Echo $plaintext;
$rsa->loadkey (Base64_decode ($publickey));
echo $rsa->verify ($plaintext, Base64_decode ($signature))? ' Verified ': ' unverified ';
echo ' Generate RSA public key, private key
Var_dump ($rsa->createkey ());
Extract ($rsa->createkey ());
Encrypting data using the RSA private key
$rsa->loadkey ($privatekey);
$ciphertext = $rsa->encrypt ($plaintext);
Decrypting data using the RSA public key
$rsa->loadkey ($publickey);
echo $rsa->decrypt ($ciphertext);

The above is the entire content described in this article, I hope this code can give you learn PHP encryption and decryption can bring some help.

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.