Simple implementation of RSA algorithm Java

Source: Internet
Author: User
Tags decrypt

RSA Introduction

RSA Span style= "font-family: Song body" > algorithm rsa The reliability of the algorithm. In other words, the more difficult the factorization of a large integer, the more reliable the rsa algorithm. If someone finds a fast factorization algorithm, the reliability of rsa rsa keys are only possible to be violently cracked. To 2008 So far, there is no reliable attack in the world rsa method of the algorithm. As long as the key length is long enough, use rsa The encrypted information is actually not solvable. The

It looks amazing, actually, in the course of learning cyber security and cryptography, I've been exposed. Nearly a year after graduation, the knowledge of number theory is almost forgotten. If you are interested in the principles of RSA algorithm, the following two articles are recommended:

Http://www.ruanyifeng.com/blog/2013/06/rsa_algorithm_part_one.html

Http://www.ruanyifeng.com/blog/2013/07/rsa_algorithm_part_two.html

This article describes a simple RSA algorithm that I implement myself.


RSA key generation and encryption decryption process



The specific implementation of RSA

The difficulty of implementing RSA is the generation of prime numbers. Fortunately, Java provides a powerful tool class BigInteger. The specific implementation is as follows

(But hundred lines):

public class RSA {private BigInteger p = null;private BigInteger q = null;private BigInteger n = null;private BigInteger totient = null  ;p rivate BigInteger e = null;private BigInteger d = null;public RSA (BigInteger p, BigInteger q) {this.p = P;THIS.Q = Q;n = P.multiply (q); n = p * q;//totient = (p-1) * (q-1) i.e. (n) totient = (P.subtract (biginteger.valueof (1)). Multiply ((Q.subtract ( Biginteger.valueof (1)))); 
                E = Gete ();//Select public key BigInteger y = EGCD (totient, E) [1]; D = Y.mod (totient); Generate the private key}public BigInteger Gete () {//here with TOTIENT/4 as the seed, select a prime number as the public key return Totient.divide (Biginteger.valueof (4)).        Nextprobableprime ();} Extended Euclid algorithm, purpose: Calculate e-1 mod npublic static biginteger[] EGCD (BigInteger d1, BigInteger D2) {biginteger[] ret = new Biginte GER[3]; BigInteger u = biginteger.valueof (1), u1 = biginteger.valueof (0); BigInteger v = biginteger.valueof (0), V1 = biginteger.valueof (1), if (D2.compareto (D1) > 0) {BigInteger tem = D1;D1 = D2 ;d 2 = tem;} while (D2.compareto (biginteger.valueof (0)) = 0) {BigInteger TQ = D1.divide (D2);//tq = D1/d2biginteger Tu = u;u = U1;u 1 = tu.subtract (tq.multiply (U1)); U1 =TU-TQ * U1biginteger TV = V;v = V1;v1 = Tv.subtract (tq.multiply (v1)); V1 = TV-TQ * V1biginteger TD1 = d1;d1 = D2;d2 = Td1.subtract (tq.multiply (D2)); D2 = TD1-TQ * D2ret[0] = u;ret[1] = v;ret[2] = d1;}        return ret;} Encrypt public BigInteger encode (bigintegEr d) {return D.modpow (THIS.E, THIS.N);} Decrypt public BigInteger decode (BigInteger c) {return C.modpow (THIS.D, THIS.N);}}

Disadvantages of RSA

The RSA algorithm uses the exponentiation operation and the plaintext is encrypted in groups, and the binary value of each grouping is less than N, which is the large

small must be less than or equals log2 (n) + 1 bits . If the clear text group is larger than this length, further subdivision is required, or a different

Encryption algorithms called Cryptography. This , RSA encryption decryption efficiency is not high, especially when the key length is very long, not suitable for large

encryption of the volume information. As a result, RSA is used in conjunction with other symmetric cryptographic algorithms.

Here is a simple encryption scheme that I designed in my undergraduate program, and a demo that was written in the last few days based on this scenario.


Assuming that the client is sending data to the service, the client first needs to generate its own RSA key through a user name and password (the private

Key CPR), and then generate the DES key deskey_c for this data transfer via a random number, and we also know that the server

The RSA public Key SPU (the RSA key for the server is fixed). With the key information for this data transfer, the client can

According to M1 through DES algorithm with Deskey_c encryption to obtain ciphertext C1, and then the client-generated des key deskey_c through the RSA algorithm

The encrypted des key C_deskey_c is obtained with the public key SPU of the server. Finally, the encrypted information (ciphertext C1 and encrypted

Des key C_deskey_c) is sent to the server via the HTTP protocol. After receiving the server, the DES Key is first passed through its own private key SPR

Decrypt it, get the deskey_c generated by the client before, and then decrypt the C1 with Deskey_c through the DES algorithm, get the original data

M1. At this point, the client-side cryptographic server decryption process is complete. During the encryption and decryption process, the DES Key is a 50-bit 10 binary number, RSA

The range of P and Q in the algorithm is 0 to 150 decimal integers. And when the server sends the data to the client, it also needs to generate the key

Information. First, the server obtains the client's RSA public key CPU through the client's user name and password, and then obtains this by a random number.

The DES key deskey_s of the secondary data transfer, and then the data M2 encrypted to ciphertext C2 by the DES algorithm, and then by the deskey_s

The RSA algorithm encrypts deskey_s to c_deskey_s with the client's public key CPU. Finally, the server will encrypt the ciphertext C2 and post-encrypted des

The key c_deskey_s is sent to the client. After the client receives, the RSA algorithm first decrypts the c_deskey_s with its own private key CPR,

The DES key deskey_s generated by the original server is obtained, and then the original data M2 is decrypted by the DES algorithm using this key. Such

The process of server-side encryption and client decryption is completed.


Demo:



Attached project source (recently overtime tight, code is not very good-_-| |) :

http://download.csdn.net/detail/he_qiao_2010/8548675


Simple implementation of RSA algorithm Java

Related Article

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.