Java RSA Encryption

后端 未结 3 1749
灰色年华
灰色年华 2021-02-04 13:42

I am trying to encode a simple String \"test\" back and forth.

public static String encode(Key publicKey, String data) throws NoSuchAlgorithmException, NoSuchPad         


        
3条回答
  •  孤城傲影
    2021-02-04 14:24

    You can't reliably convert random bytes to a String. The results will depend on what your default character encoding is on the machine where you run this. With many encodings, the cipher text will be corrupted, and information will be lost.

    Modify your code to use a byte[] instead (the result of the 'doFinal()` method.

    If you need to convert the byte[] to a character string, use an encoding like Base-64.

提交回复
热议问题