RSA encrypted Transfer (PHP)

Source: Internet
Author: User
Tags decrypt decrypt with public key

RSA Encrypted Transport:

For the first contact with RSA encryption of children's shoes, it is likely to be wound in. Recently wrote a request for RSA encryption transmission, summed up a bit. I hope I can help you a little.

The first step is to understand the following points:

1: The public key and the private key must be paired in order to

2: The private key is confidential and the public key is public

3: Public and private keys, can be mutually encrypted and decrypted

4: Use one of the keys to encrypt the data, then only the corresponding key can be solved

There are two main ways to use

  1: Public Key cryptography

A transfer data to B

(1), a encrypts the data with the public key, transmits to B

(2), B Decrypt with private key

  2: Public key authentication

A transfer data to B

(1), a with the private key to encrypt the data (that is, the private key signature), transfer to B

(2), B Decrypt with public key (i.e., verification)

Example: certificate, the process of public key authentication

Signature:

    /** Signing signing Password private key certificate requires a password*/     Public functionGET_SIGNING_STR ($data){        //$data = $this->get_request_string ();        $certs=Array(); //Dump (file_get_contents (Root_path. public/static/"." Private_key. pfx "));Openssl_pkcs12_read (file_get_contents(Root_path. " public/static/"."pRivate_key . pfx "),$certs, "Password"); if(!$certs)return; $signature= ' '; Openssl_sign ($data,$signature,$certs[' Pkey '], ' sha256 '); //Dump (Base64_encode ($signature));        return Base64_encode($signature); }

Verification:

 Public functionVerify_signing ($rest){        $rest=json_decode ($rest,true); $sign=$rest[' Sign ']; $sign=Base64_decode($sign);//Signature Parameters        unset($rest[' Sign ']); unset($rest[' Sign_type ']); $verifystr=$this->GET_REQUEST_STR ($rest);//Check Clear text string        $pkeyid= Openssl_pkey_get_public (file_get_contents(Root_path. " public/static/"." Public_key.cer ")); $verify= Openssl_verify ($verifystr,$sign,$pkeyid, ' sha256 ');//verification (Clear text/signature/key)//$msg = Openssl_error_string ();       Dump ($msg); Dump ($verify);Openssl_free_key ($pkeyid); if($verify==1){            Echo' Verification Pass '; // $data =json_encode ($rest); return $data; }Else{            return' Failed verification '; }    }

RSA Encryption Process

        $privateKeyStr=file_get_contents(root_path. ") public/static/"." Pkcs1_key.pem ");         $pi _key=openssl_pkey_get_private ($privateKeyStr);               $encryptSign= "";        Openssl_private_encrypt ($sign _str,$encryptSign,$pi _key);  //(string/key to be encrypted/encrypted)

SHA Encryption Process

$sign  = Hash (' sha256 ',$source);  //(algorithm/string to be encrypted)

RSA encrypted Transfer (PHP)

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.