@Test public void testEncryptWrongKey() throws NoSuchAlgorithmException { PublicKey publicKey = generateDsaPublicKey(); try { EncryptionUtil.encryptRsa(publicKey, TEST_SECRET); fail("We have fed DSA private key to RSA decryption. This should have failed"); } catch (ConfigEncryptionException e) { Throwable cause = e.getCause(); assertEquals("Failed to encrypt using RSA key", e.getMessage()); assertSame(InvalidKeyException.class, cause.getClass()); } }
public static String encryptRsa(PublicKey key, String secret) throws ConfigEncryptionException { Objects.requireNonNull(key, "Key must be provided for encryption"); Objects.requireNonNull(secret, "Secret message must be provided to be encrypted"); if (secret.getBytes(StandardCharsets.UTF_8).length > 190) { throw new ConfigEncryptionException("Secret value is too large. Maximum of 190 bytes is allowed."); } try { Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] encrypted = cipher.doFinal(secret.getBytes(StandardCharsets.UTF_8)); return Base64.getEncoder().encodeToString(encrypted); } catch (Exception e) { throw new ConfigEncryptionException("Failed to encrypt using RSA key", e); } }
EncryptionUtil { public static String encryptRsa(PublicKey key, String secret) throws ConfigEncryptionException { Objects.requireNonNull(key, "Key must be provided for encryption"); Objects.requireNonNull(secret, "Secret message must be provided to be encrypted"); if (secret.getBytes(StandardCharsets.UTF_8).length > 190) { throw new ConfigEncryptionException("Secret value is too large. Maximum of 190 bytes is allowed."); } try { Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] encrypted = cipher.doFinal(secret.getBytes(StandardCharsets.UTF_8)); return Base64.getEncoder().encodeToString(encrypted); } catch (Exception e) { throw new ConfigEncryptionException("Failed to encrypt using RSA key", e); } } }
EncryptionUtil { public static String encryptRsa(PublicKey key, String secret) throws ConfigEncryptionException { Objects.requireNonNull(key, "Key must be provided for encryption"); Objects.requireNonNull(secret, "Secret message must be provided to be encrypted"); if (secret.getBytes(StandardCharsets.UTF_8).length > 190) { throw new ConfigEncryptionException("Secret value is too large. Maximum of 190 bytes is allowed."); } try { Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] encrypted = cipher.doFinal(secret.getBytes(StandardCharsets.UTF_8)); return Base64.getEncoder().encodeToString(encrypted); } catch (Exception e) { throw new ConfigEncryptionException("Failed to encrypt using RSA key", e); } } private  EncryptionUtil(); }
EncryptionUtil { public static String encryptRsa(PublicKey key, String secret) throws ConfigEncryptionException { Objects.requireNonNull(key, "Key must be provided for encryption"); Objects.requireNonNull(secret, "Secret message must be provided to be encrypted"); if (secret.getBytes(StandardCharsets.UTF_8).length > 190) { throw new ConfigEncryptionException("Secret value is too large. Maximum of 190 bytes is allowed."); } try { Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] encrypted = cipher.doFinal(secret.getBytes(StandardCharsets.UTF_8)); return Base64.getEncoder().encodeToString(encrypted); } catch (Exception e) { throw new ConfigEncryptionException("Failed to encrypt using RSA key", e); } } private  EncryptionUtil(); static String decryptRsa(PrivateKey key, String encryptedBase64); static String decryptRsaLegacy(Key key, String encryptedBase64); static String encryptRsa(PublicKey key, String secret); static String encryptAes(char[] masterPassword, String secret); static String decryptAesLegacy(char[] masterPassword, String encryptedBase64); static String decryptAes(char[] masterPassword, String encryptedBase64); }
EncryptionUtil { public static String encryptRsa(PublicKey key, String secret) throws ConfigEncryptionException { Objects.requireNonNull(key, "Key must be provided for encryption"); Objects.requireNonNull(secret, "Secret message must be provided to be encrypted"); if (secret.getBytes(StandardCharsets.UTF_8).length > 190) { throw new ConfigEncryptionException("Secret value is too large. Maximum of 190 bytes is allowed."); } try { Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] encrypted = cipher.doFinal(secret.getBytes(StandardCharsets.UTF_8)); return Base64.getEncoder().encodeToString(encrypted); } catch (Exception e) { throw new ConfigEncryptionException("Failed to encrypt using RSA key", e); } } private  EncryptionUtil(); static String decryptRsa(PrivateKey key, String encryptedBase64); static String decryptRsaLegacy(Key key, String encryptedBase64); static String encryptRsa(PublicKey key, String secret); static String encryptAes(char[] masterPassword, String secret); static String decryptAesLegacy(char[] masterPassword, String encryptedBase64); static String decryptAes(char[] masterPassword, String encryptedBase64);  }