Implementation of RSA encryption and decryption algorithm function

Source: Internet
Author: User
Tags asymmetric encryption
This time to everyone to bring the RSA encryption and decryption algorithm function implementation, the RSA encryption and decryption algorithm functions to realize the attention of what, the following is the actual case, together to see.

You can first go to http://web.chacuo.net/netrsakeypair this website, generate public and private keys online

RSA Asymmetric Encryption algorithm, if it is public key encryption, you have to use the private key decryption, in turn, the private key encryption is decrypted with the public key, the following is the relevant implementation function

/** * RSA private key Encryption * @param string $private _key private key * @param strings $data to be encrypted * @return String $encrypted returns the encrypted String * @author Mosishu */function privateencrypt ($private _key, $data) {$encrypted = '; $pi _key = O Penssl_pkey_get_private ($private _key);//This function can be used to determine whether the private key is available, the return resource ID Resource ID//maximum allowable encryption length is 117, the score segment encryption $plainData = str  _split ($data, 100);//Generate key bits ($plainData as $chunk) {$partialEncrypted = '; $encryptionOk = Openssl_private_encrypt ($chunk, $partialEncrypted, $pi _key);//private key Encryption if ($encryptionOk = = = False) {return  False } $encrypted. = $partialEncrypted; } $encrypted = Base64_encode ($encrypted);//The encrypted content usually contains special characters that need to be encoded for conversion, to be aware that Base64 encoding is a URL-safe return $ when transferring over a URL between networks encrypted;} 
/** * RSA public key decryption (private key encrypted content can be decrypted by public key) * @param string $public _key Public key * @param string $dat The string after the private key is encrypted * @return string $decrypted returns the decrypted string * @author Mosishu */function publicdecrypt ($public _key, $data) {$decrypte d = '; $pu _key = openssl_pkey_get_public ($public _key);//This function can be used to determine if the public key is available $plainData = Str_split (Base64_decode ($data), 128)  ;//Generate key bits 1024x768 bit key foreach ($plainData as $chunk) {$str = ';  $decryptionOk = Openssl_public_decrypt ($chunk, $str, $pu _key);//Public key decryption if ($decryptionOk = = = False) {return false; } $decrypted. = $str; } return $decrypted;} 
RSA public key Cryptographic function Publicencrypt ($public _key, $data) {$encrypted = "; $pu _key = Openssl_pkey_get_public ($public _key); $ Plaindata = Str_split ($data, 100); foreach ($plainData as $chunk) {  $partialEncrypted = ';  $encryptionOk = Openssl_public_encrypt ($chunk, $partialEncrypted, $pu _key);//Public key Encryption  if ($encryptionOk = = = False) {   return false;  }  $encrypted. = $partialEncrypted; } $encrypted = Base64_encode ($encrypted); return $encrypted;}
RSA private key decryption function Privatedecrypt ($private _key, $data) {$decrypted = '; $pi _key = Openssl_pkey_get_private ($private _ Key); $plainData = Str_split (Base64_decode ($data), +);  foreach ($plainData as $chunk) {  $str = ';  $decryptionOk = Openssl_private_decrypt ($chunk, $str, $pi _key);//private key decryption  if ($decryptionOk = = = False) {   return false;  }  $decrypted. = $str; } return $decrypted;}

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

How the PHP generator uses

Installation tutorials for phpStudy2018

thinkphp Implementation Payment (JSAPI payment) process Tutorial _php instance

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.