title: Copying Byte Array
body: <p>I'm experimenting with AES encryption. However, I get an error I wrote from the following code:</p>
  
  <pre><code>private static byte[] readKey(String request) throws KeyFileNotFoundException,
   UnsupportedEncodingException, UnknownKeyException {
  
      File keyFile = new File(Logging.getCurrentDir() + "\\cikey.key");
      Properties keys = new Properties();
      byte[] storage;
  
      if (!keyFile.exists())
          throw new KeyFileNotFoundException("Key file not located.");
  
      if (keys.containsKey(request) == false)
          throw new UnknownKeyException("Key not found."); //I RECIEVE THIS ERROR
  
      storage = keys.getProperty(request).getBytes(); //read the STRING value and turn into a byte array
  
      return storage;
  }
  </code></pre>
  
  <p>This is the code from the method calling <code>readKey()</code>. I also am having problems <strong>copying</strong> the byte array read in through the <code>readKey()</code> method to the <code>decrypt()</code> method. Please read the comments in the methods for more detailed explanations.</p>
  
  <pre><code>public static String decrypt(String in) throws NoSuchAlgorithmException,
    NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
    IllegalBlockSizeException, BadPaddingException, IOException,
    KeyFileNotFoundException, UnknownKeyException {
  
      String out = " "; //decrypted String to return
  
      byte[] key = readKey("key").clone(); //my attempt to copy a byte array
      SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
  
      byte iv[] = readKey("iv"); //works here? same as above so I don't know.
      IvParameterSpec ivspec = new IvParameterSpec(iv);
  
      //initialize the cipher for decryption
      Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
      cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivspec);
  
      // decrypt the message
      byte[] decrypted = cipher.doFinal(in.getBytes());
  
      out = asHex(decrypted);
  
      return out;
  }
  </code></pre>
  
  <p>Any ideas to fixing this problem?</p>