title: RSA encyrption - converting between bytes array and String
body: <p>I am trying to implement RSA encryption which is able to do the following:</p>
  
  <ul>
  <li>accept a string value as an input to encrypt using a public key</li>
  <li>return the encrypted cipher as string</li>
  <li>accept the encrypted cipher as an input to decrypt using a private key</li>
  <li>return the original value, decrypted</li>
  </ul>
  
  <p>I am able to get the encryption/decryption working if I directly decrypt the <code>byte</code> array returned by the encryption, but annot seem to get it to work if I parse the <code>byte</code> array to a <code>String</code> and then back to <code>byte</code>s again.</p>
  
  <p>The following code <strong>does</strong> work:</p>
  
  <pre><code>cipher.init(Cipher.ENCRYPT_MODE, pubKey);
  byte[] cipherBytes = cipher.doFinal(input);
  System.out.println("cipher: " + new String(cipherBytes));
  returnValue += new String(cipherBytes);
  
  cipher.init(Cipher.DECRYPT_MODE, privKey);
  byte[] plainText = cipher.doFinal(cipherBytes);
  System.out.println("plain : " + new String(plainText));
  </code></pre>
  
  <p>The following code <strong>does NOT</strong> work:</p>
  
  <pre><code>byte[] cipherBytes = cipher.doFinal(input);
  System.out.println("cipher: " + new String(cipherBytes));
  returnValue += new String(cipherBytes);
  
  String cipherText = new String(cipherBytes);
  byte[] reCipherBytes = cipherText.getBytes();
  
  cipher.init(Cipher.DECRYPT_MODE, privKey);
  byte[] plainText = cipher.doFinal(reCipherBytes);
  System.out.println("plain : " + new String(plainText));
  </code></pre>
  
  <p>Can anyone advise what I'd need to do to get the second version to work successfully?</p>