title: File Encryption with AES in Java decrtypion in C++ using cryptopp
body: <p>I'm experimenting with Crypto++ library and javax.crypto library.</p>
  
  <p>So what I'm trying to do is... encrypting a text file using AES with CFB mode in java, and then decrypt the encrypted file using crypto++ in C++</p>
  
  <p>here is the java class for encryption</p>
  
  <pre><code>public class CryptoUtils {
      private static final String ALGORITHM = "AES";
      private static final String TRANSFORMATION = "AES/CFB8/NoPadding";
  
      Charset CHARSET = Charset.forName("UTF8");
  
      public static void encrypt(byte[] key, byte[] iv, File inputFile, File outputFile) throws CryptoException {
          doCrypto(Cipher.ENCRYPT_MODE, key, iv, inputFile, outputFile);
      }
  
      public static void decrypt(byte[] key, byte[] iv, File inputFile, File outputFile) throws CryptoException {
          doCrypto(Cipher.DECRYPT_MODE, key, iv, inputFile, outputFile);
      }
  
      private static void doCrypto(int cipherMode, byte[] key, byte[] iv, File inputFile, File outputFile)
              throws CryptoException {
          try {
              Key secretKey = new SecretKeySpec(key, ALGORITHM);
              IvParameterSpec ivSpec = new IvParameterSpec(iv);
  
              Cipher cipher = Cipher.getInstance(TRANSFORMATION);
              cipher.init(cipherMode, secretKey, ivSpec);
  
              FileInputStream inputStream = new FileInputStream(inputFile);
              byte[] inputBytes = new byte[(int) inputFile.length()];
              inputStream.read(inputBytes);
  
              byte[] outputBytes = cipher.doFinal(inputBytes);
  
              FileOutputStream outputStream = new FileOutputStream(outputFile);
              outputStream.write(outputBytes);
  
              inputStream.close();
              outputStream.close();
          } catch (Exception e) {
              throw new CryptoException("Error encrypting/decrypting file", e);
          }
      }
  }
  </code></pre>
  
  <p>here is the calling class, with key and iv initialized...</p>
  
  <pre><code>public class Main {
      public static void main(String[] args) {
          byte[] key = new byte[16];
          byte[] iv = new byte[16];
  
          for(int i = 0; i &lt; key.length; i++) {
              key[i] = 0x0;
              iv[i] = 0x0;
          }
  
          Charset CHARSET = Charset.forName("UTF8");
  
          String keyStr = new String(key);
          String ivStr = new String(iv);
  
          File inputFile = new File("target.txt");
          File encryptedFile = new File("target.enc");
          File decryptedFile = new File("target.dec");
  
          try {
              //CryptoUtils.decrypt(keyStr.getBytes(CHARSET), ivStr.getBytes(CHARSET), encryptedFile, decryptedFile);
              //CryptoUtils.encrypt(keyStr.getBytes(CHARSET), ivStr.getBytes(CHARSET), inputFile, encryptedFile);
              CryptoUtils.encrypt(key, iv, inputFile, encryptedFile);
              //CryptoUtils.decrypt(key, iv, encryptedFile, decryptedFile);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>and here is the C++ code... </p>
  
  <pre><code>byte key[AES::DEFAULT_KEYLENGTH];
  byte iv[AES::DEFAULT_KEYLENGTH];
  
  void encrypt64(char* in, char* out) {
      CFB_Mode&lt;AES&gt;::Encryption e;
      e.SetKeyWithIV(key, sizeof(key), iv);
  
      FileSource f(in, true,
                  new StreamTransformationFilter(e,
                  new HexEncoder(new FileSink(out))));
  }
  
  void decrypt64(char* in, char* out) {
      CFB_Mode&lt;AES&gt;::Decryption d;
      d.SetKeyWithIV(key, sizeof(key), iv);
  
      FileSource f(in, true,
                  new HexDecoder(new StreamTransformationFilter(d, new FileSink(out))));
  }
  
  int main() {
      memset(key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH);
      memset(iv, 0x00, CryptoPP::AES::BLOCKSIZE);
  
      decrypt64("target.enc", "target.dec2");
  
      return 0;
  }
  </code></pre>
  
  <p>as its apparent key and iv in both codes are same. still it doesn't work.. </p>
  
  <p>I've been at this hours now.. but as I'm a noob in cryptography and cryptography libraries.. so could not find any answer or may be found but didnt got into my head... :/</p>
  
  <p>anyway.. please let me know what I'm doing wrong and how can I fix it... thanks in advance :)</p>