Java encryption and decryption rsa usage code example
Recently, in order to analyze a request flow, I had to study RSA encryption.
First of all, emphasize that the "key" of the key reads "Yue", not "Yao", er...
There are a lot of principles about RSA on the Internet. Here is just my understanding:
1. Two sufficiently large coprime numbers P, q; 2. Modulo for modulo operation n = P * q; 3. E in public key Ku (E, n) satisfies 1 < e < (p-1) (Q-1) and is coprime with (p-1) (Q-1); 4. D in the key Kr (D, n) satisfies d * e% (p-1) (Q-1) = 1, and% is a remainder operation.
Because the public key is public, I know e and N. then according to the relationship between equations 2, 3 and 4, we can calculate the value of D and find the key as long as we deduce the value of P and Q from the value of n.
However, the key is here, n = P * Q. if the two coprime numbers P and Q are large enough, according to the current computer technology and other tools, they have not been able to decompose P and Q from n, which is a mathematical problem, and it is this problem that has become the reason why RSA encryption is widely used so far. In other words, as long as the key length n is large enough (generally 1024 is enough), it is basically impossible to deduce the private key information from the public key information.
Well, as a research essay, let's record how Java is used. There are three main methods, basically the same, but different ways to obtain the public and private keys:
Method 1:
The keypairgenerator is used to directly generate the public key and key. Generally, the private key is reserved for the server, and the public key is handed over to the client.
Operation results:
Method 2:
In fact, the first method is only used to generate the key. The generated key needs to be saved in the local file, so the keypairgenerator is generally not called on the client to generate the key.
Here, we can save the key obtained by method 1 to a file, and we can read it directly next time. I assume that it is saved in a file in the form of string, and then directly use the read string to generate the key.
Of course, you can also use OpenSSL to generate, but I don't think it's troublesome.
Operation results
Method 3:
In addition to saving the key string, other methods are generally to save only module n, e and D (exponent) of public key and private key.
Among them, N, e and D can be obtained in this way, and can be saved to the local file.
Here, suppose I have read the module and exponent from the file:
Operation results:
Here are three ways to sum up, that is
1,. Keypairgenerator gets the key; 2. String get key; 3. Module and exponent obtain the key.
--------------------Later, I found that the data was too long and abnormal, okay---------------------------
However, when the encrypted data is too long, it needs to be encrypted in groups, otherwise exceptions will be thrown if the data is too long, such as "encrypt data is too much", or "data length is longer than 127".
The n value of the key used by the above three methods (module) is 1024bit, that is, 128bytes. According to RSA encryption rules, 12 bytes are required to encrypt 1 byte of data, that is, other 11bytes may be used to record other information. I don't know here. A 1024bit key can encrypt data of 128-11 = 117bytes at most. Therefore, for data exceeding 117bytes, we need to use 117bytes as a group Data segmentation.
Operation results
Finally, one point must be emphasized, because it took me a lot of time.
After intermediate encryption, if you want to print it, you must print it in the form of hexadecimal or BCD code. You can't get bytes () from this string after new string (byte []), and don't use Base64, otherwise the original data will be destroyed.
For example:
Create a byte array new string and then GetBytes to see the running results:
The last byte changes from - 67 to 63. Please pay attention to this~
summary
The above is all about the code example of java encryption and decryption rsa use method in this article. I hope it will be helpful to you. Welcome to BigInteger of Java large number operation, encryption and decryption code examples of thread + IO files of Java exploration, etc. you can leave a message to point out any problems. Welcome to exchange and discuss.