title: Java: How to create a RSA Public Key from the String
body: <p>I have the byte array of the RSA Public Key. I found on the internet that I can create a real PublicKey object of it by using this code:</p>
  
  <pre><code>PublicKey publicKey = 
      KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(bytes));
  </code></pre>
  
  <p>But every time I run this code, I'm getting another result for the encrypted data using that key. I'm sure the data I want to encrypt is always the same, so does the byte array representing the key.</p>
  
  <p>Is this normal?</p>
  
  <p>Here is my code always producing another output:</p>
  
  <pre><code>byte[] keyBytes = Base64.decodeBase64(rsa_1024_public_key);
        // rsa_1024_public key is a constant String
  
  Cipher c = Cipher.getInstance("RSA");
  
  PublicKey publicKey =
     KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(keyBytes));
  
  c.init(Cipher.ENCRYPT_MODE, publicKey);
  
  return c.doFinal(password.getBytes());
  </code></pre>
  
  <p>This is probably a part of the asymmetric encryption algorithm?</p>
  
  <p>Thanks.</p>