Removing BadPddingException

Hi all,

Please help me with this problem which is troubling me from last 3-4 days.

There are two files

1)Receiver.java

2)Sender.java

In Receiver.java,I am generating a public and private key using RSA and storing them in two separate files public.dat and private.dat.

Here is the code segment for this.....

import java.io.*;

import java.security.*;

import sun.misc.*;

import javax.crypto.*;

public class Receiver {

public static void main (String[] args) throws Exception {

// generate an RSA key

ObjectOutputStream f=new ObjectOutputStream(new FileOutputStream("public.dat"));

ObjectOutputStream f1=new ObjectOutputStream(new FileOutputStream("private.dat"));

System.out.println( "\nStart generating RSA key" );

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");

keyGen.initialize(1024);

KeyPair keypair = keyGen.generateKeyPair();

f.writeObject(keypair.getPublic());

f.close();

f1.writeObject(keypair.getPrivate());

f1.close();

System.out.println( "Finish generating RSA key" );

Now the public and private keys are in public.dat and private.dat respectively. Now im loading an encypted file called encrypt.txt which was encrypted using RSA public key in Sender.java.

While trying to decrypt it using private.dat, a BadPaddingException is coming.....

Here is the code

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

ObjectInputStream in1 = new ObjectInputStream(new FileInputStream("encrypt.txt"));

ObjectInputStream in2 = new ObjectInputStream(new FileInputStream("private.dat"));

Key key = (Key)in2.readObject();

cipher.init(Cipher.DECRYPT_MODE,key);

byte newMsg1[]=(byte[])in1.readObject();

byte[] newPlainText = cipher.doFinal(newMsg1);

System.out.println( "Finish decryption: " );

System.out.println(new String(newPlainText,"UTF8"));

in1.close();

in2.close();

Also look at Sender.java

import java.util.Scanner;

import java.security.*;

import java.io.*;

import javax.crypto.*;

import sun.misc.*;

public class Sender {

public static void main(String[] args)throws Exception {

Scanner scan=new Scanner(System.in);

System.out.println("Enter any message");

String msg=scan.next();

OutputStream f=new FileOutputStream("plainText.txt");

byte b[]=msg.getBytes();

f.write(b);

f.close();

// encrypt the plaintext using the public key from the file "public.dat"

ObjectInputStream in=new ObjectInputStream(new FileInputStream("public.dat"));

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

Key key=(Key)in.readObject();

System.out.println( "\nStart encryption" );

cipher.init(Cipher.ENCRYPT_MODE, key);

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

ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("encrypt.txt"));

//produce the encypted msg.....

out.writeObject(cipherText);

in.close();

out.close();

System.out.println( "\nFinish encryption: \n"+new String(cipherText,"UTF8") );

}

}

What is the possible solution for this problem?

[3294 byte] By [nikku3881a] at [2007-11-27 5:59:29]
# 1

Worked for me, once I split the key-gen out into its own entity.

The Sender needs the keys first, by the way - if you run your Sender code before you've tried to Receive something, it'll fail because it won't find the keys.

Could it be that you've done something like 'run Sender, have it fail because no keys, run Receiver, have it fail because no message, run Sender, have it work because it sees the keys Receiver laid out there, run Receiver, have it fail because it's generating new keys and using them on the ciphertext Sender encrypted with the old ones'?

I pulled your key-gen code from Receiver into a little helper class:package javaforum;

import java.io.FileOutputStream;

import java.io.ObjectOutputStream;

import java.security.KeyPair;

import java.security.KeyPairGenerator;

public class RSAKeyGen {

public static void main(String[] args) throws Exception{

// generate an RSA key

ObjectOutputStream f = new ObjectOutputStream(new FileOutputStream("public.dat"));

ObjectOutputStream f1 = new ObjectOutputStream(new FileOutputStream("private.dat"));

System.out.println("\nStart generating RSA key");

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");

keyGen.initialize(1024);

KeyPair keypair = keyGen.generateKeyPair();

f.writeObject(keypair.getPublic());

f.close();

f1.writeObject(keypair.getPrivate());

f1.close();

System.out.println("Finish generating RSA key");

}

}

RUN THIS FIRST - then see if Sender/Receiver work.

Note: this will work until your message gets longer than the RSA keysize. If you want a general solution, you need to use a symmetric key to encrypt your data, and only use RSA to encrypt that key. Lots of examples have been given in this forum.

Note the second: please use the [ code ] tags around code samples - it makes it much easier for us to read your code, and hence offer help.

Grant

ggaineya at 2007-7-12 16:36:05 > top of Java-index,Security,Cryptography...