For this, we’ll need another Cipher instance. Now, let’s see how we can decrypt the message to its original form. This way, the message will be more readable and easier to work with. If we’d like to store it in a database or send it via REST API, it would be more convenient to encode it with the Base64 Alphabet: String encodedMessage = Base64.getEncoder().encodeToString(encryptedMessageBytes) Now, our message is successfully encoded.
Note that it accepts only byte array arguments, so we need to transform our string before: byte secretMessageBytes = secretMessage.getBytes(StandardCharsets.UTF_8) )īyte encryptedMessageBytes = encryptCipher.doFinal(secretMessageBytes)
Having that ready, we can invoke the doFinal method to encrypt our message.
Cipher encryptCipher = Cipher.getInstance('RSA') ĮncryptCipher.init(Cipher.ENCRYPT_MODE, publicKey)