RSA encryption/ Decryption in a client server application

Posted by user308806 on Stack Overflow See other posts from Stack Overflow or by user308806
Published on 2010-05-09T12:47:48Z Indexed on 2010/05/09 12:58 UTC
Read the original article Hit count: 277

Filed under:

Hi guys, probably missing something very straight forward on this, but please forgive me, I'm very naive!

Have a client server application where the client identifies its self with an RSA encrypted username & password.

Unfortunately I'm getting a "bad padding exception: data must start with zero" when i try to decrypt with the public key on the client side.

I'm fairly sure the key is correct as I have tested encrypting with public key then decrypting with private key on the client side with no problems at all. Just seems when I transfer it over the connection it messses it up somehow?!

Using PrintWriter & BufferedReader on the sockets if thats of importance. EncodeBASE64 & DecodeBASE64 encode byte[] to 64base and vice versa respectively.

Any ideas guys?? Client side:

        Socket connectionToServer = new Socket("127.0.0.1", 7050);
        InputStream in = connectionToServer.getInputStream();
        DataInputStream dis = new DataInputStream(in);
        int length = dis.readInt();
        byte[] data = new byte[length];
      //  dis.readFully(data);
        dis.read(data);
        System.out.println("The received Data*****************************************");
        System.out.println("The length of bits "+ length);
        System.out.println(data);
        System.out.println("***********************************************************");
          Decryption d = new Decryption();
          byte [] ttt = d.decrypt(data);
        System.out.print(data);
        String ss = new String(ttt);
        System.out.println("***********************");
        System.out.println(ss);
        System.out.println("************************");

Server Side:

       in = connectionFromClient.getInputStream();
        OutputStream out = connectionFromClient.getOutputStream();
        DataOutputStream dataOut = new DataOutputStream(out);
        LicenseList licenses = new LicenseList();
        String ValidIDs = licenses.getAllIDs();
        System.out.println(ValidIDs);
        Encryption enc = new Encryption();
        byte[] encrypted = enc.encrypt(ValidIDs);
        byte[] dd = enc.encrypt(ValidIDs);
        String tobesent = new String(dd);
        //byte[] rsult =  enc.decrypt(dd);
        //String tt = String(rsult);
        System.out.println("The sent data**********************************************");
        System.out.println(dd);
        String temp = new String(dd);
        System.out.println(temp);
        System.out.println("*************************************************************");
        //BufferedWriter bf = new BufferedWriter(OutputStreamWriter(out));
        //dataOut.write(ValidIDs.getBytes().length);
        dataOut.writeInt(ValidIDs.getBytes().length);
        dataOut.flush();
        dataOut.write(encrypted);
        dataOut.flush();
        System.out.println("********Testing**************");
        System.out.println("Here are the ids:::");
        System.out.println(licenses.getAllIDs());
        System.out.println("**********************");
        //bw.write("it is working well\n");

© Stack Overflow or respective owner

Related posts about java