Why do encryped RSA bytes in Java variate?

Paul :

I try to code a hybrid encryption to communicate between server and client. So I send a public RSA key from the client the server enrypts his AES key and sends it back. But then if i decrypt it on client site, the key is longer than i send it and i don't know why...

Here the code:

Client:

socket.getOutputStream().write(security.getKeyPair().getPublic().getEncoded());
byte[] keyBuffer = new byte[512];
socket.getInputStream().read(keyBuffer);
security.setKey(new SecretKeySpec(security.decryptRSA(keyBuffer).getBytes(), "AES"));

Server:

byte[] keyBuffer = new byte[550];
this.socket.getInputStream().read(keyBuffer);
PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(keyBuffer));
this.socket.getOutputStream().write(this.security.encryptRSA(this.security.getKey().getEncoded(), publicKey));

Security class methods:

public byte[] encryptRSA(byte[] message, PublicKey key) {
        byte[] buffer = message;
        try {
            this.cipher = Cipher.getInstance("RSA");
            this.cipher.init(Cipher.ENCRYPT_MODE, key);
            buffer = this.cipher.doFinal(buffer);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }

        return buffer;
    }


public String decryptRSA(byte[] message) {
        byte[] buffer = message;
        try {
            this.cipher = Cipher.getInstance("RSA");
            this.cipher.init(Cipher.DECRYPT_MODE, this.keyPair.getPrivate());
            buffer = this.cipher.doFinal(buffer);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }

        return new String(buffer);
    }


Thanks!

Maarten Bodewes :

The problem is likely that you stringified your code needlessly:

   return new String(buffer);

This will interpret the buffer in a certain way as a string. However, the key consists of random bytes, which may decode to unexpected characters. Then you revert back to bytes using .getBytes(), but by then it is too late.

Simply leave the AES key to be bytes and that part should be fixed.


Besides that, read is not the same as readNBytes​ (and just read is wrong), but that's probably not the error; you'd get into trouble with RSA, not the AES key if it would be the problem.


Note that you can retrieve the modulus size in bytes to determine the amount of bytes to read, so you don't need to have a 512 constant there either (which I would write as KEY_SIZE / Byte.SIZE where KEY_SIZE = 4096 to at least indicate that the buffer is the same as the key size clearly.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=238459&siteId=1