title: Android: Java: Decrypt AES using key instead of password
body: <p>I'm using a snippet to decrypt an AES file.</p>
  
  <p>However, i'd like to use a key file instead of a password to decrypt the file.</p>
  
  <p>What changes do i need to make in the code below to make that happen?</p>
  
  <pre><code>import javax.crypto.Cipher;
  import javax.crypto.KeyGenerator;
  import javax.crypto.SecretKey;
  import javax.crypto.spec.SecretKeySpec;
  
  public class AESCrypto {
  
      public static String encrypt(String seed, String cleartext) throws Exception {
          byte[] rawKey = getRawKey(seed.getBytes());
          byte[] result = encrypt(rawKey, cleartext.getBytes());
          return Converters.toHex(result);
      }
  
      public static String decrypt(String seed, String encrypted) throws Exception {
          byte[] rawKey = getRawKey(seed.getBytes());
          byte[] enc = Converters.toByte(encrypted);
          byte[] result = decrypt(rawKey, enc);
          return new String(result);
      }
  
      public static byte[] getRawKey(byte[] seed) throws Exception {
          KeyGenerator kgen = KeyGenerator.getInstance("AES");
          SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
          sr.setSeed(seed);
          try {
              kgen.init(256, sr);
              } catch (Exception e) {
              // Log.w(LOG, "This device doesn't support 256 bits, trying 192 bits.");
              try {
              kgen.init(192, sr);
              } catch (Exception e1) {
              // Log.w(LOG, "This device doesn't support 192 bits, trying 128 bits.");
              kgen.init(128, sr);
              }
          }
          SecretKey skey = kgen.generateKey();
          byte[] raw = skey.getEncoded();
          return raw;
      }
  
      private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
          SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
              Cipher cipher = Cipher.getInstance("AES");
          cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
          byte[] encrypted = cipher.doFinal(clear);
              return encrypted;
      }
  
      private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
          SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
              Cipher cipher = Cipher.getInstance("AES");
          cipher.init(Cipher.DECRYPT_MODE, skeySpec);
          byte[] decrypted = cipher.doFinal(encrypted);
              return decrypted;
      }
  }
  </code></pre>