I'm doing a small school "project" that is a small chat program. You start the server and the server sends a public key to the client so that the client can send back encrypted data.
The problem is that when I try to decrypt it on the server side I recieve a error.
javax.crypto.BadPaddingException: Decryption error at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:380) at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291) My server awaits for a socket.accept() and when a client connect it sends a public key to this socket.
(This is not the whole code)
private Client client; private JSONObject json; //Connector is used as a listener for messages. public Connector(Socket socket, ServerBroadCast SBC) throws IOException { DIS = new DataInputStream(socket.getInputStream()); this.SBC = SBC; this.client = SBC.AddClient(socket); //Sending public RSA key to client to use to encrypt their message this.client.sendPublicKey(RSA.getPublicKey()); //This is where I am sending RSA public key to client } I'm just sending one message back to server to see if it's encrypted and if the server can decrypt it.
Using this class to send to CLIENT
DataOutputStream DOS; public Client(Socket s) throws IOException { DOS = new DataOutputStream(s.getOutputStream()); } public void sendPublicKey(PublicKey publicKey) throws IOException { JSONObject json = new JSONObject(); json.put(JSONKeys.TYPE, JSONTypes.PUBLIC_KEY); json.put(JSONKeys.MESSAGE, RSA.getEncodedPublicKey()); this.send(json); } public void send(JSONObject json) throws IOException{ this.DOS.writeUTF(json.toString()); System.out.println(json); } And here is my RSA class to encrypt and decrypt
private static PrivateKey privateKey = null; private static PublicKey publicKey = null; public static void generateKeyPair() { KeyPairGenerator keyPairGenerator = null; KeyPair keyPair = null; try { keyPairGenerator = KeyPairGenerator.getInstance("RSA"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } keyPairGenerator.initialize(1024); keyPair = keyPairGenerator.generateKeyPair(); RSA.privateKey = keyPair.getPrivate(); RSA.publicKey = keyPair.getPublic(); } public static String getEncodedPublicKey() { return (new String(Base64.encode(RSA.getPublicKey().getEncoded()))); } public static String encrypt(String string) { byte[] encrypted = null; try { Cipher cipher = Cipher.getInstance("RSA");//174 bytes cipher.init(Cipher.ENCRYPT_MODE, RSA.getPublicKey()); encrypted = cipher.doFinal(string.getBytes()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return (new String(Base64.encode(encrypted))); } public static String decrypt(String string) { byte[] decrypted = null; try { Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.DECRYPT_MODE, RSA.getPrivateKey()); decrypted = cipher.doFinal(Base64.decode(string)); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (Base64DecodingException e) { e.printStackTrace(); } return (new String(decrypted)); } I get that there is something "wrong" with my padding since the error says that but I don't understand what.
The length of what I get back from the clients encrypted message is 174 byte long. I've understood that it is to long, but why does it create such a huge encrypted message from my plain text.
private final String MESSAGE = "Hello from client side";
public static String getEncodedPublicKey() { return (new String(Base64.encode(RSA.getPublicKey().getEncoded()))); }That is what I am using