Php interface data encryption, decryption, Signature Verification

Source: Internet
Author: User
This article mainly introduces data encryption, decryption, and signature verification for php interfaces. For more information, see

This article mainly introduces data encryption, decryption, and signature verification for php interfaces. For more information, see

The code is very simple, so there is not much nonsense here, and the code is provided directly.

The Code is as follows:


<? Php
// Header ('content-Type: text/xml; charset = UTF-8 ');
Include_once (dirname (_ FILE _). DIRECTORY_SEPARATOR. 'phpsec '. DIRECTORY_SEPARATOR. 'Math'. DIRECTORY_SEPARATOR. 'biginteger. php ');
Include_once (dirname (_ FILE _). DIRECTORY_SEPARATOR. 'phpsec '. DIRECTORY_SEPARATOR. 'crypt'. DIRECTORY_SEPARATOR. 'aes. php ');
Include_once (dirname (_ FILE _). DIRECTORY_SEPARATOR. 'phpsec '. DIRECTORY_SEPARATOR. 'crypt'. DIRECTORY_SEPARATOR. 'rsa. php ');
// Ciphertext
$ Crypttext = 'encrypt/decrypt/k/encrypt/decrypt/xGb0g2XE/encrypt + qB7P/EMII/encrypt + 0 lujNgxIq/encrypt/hBT1UvIUml + encrypt/JcDCrdy0Co/decrypt + decrypt /release + btyn1_v/release + nQjj1THk0XHFc69N9g2 + release/QVVU2julTeKunvgAAEc3n + release/revgpasTOzDQa5NqR1B + release + CA ';
$ Aeskey = base64_decode ('qze60qzfxuirub2ey4 + 7 + Q = ');
// AES decryption, in ECB Mode
$ Aes = new Crypt_AES (CRYPT_MODE_ECB );
// Set the AES key
$ Aes-> setKey ($ aeskey );
// Decrypt the AES ciphertext
$ Plaintext = $ aes-> decrypt (base64_decode ($ crypttext ));
Echo $ plaintext;
Echo'

';
// AES encrypted plaintext
// Echo $ aes-> encrypt ($ plaintext );
// Rsa public key
$ Publickey = 'prop + rDknXLqMT + DXeQAqGo4hBmcbej1aoMzn6hIJHk3/large/A0Vfb0 + xm8mnf46ddhhrrycerbsbyrcwidaqab ';
// Echo base64_decode ($ publickey );
// Rsa Signature
$ Signature = 'xhin4uufqrkdehkbd/signature + 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 the RSA public key and Private Key
// Extract ($ rsa-> createKey ());
// Use the RSA private key to generate a signature
// $ Rsa-> loadKey ($ privatekey );
// $ Signature = $ rsa-> sign ($ plaintext );
// Use the RSA public key to verify the signature
Echo $ plaintext;
$ Rsa-> loadKey (base64_decode ($ publickey ));
Echo $ rsa-> verify ($ plaintext, base64_decode ($ signature ))? 'Verified ': 'unverified ';
Echo' ';
// Generate the RSA public key and Private Key
// Var_dump ($ rsa-> createKey ());
Extract ($ rsa-> createKey ());
// Use the RSA private key to encrypt data
$ Rsa-> loadKey ($ privatekey );
$ Ciphertext = $ rsa-> encrypt ($ plaintext );
// Use the RSA public key to decrypt data
$ Rsa-> loadKey ($ publickey );
Echo $ rsa-> decrypt ($ ciphertext );

The above is all the content described in this article. I hope this code can help you learn about php encryption and decryption.

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.