RSA encryption algorithm analysis

Encryption and decryption mechanism of RSA

RSA's encryption system can be divided into three parts: the generation of user key, encryption algorithm and decryption algorithm.

Generation of user key

In the generation part of user key, first of all, each user of the system will choose a group of public key by himself, and then make it public, so that others can use the public key to encrypt the confidential data, and then use the private key to decrypt and obtain the confidential data.

The following is the generation process of RSA key:

< 1 >. First find out two prime numbers p and q, both of which are at least 100 decimal numbers.

< 2 >. Calculate N=p * q and φ (N)= (q-1)*(p-1), where φ (N) is the Euler function, which means the number of integers with N coprime in integers less than N. For example, only numbers 1 and 5 are coprime with 6, so φ (6) = 2.

< 3 >. Find a number e that is coprime with φ (N), so (E, N) can be used as the public key of the user.

< 4 >. Calculate the user's private key d, so that e * d mod φ (N) =1, that is, in the case of module φ (N), e and d are mutually multiplicative inverse elements, where d is the user's private key, which must be absolutely confidential.

Encryption and decryption mechanism

In RSA cryptosystem, the value of each encrypted or decrypted block is in the range of 0 ~ (N-1). If the plaintext and ciphertext are larger than this block, they are divided into blocks.

In the actual encryption and decryption process, N is at least 521 bits. The higher the N value, the higher the security of RSA, and the longer the encryption and decryption time.

Now suppose to encrypt M information, where 0 < = M < = (n-1), RSA encryption algorithm is: C=Ee (M) = M^2modN
C=Ee(M)=MemodN C=Ee(M)=M^emodN C=Ee(M)=MemodN
C is the encrypted ciphertext. To decrypt C, you need to calculate: M=Dd (c) modM
M=Dd(C)modN M=Dd(C)modN M=Dd(C)modN

JAVA code implementation of encryption and decryption algorithm

The simple JAVA implementation code of RSA encryption and decryption is as follows:

package com.lcx.rsa;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class RSAEncrypt {
    private int p=0;
    private int q=0;
    private long n=0;
    private long m=0;

    private long public_key=0;//Public key
    private long private_key=0;//key

    private long text=0;//Plaintext
    private long secretword=0;//ciphertext
    private long word=0;//Plaintext after decryption

    //Judge whether it is prime
    public boolean primenumber(long t)
    {
        long k=0;
        k=(long)Math.sqrt((double)t);
        boolean flag=true;
        outer:for(int i=2;i<=k;i++)
        {
            if((t%i)==0)
            {
                flag = false;
                break outer;
            }
        }
        return flag;
    }
    //Input PQ
    public void inputPQ()throws Exception
    {
        do{
            System.out.println("Please enter prime number p: ");
            BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
            String br=stdin.readLine();
            this.p=Integer.parseInt(br);
        }
        while(!primenumber(this.p));
        do{
            System.out.println("Please enter prime number q: ");
            BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
            String br=stdin.readLine();
            this.q=Integer.parseInt(br);
        }
        while(!primenumber(this.q));
        this.n=this.p*this.q;
        this.m=(p-1)*(q-1);
        System.out.println("The product of these two prime numbers is p*q: "+this.n);
        System.out.println("Less thanNAnd withNThe number of integers of Coprime is m=(p-1)(q-1): "+this.m);
    }
    //greatest common factor 
    public long gcd(long a,long b)
    {
        long gcd;
        if(b==0)
            gcd=a;
        else
            gcd=gcd(b,a%b);
        System.out.println("gcd:"+gcd);
        return gcd;

    }
    //Input spoon
    public void getPublic_key()throws Exception
    {
        do{
            System.out.println("Please enter a value of public key, which should be less than m And and m Coprime: ");
            BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
            String br=stdin.readLine();
            this.public_key=Long.parseLong(br);
        }while((this.public_key >= this.m) || (this.gcd(this.m,this.public_key)!=1));
        System.out.println("The public key is:"+this.public_key);
    }
    //Calculate the key
    public void getPrivate_key()
    {
        long value=1;
        outer:for(long i=1;;i++)
        {
            value=i*this.m+1;
            System.out.println("value:  "+value);
            if((value%this.public_key==0)&& (value/this.public_key < this.m))
            {
                this.private_key=value/this.public_key;
                break outer;
            }
        }
        System.out.println("The generated private key is:"+this.private_key);
    }
    //Input plaintext
    public void getText()throws Exception
    {
        System.out.println("Please enter clear text:");
        BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
        String br=stdin.readLine();
        this.text=Long.parseLong(br);
    }
    //Encryption and decryption calculation
    public long colum(long y,long n,long key)
    {
        long mul;
        if(key==1)
            mul=y%n;
        else
            mul=y*this.colum(y,n,key-1)%n;
        return mul;
    }

    //Decryption after encryption
    public void pascolum()throws Exception
    {
        this.getText();
        System.out.println("Enter clear text as: "+this.text);
        //encryption
        this.secretword=this.colum(this.text,this.n,this.public_key);
        System.out.println("The ciphertext obtained is:"+this.secretword);
        //Decrypt
        this.word=this.colum(this.secretword,this.n,this.private_key);
        System.out.println("The plaintext obtained after decryption is:"+this.word);

    }
}

Test the following code:

package com.lcx.rsa;

public class RSAEncryptTest {
    public static void main(String []args)throws Exception
    {
        RSAEncrypt t = new RSAEncrypt();
        t.inputPQ();
        t.getPublic_key();
        t.getPrivate_key();
        t.pascolum();
    }
}

The input data source is provided by the example in the textbook to verify the correctness of the encryption and decryption algorithm.

The textbook examples are as follows:

The operation results are as follows:


To read more blogs, please click here to view – > https://lisongbai.top/

Published 2 original articles, praised 0 and visited 13
Private letter follow

Tags: Java less

Posted on Fri, 13 Mar 2020 07:01:36 -0400 by twilightnights