RSA Asymmetric Encryption Principle

RSA Encryption Principle

step Explain describe Remarks
1, Find prime numbers P ,Q -
2, Calculate Common Modulus N = P * Q -
3, Euler function φ(N) = (P-1)(Q-1) -
4, Compute Public Key E 1 < E < φ(N) E must be an integer, E and phi(N) must be reciprocal prime numbers
5, Compute Private Key D E * D % φ(N) = 1 -
6, encryption C = M E mod N C: Cipher M: Clear text
7, Decrypt M =C D mod N C: Cipher M: Clear text

Public key = (E, N)
Private key = (D, N)

Outside, we only expose public keys.

Example

1. Find prime numbers P and Q

P = 3  
Q = 11

2. Calculate Common Modulus

N = P * Q = 3 * 11 = 33
N = 33

3. Euler functions

φ(N) = (P-1)(Q-1) = 2 * 10 = 20
φ(N) = 20

4. Compute Public Key E

1 < E < φ(N)
1 <E < 20

Range of values for E {3, 7, 9, 11, 13, 17, 19}
E must be an integer, E and phi(N) must be reciprocal prime numbers
For testing purposes, we take the minimum value E =3
3 and phi(N) =20 are prime numbers, satisfying the condition

5. Compute Private Key D

E * D % φ(N) = 1
3 * D  % 20 = 1   

D = 7 can be calculated from above

6. Public Key Encryption

For demonstration purposes, we encrypt a smaller number M = 2

Formula: C = ME mod N

M = 2
E = 3
N = 33

C = 23 % 33 = 8

Plaintext "2" is encrypted by RSA and becomes ciphertext "8"

7. Private key decryption

M =CD mod N

C = 8
D = 7
N = 33

M = 87 % 33
8 * 8 * 8 * 8 * 8 * 8 * 8=2097152
8 * 8 * 8 * 8 * 8 * 8 * 8 % 33 = 2

The cipher "8" is decrypted by RSA and becomes clear text 2.

Public Key Encryption-Private Key Decryption Flowchart

Public Key Encryption-Private Key Decryption Flowchart

Private Key Encryption-Public Key Decryption Flowchart

Private Key Encryption-Public Key Decryption Flowchart

Examples of RSA algorithms that come with JDK

import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;

import org.apache.commons.codec.binary.Base64;

/**
 * Asymmetric encryption is the only widely accepted and implemented data encryption-digital signature public key encryption, private key decryption private key encryption, public key decryption
 * 
 * @author jjs
 *
 */
public class RSADemo {

    private static String src = "infcn";

    private static RSAPublicKey rsaPublicKey;
    private static RSAPrivateKey rsaPrivateKey;

    static {
        // 1. Initialization Key
        KeyPairGenerator keyPairGenerator;
        try {
            keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            keyPairGenerator.initialize(512);// Integer multiple of 64
            KeyPair keyPair = keyPairGenerator.generateKeyPair();
            rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
            rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
            System.out.println("Public Key : " + Base64.encodeBase64String(rsaPublicKey.getEncoded()));
            System.out.println("Private Key : " + Base64.encodeBase64String(rsaPrivateKey.getEncoded()));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * Public key encryption, private key decryption
     * @author jijs
     */
    public static void pubEn2PriDe() {
        //Public Key Encryption
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] result = cipher.doFinal(src.getBytes());
        System.out.println("Public key encryption, private key decryption --encryption: " + Base64.encodeBase64String(result));

        //Private key decryption
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
        keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
        cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        result = cipher.doFinal(result);
        System.out.println("Public key encryption, private key decryption --Decrypt: " + new String(result));
    }
    

    /**
     * Private key encryption, public key decryption
     * @author jijs
     */
    public static void priEn2PubDe() {

        //secret key encryption
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        byte[] result = cipher.doFinal(src.getBytes());
        System.out.println("Private key encryption, public key decryption --encryption : " + Base64.encodeBase64String(result));

        //Public Key Decryption
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
        keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
        cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, publicKey);
        result = cipher.doFinal(result);
        System.out.println("Private key encryption, public key decryption   --Decrypt: " + new String(result));
    }

    public static void main(String[] args) {
        pubEn2PriDe();  //Public key encryption, private key decryption
        priEn2PubDe();  //Private key encryption, public key decryption
    }
}

Tags: Java JDK Apache codec

Posted on Sat, 14 Mar 2020 09:16:44 -0700 by simmsy