-3

I am generating a asymmetric key pair (public and private) in java using RSA algorithm and trying to use the public key in java-script to decrypt some text and after modification of data again encrypt the data using same public key sent to spring-boot server , so that spring boot can decrypt using by private key

source link : https://github.com/jeebendu/RSA_Asymetric_Encryption/blob/main/AsymmetricMain.java

Can anyone help how to achieve in angularjs or any javascript framework CryptoJS

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.SecureRandom;
import java.security.spec.EncodedKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

import javax.crypto.Cipher;

public class AsymmetricMain {

    private static final String ALGORITHM = "RSA";
    private static final String PVTKEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCa1OwhIXq10pykhtCap/stVCL0 X/1VyyeAl82HXEHotvw8F6TR8y9AsARdgSpHn6ozPaHSvo39QqTPvni065iaYidXFt4rR6dLrQ8S Ar1edbVQ/L//DCSQPQezmr0fL88R2oMO/MdPVPLlnwTJMXhMCQTnKydD/otShjSfpd9FVqII9BGh MR68pUBCtLgNcmn1YgBqXUyUGWahFbPsOAn0yRD+/28Y3xDYu3y3rNINITNf2bS2GKbmhKV1QLjp 0v7N5VppWBrrYlRSmlMyf2/1ChZPWEPUjdC117aR+PDY6t6dPtcap/pCxU4vQo1oIZBre1AEfoCe mt5bNj4yB30rAgMBAAECggEAeXfn1EomsfSMTYsyptJ4fORQE/YmkqMx13HAnZwkXQUJt7844Dlv 7cjpc838tHovlcmZZfN3A6TAFfcxTYazjxLIGEvpenzZY8ZeV1vs4ulnmSThH5+QI9StcRtJmejx A+mw/hYT60oS0VBC+fCklspQnWc+g9pzxQdiy4jfM8534yjv8FnFQqRxuqAZfbJraSx5E+nT3XSt IrLriMoKsNaLSzXn+RulGJndxRmcZ63j+rmoiEPxl9nrU0S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B6Lb8PBek0fMvQLAEXYEqR5+qMz2h0r6N/UKkz754tOuYmmInVxbeK0enS60PEgK9XnW1UPy//wwkkD0Hs5q9Hy/PEdqDDvzHT1Ty5Z8EyTF4TAkE5ysnQ/6LUoY0n6XfRVaiCPQRoTEevKVAQrS4DXJp9WIAal1MlBlmoRWz7DgJ9MkQ/v9vGN8Q2Lt8t6zSDSEzX9m0thim5oSldUC46dL+zeVaaVga62JUUppTMn9v9QoWT1hD1I3Qtde2kfjw2OrenT7XGqf6QsVOL0KNaCGQa3tQBH6AnpreWzY+Mgd9KwIDAQAB";

    // Generating public & private keys
    // using RSA algorithm.
    public static KeyPair generateRSAKkeyPair() throws Exception {
        SecureRandom secureRandom = new SecureRandom();
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);

        keyPairGenerator.initialize(2048, secureRandom);
        return keyPairGenerator.generateKeyPair();
    }

    // Encryption function which converts
    // the plainText into a cipherText
    // using private Key.
    public static byte[] encrypt(String plainText, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM);

        cipher.init(Cipher.ENCRYPT_MODE, privateKey);

        return cipher.doFinal(plainText.getBytes());
    }

    // Decryption function which converts
    // the ciphertext back to the
    // original plaintext.
    public static String decrypt(byte[] cipherText, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM);

        cipher.init(Cipher.DECRYPT_MODE, publicKey);
        byte[] result = cipher.doFinal(cipherText);

        return new String(result);
    }

    public static byte[] encrypt(String plainText, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);

        return cipher.doFinal(plainText.getBytes());
    }

    public static String decrypt(byte[] cipherText, PrivateKey privateKey) throws Exception {

        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);

        byte[] decryptedBytes = cipher.doFinal(cipherText);

        return new String(decryptedBytes);
    }
    
    public static PublicKey getPublicKey(byte[] pk) throws NoSuchAlgorithmException, InvalidKeySpecException {
        EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(pk);
        KeyFactory kf = KeyFactory.getInstance(ALGORITHM);
        PublicKey pub = kf.generatePublic(publicKeySpec);
        return pub;
    }

    public static PrivateKey getPrivateKey(byte[] privk) throws NoSuchAlgorithmException, InvalidKeySpecException {
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privk);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        PrivateKey privKey = kf.generatePrivate(keySpec);
        return privKey;
    }

    // Driver code
    public static void main(String args[]) throws Exception {
        springToAngularJS(); 

    }

    

    
    // Encrypt the data by private key and send to angularjs
    private static void springToAngularJS() {
        try {
            /*
            KeyPair keypair = generateRSAKkeyPair();
            
            System.out.println ("-----BEGIN PRIVATE KEY-----");
            System.out.println (Base64.getMimeEncoder().encodeToString( keypair.getPrivate().getEncoded()));
            System.out.println ("-----END PRIVATE KEY-----");
            System.out.println ("-----BEGIN PUBLIC KEY-----");
            System.out.println (Base64.getMimeEncoder().encodeToString( keypair.getPublic().getEncoded()));
            System.out.println ("-----END PUBLIC KEY-----");
*/
            String plainText = "This is the PlainText " + "I want to Encrypt using RSA.";
            
            
            byte[] encodedPublicKey = Base64.getDecoder().decode((PUBKEY.replace(" ", "").getBytes()));
            byte[] encodedPrivateKey = Base64.getDecoder().decode((PVTKEY.replace(" ", "").getBytes()));

            PublicKey publicKey = getPublicKey(encodedPublicKey);
            PrivateKey privateKey = getPrivateKey(encodedPrivateKey);
            
            byte[] cipherText = encrypt(plainText, privateKey);

            //System.out.println( "The Public Key is: " + DatatypeConverter.printHexBinary(keypair.getPublic().getEncoded()));

            //System.out.println( "The Private Key is: " + DatatypeConverter.printHexBinary(keypair.getPrivate().getEncoded()));

            System.out.print("Backend :The Encrypted Text is: ");

            System.out.println(Base64.getEncoder().encodeToString(cipherText));
            
            
            String decryptedText = decrypt(cipherText, publicKey);

            System.out.println("Backend : The decrypted text is: " + decryptedText);

            angularjsToSpringboot(Base64.getEncoder().encodeToString(cipherText));

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    
    //Receive encrypted data from springboot and decrypt by public key
    //After any modification encrypt by public key and sent to springboot
    private static void angularjsToSpringboot(String cipherText) {
        try {
            
            byte[] encodedPublicKey = Base64.getDecoder().decode((PUBKEY.replace(" ", "").getBytes()));
            PublicKey publicKey = getPublicKey(encodedPublicKey);
            String decryptedText = decrypt(Base64.getDecoder().decode(cipherText), publicKey);
            System.out.println("Public : The decrypted text is: " + decryptedText);
            
            byte[] dataEncByPublicKey = encrypt(decryptedText, publicKey);
            System.out.print("Public : The Encrypted Text is: ");
            System.out.println(Base64.getEncoder().encodeToString(dataEncByPublicKey));
            
            decryptBySpringboot(Base64.getEncoder().encodeToString(dataEncByPublicKey));
            //decryptBySpringboot(DatatypeConverter.printHexBinary(dataEncByPublicKey));
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    
    
    //receive encrypted data and decrypt by private key and save to db
    private static void decryptBySpringboot(String cipherText) {
        try {
            
        byte[] encodedPrivateKey = Base64.getDecoder().decode((PVTKEY.replace(" ", "").getBytes()));
        PrivateKey privateKey = getPrivateKey(encodedPrivateKey);
        String decryptedText = decrypt(Base64.getDecoder().decode(cipherText), privateKey);
        System.out.println("Backend : The decrypted text is: " + decryptedText);
        
        
        
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }

    

}

1 Answers1

2

I am generating a asymmetric key pair (public and private) in java using RSA algorithm and trying to use the public key in java-script to decrypt some text

You can not use the public key to decrypt. In Asymmetric encryption only private key can decrypt.

and after modification of data again encrypt the data using same public key sent to spring-boot server , so that spring boot can decrypt using by private key

Why don't you just use HTTPS/SSL protocol instead of doing your own encryption/decryption. Asymmetric encryption is not good for large content anyway.

shazin
  • 21,379
  • 3
  • 54
  • 71