title: Suitable replacement for decodeBuffer Method in Commons Codec
body: <p>I have a piece of code which encrypts and decrypts using AES algorithm, which was using sun.misc.* packages.</p>
  
  <p>Later i came to know it is wrong to use those set of packages which made me follow the advice of using Apache's  Commons Codec which is effective.</p>
  
  <p><strong>The previous code was as below :</strong></p>
  
  <pre><code>import java.security.*;
  import java.security.spec.InvalidKeySpecException;
  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) throws Exception {
      Key key = generateKey();
      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) throws Exception {
      Key key = generateKey();
      Cipher c = Cipher.getInstance(ALGO);
      c.init(Cipher.DECRYPT_MODE, key);
      byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
      byte[] decValue = c.doFinal(decordedValue);
      String decryptedValue = new String(decValue);
      return decryptedValue;
  }
  
  private static Key generateKey() throws Exception {
      Key key = new SecretKeySpec(keyValue, ALGO);
      return key;
  }
  
  
  
  }
  </code></pre>
  
  <p>As suggested I have removed sun.misc and did the following alteration.</p>
  
  <p><strong>After replacing BASE64Encoder class with Base64 in Apache's commons codec:</strong></p>
  
  <pre><code> public static String encrypt(String Data) throws Exception {
              Key key = generateKey();
              Cipher c = Cipher.getInstance(ALGO);
              c.init(Cipher.ENCRYPT_MODE, key);
              byte[] encVal = c.doFinal(Data.getBytes());
              byte[] encryptedValue = new Base64().encode(encVal);
              return new String(encryptedValue);
          }
  </code></pre>
  
  <p>I am not able to find a suitable replacement for decryption as i am struck at line: </p>
  
  <pre><code>byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
  </code></pre>
  
  <p>I am not finding any method which does the job of decodeBuffer(String encryptedData) and returns me a byte array of decodedValue. </p>