title: Encrypting and Decrypting password in java
body: <p>Im Trying to do my homework to create a class called Password that implements the Encryptable interface.</p>
  
  <p>Im trying using RSA Algorythm.</p>
  
  <p>I use some RSA code references from the Google and resulting my code below.</p>
  
  <pre><code>import java.security.KeyPair;
  import java.security.KeyPairGenerator;
  import java.security.Key;
  import java.util.Arrays;
  import javax.crypto.Cipher;
  import java.util.Scanner;
  public class Password
  {
      public static void main(String[] args) 
      {
          Scanner sc = new Scanner(System.in);
          String password = sc.nextLine();
          KeyPair keyPair = RSAKeyPair.keyPairRSA();
          Key publicKey = keyPair.getPublic();
          Key privateKey = keyPair.getPrivate();
          System.out.println("Original: " + password);
          byte[] encrypted = RSAEncryptDecrypt.encrypt(password, privateKey);
          System.out.println("Encrypted: " + new String(encrypted));
          byte[] decrypted = RSAEncryptDecrypt.decrypt(encrypted, publicKey);
          System.out.println("Decrypted: " + new String(decrypted));
      }
  }
  final class RSAConstants {
      private RSAConstants() {
      }
      public static final String ALGORITHM = "RSA";
      public static final int ALGORITHM_BITS = 2048;
  }
  class RSAKeyPair {
      public static KeyPair keyPairRSA() {
          KeyPairGenerator generator = null;
          try {
              generator = KeyPairGenerator.getInstance(RSAConstants.ALGORITHM);
          } catch (Exception e) {
              e.printStackTrace();
          }
          if (generator != null) {
              generator.initialize(RSAConstants.ALGORITHM_BITS);
              KeyPair keyPair = generator.genKeyPair();
              return keyPair;
          }
          return null;
      }
  }
  
  class RSAEncryptDecrypt {
      public static byte[] encrypt(String original, Key privateKey) {
          if (original != null &amp;&amp; privateKey != null) {
              byte[] bs = original.getBytes();
              byte[] encData = convert(bs, privateKey, Cipher.ENCRYPT_MODE);
              return encData;
          }
          return null;
      }
      public static byte[] decrypt(byte[] encrypted, Key publicKey) {
          if (encrypted != null &amp;&amp; publicKey != null) {
              byte[] decData = convert(encrypted, publicKey, Cipher.DECRYPT_MODE);
              return decData;
          }
          return null;
      }
      private static byte[] convert(byte[] data, Key key, int mode) {
          try {
              Cipher cipher = Cipher.getInstance(RSAConstants.ALGORITHM);
              cipher.init(mode, key);
              byte[] newData = cipher.doFinal(data);
              return newData;
          } catch (Exception e) {
              e.printStackTrace();
          }
          return null;
      }
  }
  </code></pre>
  
  <p>My Input is:</p>
  
  <p><code>InterstellarGalactica
  </code></p>
  
  <p>All goes smooth except for the result of Encrypted Password
  Resulting below</p>
  
  <pre><code>Original: InterstellarGalactica
  Encrypted: Sªë/H?ù,X?U4??A???ìñáQ
  ÷?      *?7*??d?'å?Ñ¡w °??? Pè???«{?D÷??cB???'É »???qªîÉDë??~hb??z8?çÿ?hí?{mè?{*îèGê??WÅ{x??ï.5¼?úü;e??G?-F?shèn?FI
  áh`UƒIàB!?åäô+D&lt;&amp;"?)?????ß!??3ä?¬???â???&lt;?¬Ü?{ @ó12B?òt?ƒòÆr²Ä·oHQ?ë?«ú?°?î??Äy?:X^&lt;?
  &amp;:ryb\?¼
  Decrypted: InterstellarGalactica
  </code></pre>
  
  <p>Why do it is became a meaningless character?</p>
  
  <p>Is there anything wrong with my code?</p>
  
  <p>Can you explain how to do it in proper way(if there is)?</p>
  
  <p>Thanks in advance.</p>