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.