title: How to fix the Base64 Errors I am getting?
body: <p>After reading a lot about AES, JCE and its unlimited strength policy files for 256 bit key, and this <a href="http://java.sun.com/developer/technicalArticles/Security/AES/AES_v1.html" rel="nofollow">Oracle/Sun guide</a> I've implemented this.</p>
  
  <p><strong>UPDATE</strong> </p>
  
  <p>I combined the code provided by sun and made a java class
  AESencrp.java</p>
  
  <pre><code>/**
   *
   * @author MUDASSIR
   */
  
  import java.security.*;
  import javax.crypto.*;
  import javax.crypto.spec.SecretKeySpec;
  import sun.misc.*;
  
  public class AESencrp {
  
       private static final String ALGO = "AES";
  //    private static final byte[] keyValue = 
  //        new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't',
  //'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };
  
  public static String encrypt(String Data, byte[] keyValue) throws Exception {
          Key key = generateKey(keyValue);
          Cipher c = Cipher.getInstance(ALGO);
          c.init(Cipher.ENCRYPT_MODE, key);
          byte[] encVal = c.doFinal(Data.getBytes());
          String encryptedValue = new BASE64Encoder().encode(encVal);
          return encryptedValue;
      }
  
      public static String decrypt(String encryptedData, byte[] keyValue) throws Exception {
          Key key = generateKey(keyValue);
          Cipher c = Cipher.getInstance(ALGO);
          c.init(Cipher.DECRYPT_MODE, key);
          byte[] decodedValue = new BASE64Decoder().decodeBuffer(encryptedData);
          byte[] decValue = c.doFinal(decodedValue);
          String decryptedValue = new String(decValue);
          return decryptedValue;
      }
      private static Key generateKey(byte[] keyValue) throws Exception {
          Key key = new SecretKeySpec(keyValue, ALGO);
          return key;
  }
  
  }
  </code></pre>
  
  <p>This works just fine, but whenever i build my project it give the warning that <br />"Base64encoder is a proprietary software and must be removed in the future release".</p>
  
  <p>When i removed the base64 encoder and used the asHex method instead(as provided by the sun guide <a href="http://java.sun.com/developer/technicalArticles/Security/AES/AES_v1.html" rel="nofollow">here</a>), it gave me a BadPadding exception.<br />
  Exception in thread "main" javax.crypto.BadPaddingException: Given final block not properly padded</p>
  
  <p>This is my code without base64 encoder</p>
  
  <pre><code>/**
   *
   * @author MUDASSIR
   */
  
  import java.security.*;
  import javax.crypto.*;
  import javax.crypto.spec.SecretKeySpec;
  import sun.misc.*;
  
  public class AESencrp2 {
  
       private static final String ALGO = "AES";
  //    private static final byte[] keyValue = 
  //        new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't',
  //'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };
  
       public static String asHex(byte buf[]) {
          StringBuilder strbuf = new StringBuilder(buf.length * 2);
          int i;
  
          for (i = 0; i &lt; buf.length; i++) {
              if (((int) buf[i] &amp; 0xff) &lt; 0x10) {
                  strbuf.append("0");
              }
  
              strbuf.append(Long.toString((int) buf[i] &amp; 0xff, 16));
          }
  
          return strbuf.toString();
      }
  
  public static String encrypt(String Data, byte[] keyValue) throws Exception {
          Key key = generateKey(keyValue);
          Cipher c = Cipher.getInstance(ALGO);
          c.init(Cipher.ENCRYPT_MODE, key);
          byte[] encryptedValue = c.doFinal(Data.getBytes());
          return asHex(encryptedValue);
      }
  
      public static String decrypt(String encryptedData, byte[] keyValue) throws Exception {
          Key key = generateKey(keyValue);
          Cipher c = Cipher.getInstance(ALGO);
          c.init(Cipher.DECRYPT_MODE, key);
          byte[] decValue = c.doFinal(encryptedData.getBytes());
          String decryptedValue = new String(decValue);
          return decryptedValue;
      }
      private static Key generateKey(byte[] keyValue) throws Exception {
          Key key = new SecretKeySpec(keyValue, ALGO);
          return key;
  }
  
  }
  </code></pre>
  
  <p>This is the main, where i try the code</p>
  
  <pre><code>public static void main(String args[]) throws Exception {
  
          byte[] keyValue = new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't',
  'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };
  
          String password = "This is the data that is going to be encrypted";
  
          String passwordEnc = AESencrp.encrypt(password, keyValue);
          //String passwordEnc = AESencrp2.encrypt(password, keyValue);
  
          String passwordDec = AESencrp.decrypt(passwordEnc, keyValue);
          //String passwordDec = AESencrp.decrypt(passwordEnc, keyValue);
  
          System.out.println("Plain Text : " + password);
          System.out.println("Encrypted Text : " + passwordEnc);
          System.out.println("Decrypted Text : " + passwordDec);
  } 
  </code></pre>
  
  <p>Problem, </p>
  
  <ol>
  <li>1 - If i use base64 encoder, its proprietary.</li>
  <li>2 - If is use asHex method provided by sun, i get bad padding exception.</li>
  </ol>
  
  <p>Help please.</p>