PHP RSA encryption, decryption, signature, verification of detailed _php tips

Source: Internet
Author: User
Tags decrypt php language php class

PHP RSA encryption, decryption, signature, verification

Since the docking of Third-party agencies using the Java version of the RSA Plus decryption method, all just started to search the Internet many PHP version of RSA encryption, but most docking Java does not apply.

The following PHP version is suitable for docking Java interface, Java applicable key again PHP language use is needed to add

-----BEGIN Certificate-----
 
-----End Certificate-----

Use key:
Encrypting public Key Public_key.cer

Decrypt the private key Private_key.key

Signature private Key Sign_key.key

Verification of Public Key Verify.cer

<?php class rsautils{//Cryptographic public key function Redpukey () {//Mosaic encryption Public key Path $encryptionKeyPath = "D:/encryptions.cer   
    ";

    $encryptionKey 4Server = file_get_contents ($encryptionKeyPath); $pem = Chunk_split ($encryptionKey 4Server), Base64_encode, "\ n");//Convert to PEM-formatted public key $PEM = "-----BEGIN certificate-----\ n ". $pem."
    -----End Certificate-----\ n ";
    $publicKey = Openssl_pkey_get_public ($PEM);
  return $publicKey;   
    }//Decrypt private key function Redpikey () {///Splice decryption private key Path $decryptKeyPath = "D:/decrypts.key";

    $decryptKey 4Server = file_get_contents ($decryptKeyPath); $pem = Chunk_split ($decryptKey 4server,64, "\ n");//Convert to PEM-formatted private key $PEM = "-----BEGIN private key-----\ n". $pem. "
    -----end PRIVATE KEY-----\ n ";
    $privateKey = Openssl_pkey_get_private ($PEM);
  return $privateKey;
    }//Signature private key function Redsignkey () {//mosaic signature Path $signKeyPath = "D:/demo/sign.key";

    $signKey 4Server = file_get_contents ($signKeyPath); $pem = Chunk_split ($signKey 4Server,64, "\ n");//Convert to PEM-formatted private key $PEM = "-----BEGIN private key-----\ n". $pem. "
    -----end PRIVATE KEY-----\ n ";
    $signKey = Openssl_pkey_get_private ($PEM);
  return $signKey;  
    }//Check the public key function Redverifykey () {//stitching the verification path $verifyKeyPath = "D:/demo/verify.cer";

    $verifyKey 4Server = file_get_contents ($verifyKeyPath); $pem = Chunk_split ($verifyKey 4Server), Base64_encode, "\ n");//Convert to PEM-formatted public key $PEM = "-----BEGIN certificate-----\ n". $ Pem. "
    -----End Certificate-----\ n ";
    $verifyKey = Openssl_pkey_get_public ($PEM);
  return $verifyKey;
    }//Public key cryptographic function Pubkeyencrypt ($source _data, $pu _key) {$data = "";
    $dataArray = Str_split ($source _data, 117); 
      foreach ($dataArray as $value) {$encryptedTemp = "";
    Openssl_public_encrypt ($value, $encryptedTemp, $pu _key);//public key encryption $data. = Base64_encode ($encryptedTemp);
  return $data;
    }//private key decryption function Pikeydecrypt ($eccryptData, $decryptKey) {$decrypted = ""; $decodEstr = Base64_decode ($eccryptData);

    $enArray = Str_split ($DECODESTR, 256); foreach ($enArray as $va) {Openssl_private_decrypt ($va, $decryptedTemp, $decryptKey);//private key decryption $decrypted. = $DECR
    Yptedtemp;
  return $decrypted;

 }}?>

Attention:

Sometimes with Base64_encode encryption, to get the form to other pages, with Base64_decode decryption, there are garbled.

In the face of this problem, I wonder why some of the correct decryption, but there are some garbled it?

Later, after examination, found that there are some Chinese characters, in the form of get passed, + will be replaced with spaces.

In order to prevent the occurrence of garbled, I made a step to replace, and then decrypted, sure enough, garbled problem, no longer exist!

For example, if you pass a OID variable in the form of get, then when decrypting the restore, replace the space with the + number first. Then the output is normal.

As follows: $oid =base64_decode (Str_replace ("", "+", $_get[oid]);

Thank you for reading, I hope to help you, thank you for your support for this site!

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.