Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem sending encrypted text using Bouncy Castle

843810Sep 7 2003 — edited Sep 8 2003
Hi, I am a newbie in Java and Security.
I am trying to write a program that takes some text, encrypt it using RSA encryption, sends it over to another program running in the same network which decrypts the text.

My problem is, when I'm using the Bouncy Castle provider for the RSA encryption, the sent encrypted msg is different from the one received at the other end.
From my observation, all the "?" character changed to this "�"(square), and only beginning of the msg got thru...

Tried using DES encryption with default JCE provider, it works ok...
Has anyone experienced this problem with Bouncy Castle?
I need to use RSA public key encryption, which JCE doesn't have...

Here the code for the "client" that does the encryption
import java.io.*;
import java.net.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;

public class myclient {
    public static void main(String argv[]) throws Exception {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

	    String temp, msg=null;
	    Socket client=null;
	    BufferedReader in = new BufferedReader(new InputStreamReader(System.in) );

	    try {
	        client = new Socket("127.0.0.1",6789);
	    }
	    catch (UnknownHostException e) {
	        System.out.println(e);
	        System.exit(1);
	    }

	    try {
			DataOutputStream out = new DataOutputStream(client.getOutputStream());

			msg = in.readLine();

            System.out.println("Org Msg="+msg);
			FileInputStream fis = new FileInputStream ("pubkey.dat");
			ObjectInputStream ois = new ObjectInputStream (fis);
			PublicKey pub = (PublicKey)ois.readObject ();

			System.out.println( "\nStart encryption" );
			Cipher enc_cipher = Cipher.getInstance("RSA","BC");
			System.out.println( "\n" + enc_cipher.getProvider().getInfo() );
			enc_cipher.init(Cipher.ENCRYPT_MODE, pub);

			byte[] msgbyte = msg.getBytes("ASCII");

			ByteArrayOutputStream bout = null;
	        try {
	            int bzise = enc_cipher.getBlockSize();
	            bout = new ByteArrayOutputStream();
	            int size = enc_cipher.getBlockSize();
	            int rest = 0;
	            for (int t = 0; t < msgbyte.length; t += size) {
	                if (msgbyte.length - t <= size) {
	                    rest = msgbyte.length - t;
	                } else {
	                    rest = size;
	                }
	                bout.write(enc_cipher.doFinal(msgbyte, t, rest));
	            }
	            bout.flush();
	            bout.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        } catch (IllegalStateException e) {
	            e.printStackTrace();
	        } catch (IllegalBlockSizeException e) {
	            e.printStackTrace();
	        } catch (BadPaddingException e) {
	            e.printStackTrace();
	        }

			byte[] cipherbyte = bout.toByteArray();
			System.out.println( "Finish encryption: " );
			String cipher = new String(cipherbyte,"ASCII");
			out.writeBytes(cipher);
            System.out.println("Ciphered Msg="+cipher);
	        client.close();
		}
		catch (IOException e) {
	        System.out.println(e);
	    }
    }
}
Here is a simple "server" program I wrote to check the received encrypted msg
import java.io.*;
import java.net.*;

public class server {

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

		 String clientSentence;
		 ServerSocket welcome = new ServerSocket(6789);

		 while(true) {
			 Socket connection = welcome.accept();
			 BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			 clientSentence = in.readLine();
			 System.out.println(clientSentence);
		 }
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 6 2003
Added on Sep 7 2003
6 comments
264 views