How does php generate rsa-encrypted public and private keys?

Source: Internet
Author: User
Tags gmp asymmetric encryption
How does php generate an rsa-encrypted public key and private key? I can see it on the internet. it seems that openssl can be used, but it seems that all files are read. Why does php call it ??? I don't know which Big Brother can give me some advice? Code or address for example! My system may require that the public key and private key be stored in the database. what should I do ?, I only know that this algorithm is useless. I have saved some information for you to refer to how php generates rsa-encrypted public and private keys.
I read it online. it seems that openssl can be used.
But it seems that all files are read.

Why does php call him ??? I don't know which Big Brother can give me some advice? Code for example

Or address!

Now my system may require a database

Put the public key and private key in the database.

What should I do?

------ Solution --------------------
I only know that this algorithm is useless. Save some information for your reference
Rsa asymmetric encryption

Http:// I .laoer.com/php-rsa.html

Http://www.zeali.net/entry/8

Http://pear.php.net/package/Math_BigInteger/download


Dh asymmetric encryption

Http://pear.php.net/package/Crypt_DiffieHellman/download

Use
Http://blog.csdn.net/ihefe/archive/2011/01/11/6128386.aspx
------ Solution --------------------
The following content is from the internet
[Url] http://su200909.blog.163.com/blog/static/410808604200910201096745/#/url]

In short, the RSA method generates a group of numbers n, e, and d. Use n and e to encrypt the plaintext (the plaintext must be less than n) and use n and d to decrypt the ciphertext.
It is easy to find a group of n, e, and d, but when n is large enough, it is difficult to find d from n and e. This is the security of RSA.
Https is not required. Here, we will talk about how to use JavaScript and PHP in the non-https environment to implement RSA encryption/decryption: use JavaScript to encrypt information in the browser and use PHP to decrypt information on the server.

1. JavaScript encryption

For more information about RSA, see [url] http://ohdave.com/rsa/#/url] (download the key generation program on this page ). Easy to use:

1. set parameters

SetMaxDigits (d );

The function parameter d is related to the number of digits of n. Link is

D = hexadecimal digits of n/2 + 3

For example, n is a hexadecimal 16-bit (corresponding to binary 125 ~ 128 bits), and d is set to 19.

2. set the key

Key = new RSAKeyPair (e, d, n );

The e, d, and n parameters are in hexadecimal notation. Only e and n and d are required for encryption. For example:

Key = new RSAKeyPair ('3D ', '', 'a090f4fdaba1c60975fb3b9ea6937a27 ');

3. Encryption

Ct = encryptedString (key, pt + '\ x01 ');

This function encrypts the plaintext pt into the ciphertext ct (hexadecimal string ). For long information, segment encryption (but PEAR: Crypt_RSA is not recognized. this problem will be resolved below ).
'\ X01' is the ending character required by PEAR: Crypt_RSA. To ensure that PEAR: Crypt_RSA can be successfully decrypted, the length of a set of plain text (in bytes) is:

Hexadecimal digits of n/2-3

For a key of 2, 13 bytes can be encrypted at a time (14 May also succeed, depending on the key and plaintext ).

2. use PHP for decryption

PEAR: Crypt_RSA is easy to install.

1. open the math library

Require_once ('Crypt/RSA. php'); // This path is generally used.
$ Math_obj = & Crypt_RSA_MathLoader: loadWrapper ();

PHP math libraries include GMP, BigInt, and BCMath. If no parameter is specified for loadWrapper, the loaded math library will be selected in this order (corresponding to the Crypt_RSA_Math_GMP class, Crypt_RSA_Math_BigInt class, and Crypt_RSA_Math_BCMath class ). BCMath is the default configuration of php, but it is much slower than GMP. I don't know about BigInt.

2. load the key

$ Dd = $ math_obj-> int2bin ('0x '. $ d );
$ Nn = $ math_obj-> int2bin ('0x '. $ n );
$ Pk = new Crypt_RSA_Key ($ nn, $ dd, 'private ');

$ Rsa_obj = new Crypt_RSA;
$ Rsa_obj-> setParams (array ('Dec _ key' => $ pk ));

3. decryption

$ Pt = $ rsa_obj-> decryptBinary ($ math_obj-> int2bin ('0x '. $ ct ));

PEAR: Crypt_RSA can use a hexadecimal key or decrypt the hexadecimal ciphertext. However, for the Crypt_RSA_Math_BCMath class, you must modify the int2bin code so that it can convert hexadecimal numbers into internal formats.
Of course, if it is a decimal key/ciphertext, the '0x 'prefix does not need to be added.

The changes to Crypt_RSA_Math_BCMath: int2bin () are added at the beginning of the function:

$ P = substr ($ num, 0, 2 );
If ($ p = '0x '| $ p = '0x '){
$ Len = strlen ($ num );
$ Result = 0;
$ Factor = 1;
For ($ I = $ len-1; $ I >=2; $ I --){
$ Result = bcadd ($ result, bcmul (hexdec ($ num [$ I]), $ factor ));
$ Factor = bcmul ($ factor, 16 );
}
$ Num = $ result;
}

III. solving the waste of JavaScript encryption functions

The encryptedString () function of JavaScript encrypts long information in segments, and uses spaces to separate paragraphs. The decryptBinary () function of the Crypt_RSA class cannot adapt to this problem. To fully utilize the JavaScript encryption function, only one function can be added to the Crypt_RSA class:

Function decryptString ($ s, $ key = null)
{
$ Result = '';
If ($ key = null)
$ Key = $ this-> _ dec_key;
$ Exp = $ this-> _ math_obj-> bin2int ($ key-> getExponent ());
$ Modulus = $ this-> _ math_obj-> bin2int ($ key-> getModulus ());
$ Chunk_len = $ key-> getKeyLength ()-1;
$ Block_len = (int) ceil ($ chunk_len/8 );

$ Radix = 10;
If (substr ($ s, 0, 2) = '0x '){
$ Radix = 16;
$ S = substr ($ s, 2 );
}
$ Blocks = explode ("", $ s );

For ($ I = 0; $ I <count ($ blocks); ++ $ I ){
$ Enc_data = $ blocks [$ I];
If ($ radix = 16)
$ Enc_data = '0x '. $ enc_data;
$ Enc_data = $ this-> _ math_obj-> int2bin ($ enc_data );

$ Data_len = strlen ($ enc_data );
$ Curr_pos = 0;
$ Bit_pos = 0;
$ Plain_data = $ this-> _ math_obj-> bin2int ("\ 0 ");

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.