title: Is it safe to use RSA private key to encrypt a message
body: <p>I want to distribute encrypted message, that only I can encrypt, but everybody can decrypt. I am using following code to perform the task. It works but I want to know if there are any security issues with it? </p>
  
  <p>I know that normal usage is to use RSA public key to encrypt and private key to decrypt (but my use case is the reverse). Also I don't want to use java.security.Signature here.</p>
  
  <p>Use-case:
  I want to send configuration files from server to clients via email without the recipient being able to read the configuration in clear text. The application which had the server public key during the installation will be able to decrypt the configuration file once it is imported into configuration directory.</p>
  
  <pre><code>public static final String ALGORITHM = "RSA/ECB/PKCS1Padding";
  
  public Cipher createCipher(final int encryptionMode, final Key key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
      final Cipher cipher = Cipher.getInstance(ALGORITHM);
      cipher.init(encryptionMode, key);
      return cipher;
  }
  
  public byte[] encryptString(final String text, final PrivateKey privateKey) throws GeneralSecurityException, IOException {
      return createCipher(Cipher.ENCRYPT_MODE, privateKey).doFinal(text.getBytes("UTF-8"));
  }
  
  public String decryptString(final byte[] msg, final PublicKey publicKey) throws GeneralSecurityException, IOException {
      final byte[] decrypted = createCipher(Cipher.DECRYPT_MODE, publicKey).doFinal(msg);
      return new String(decrypted, "UTF-8");
  }
  
  // me 
  final PrivateKey privateKey = ... read from file ...
  final byte[] msg = encryptString("my-secret-text-that-everybody-can-read-but-only-I-can-generate", privateKey);
  
  // other person
  final PublicKey publicKey = ... read from file ...
  final String text = decryptString(msg, publicKey));
  </code></pre>