1
votes

So I'm trying to encrypt/decrypt a string using RSA public and private keys. It encrypts fine, but whenever I attempt to decrypt the encrypted string I get a very odd output. For example, "hello" decrypts to:

�]t1��#Z�-�-p���ԏh2�5�~��E'����bh��~�-I�����t�k�>�����+ v���?NA��y�[@���jp!2p�X@oZ$٥�@&�v���=�A��e�A�m��;@d<"&}��� ���}r�q5U������?�J�c����Db�A�!�{�w|�IE�Ì魈S��]�'����88��%������ ��O.N:S�E�J0�4�l�3�ϓn3��{.�

I'm not sure if there's something wrong with my decrypting algorithm or if it has to do with converting the decrypted bytes into a string (or something along those lines).

Here's the decryption method:

public String decryptString(PrivateKey key, String string) {
    try {

        pubDecryptCipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decodedBytes = Base64.decodeBase64(string.getBytes());
        byte[] unencryptedByteArray = pubDecryptCipher.doFinal(decodedBytes);
        return new String(unencryptedByteArray, "UTF8");

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

Encryption:

public String encryptString(PublicKey key, String string) {
    try {
        pubEncryptCipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] eba = pubEncryptCipher.doFinal(string.getBytes("UTF8"));
        byte[] encodedBytes = Base64.encodeBase64(eba);

        return encodedBytes.toString();

    } catch (InvalidKeyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (BadPaddingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    }
    return null;
}

Decrypted string in hex:

efbfbdefbfbdefbfbdc78d2defbfbd6e2fefbfbdefbfbdefbfbd50efbfbd73efbfbd6defbfbd31efbfbd40117defbfbdefbfbd2cefbfbd576ed39c2aefbfbd41533defbfbdefbfbd1a167c23efbfbdd0a11a3213355eefbfbdefbfbdefbfbd6145efbfbd55efbfbdefbfbd7d09efbfbd700814efbfbdefbfbd687b225eefbfbd3c00efbfbdefbfbd3509efbfbd6c5eefbfbdefbfbd79efbfbdefbfbd033d77efbfbd71efbfbd57efbfbd7e76d6a101efbfbd4aefbfbd4a0540efbfbdefbfbd297f6fefbfbd73efbfbdefbfbdefbfbd010defbfbdefbfbdefbfbd00efbfbd490706efbfbdefbfbdefbfbd322435efbfbd02efbfbdefbfbd1dd79477efbfbd13efbfbdd5bb57efbfbdefbfbd0aefbfbd39404b53efbfbdefbfbd06165aefbfbd32efbfbdefbfbd4665efbfbddd9d6aefbfbd0842efbfbdefbfbd7c35efbfbdefbfbdefbfbd3e7eefbfbdefbfbd11efbfbd1fc7b839efbfbd44efbfbdefbfbd1aefbfbd3a314ac48eefbfbd7cefbfbd77efbfbd097449efbfbdcaa8351aefbfbdefbfbd5befbfbdefbfbd655a62efbfbd3651efbfbdefbfbd41efbfbd64efbfbd1150efbfbd65efbfbd224720060cefbfbd11efbfbdefbfbdefbfbdefbfbd0defbfbd1439efbfbdefbfbd

And by string algorithm do you mean:

pubEncryptCipher = Cipher.getInstance("RSA");
pubDecryptCipher = Cipher.getInstance("RSA");

?

1
Could you show us the encryption code, the algorithm string and the result in hexadecimals? It looks like you are using "RSA/ECB/NoPadding" which is always the incorrect algorithm string; the padding mode is an important part of RSA encryption/decryption. - Maarten Bodewes
@owlstead alright updated - Embattled Swag

1 Answers

0
votes

The issue is that encodedBytes.toString() does not do what you expect it to do. You need to do new String(encodedBytes, StandardCharsets.ASCII); instead. Or new String(encodedBytes, Charset.forName("ASCII")); before Java SE 1.7 compatible code.

You should always indicate the padding mode; please use e.g. "RSA/None/PKCS1Padding" instead of "RSA".