Hinok Wong
Hinok Wong

Reputation: 1

RSA with AES encryption and decryption

What is the problem of my decryption for RSA?

here is the code for encryption :

    try {
        //Get the public key from the keyStore and set up the Cipher object
        PublicKey publicKey = getPubKey(keyStore,keyName);
        Cipher rsaCipher = Cipher.getInstance("RSA");
        rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);

        //Read the plainText
        System.out.println("Loading plaintext file: "+inFile); 
        RandomAccessFile rawDataFromFile = new RandomAccessFile(inFile, "r");
        byte[] plainText = new byte[(int)rawDataFromFile.length()];
        rawDataFromFile.read(plainText);

        // Generate a symmetric key to encrypt the data and initiate the AES Cipher Object
        System.out.println("Generating AES key"); 
        KeyGenerator sKenGen = KeyGenerator.getInstance("AES"); 
        Key aesKey = sKenGen.generateKey();
        Cipher aesCipher = Cipher.getInstance("AES");
        aesCipher.init(Cipher.ENCRYPT_MODE, aesKey);

        // Encrypt the symmetric AES key with the public RSA key
        System.out.println("Encrypting Data"); 
        byte[] encodedKey = rsaCipher.doFinal(aesKey.getEncoded()); 
        // Encrypt the plaintext with the AES key
        byte[] cipherText = aesCipher.doFinal(plainText);

        //Write the encrypted AES key and Ciphertext to the file.
        System.out.println("Writting to file: "+outFile);
        FileOutputStream outToFile = new FileOutputStream(outFile);
        outToFile.write(encodedKey);
        outToFile.write(cipherText);

        System.out.println("Closing Files");
        rawDataFromFile.close();
        outToFile.close();
    }
    catch (Exception e) { 
        System.out.println("Doh: "+e); 
    }

and here is my code for decryption, i thought it will work quite well but it didnt. anyone can help me ?

it kept having the error : javax.crypto.BadPaddingException: Decryption error

dont really know what to do, anyone can give me some advices?

private static void decryptRSA() {
    try {
        System.out.println("Loading plaintext file: "+inFile); 
        RandomAccessFile rawDataFromFile = new RandomAccessFile(inFile, "r");
        byte[] cipherText = new byte[(int)rawDataFromFile.length()];
        byte encodedkey[] = new byte[256];
        rawDataFromFile.read(encodedkey, 0, 256);
        rawDataFromFile.read(cipherText);

        PublicKey publicKey = getPubKey(keyStore,keyName);
        Cipher rsaCipher = Cipher.getInstance("RSA");
        rsaCipher.init(Cipher.DECRYPT_MODE, publicKey);

        byte[] aeskey = rsaCipher.doFinal(encodedkey);
        SecretKeySpec aesKey = new SecretKeySpec(aeskey, "AES");
        Cipher aesCipher = Cipher.getInstance("AES");
        aesCipher.init(Cipher.DECRYPT_MODE, aesKey);

        byte[] plainText = aesCipher.doFinal(cipherText);

        System.out.println("Writting to file: "+outFile);
        FileOutputStream outToFile = new FileOutputStream(outFile);
        outToFile.write(plainText);
        System.out.println("Closing Files");
        rawDataFromFile.close();
        outToFile.close();
    }
    catch (Exception e) { 
        System.out.println("Doh: "+e); 
    }
}

Upvotes: 0

Views: 2630

Answers (2)

Dhruv
Dhruv

Reputation: 1

package rsa;

import java.math.BigInteger;
import java.util.Scanner;

public class RSA {

    public static void main(String[] args) {
        // TODO code application logic here
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter Plaintext");
        String plaintext=sc.nextLine();

        BigInteger n,n1,n2,M=BigInteger.valueOf(0),M1,p,q,pn,e,d,c;

        System.out.println("Enter p q");
        p=sc.nextBigInteger();
        q=sc.nextBigInteger();
        n=p.multiply(q);

        n1=p.subtract(BigInteger.valueOf(1));
        n2=q.subtract(BigInteger.valueOf(1));
        pn=n1.multiply(n2);

        System.out.println("Choose e");
        e=sc.nextBigInteger();
        d=e.modInverse(pn);
        System.out.println("D is"+d);

        plaintext=plaintext.toLowerCase();
        char arr[]=new char[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

        if(plaintext.length()%2!=0)
            plaintext=plaintext+"a";

        String cc="",s="",plain="",t="";
        int z;
        for(int i=0;i<plaintext.length();i=i+2)
        {
            z=plaintext.codePointAt(i)-87;
            s=s+z;
            z=plaintext.codePointAt(i+1)-87;
            s=s+z;
            M=BigInteger.valueOf(Long.parseLong(s));
            t=t+M.toString();

            c=M.pow(e.intValue());
            c=c.mod(n);
            cc=cc+c;     
            s="";

            M1=c.pow(d.intValue());
            M1=M1.mod(n);
            plain=plain+M1.toString();

        }
        System.out.println("Plaintext is "+plaintext);
        System.out.println("Before Encryption "+t);
        System.out.println("cipher "+cc);
        System.out.println("First Decryption "+plain);

        String h="";
        for(int i=0;i<plain.length();i=i+2)
        {
            int k=Integer.parseInt(Character.toString(plain.charAt(i))+Character.toString(plain.charAt(i+1)));
            h=h+arr[k-10];
        }
        System.out.println("Decryption "+h);

    }

}

Upvotes: 0

user207421
user207421

Reputation: 311054

  1. RSA decryption is done with the private key, not the public key.

  2. The length of the cipherText array in the decryption code isn't correct. You should subtract 256, or pass the actual read length to Cipher.doFinal(), or in fact both.

NB Despite the message you're printing, your decrypt step is really reading from the ciphertext file, not the plaintext file.

Upvotes: 2

Related Questions