title: Encrypting a string in Java and decrypting it in C++. crypto++
body: <p>I need to encrypt a string in Java and decrypt it in C++. I've seen C++ has a Crypto++ library and Java has JCE.
  The algorithm is AES/ECB/NOPADDING 
  String is "12345690123456"  for nopadding issue in java.
  Key is byte []key=new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};  16 byte.
  These are same condition in java and c++.</p>
  
  <p>Below is java code. </p>
  
  <pre><code>import javax.crypto.*;
  import javax.crypto.spec.*;
  
  public class etest {  
      public static void main(String[] args) {
          // TODO Auto-generated method stub
          encryp ec=new encryp();
          String name="1234567890123456";
          System.out.println(name);
          try {
              System.out.println(ec.encrypt(name));
  //          System.out.println(ec.decrypt(ec.encrypt(name)));
          } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
  }
  
  class encryp {
  //  public static String key = "abcdefgh12345678";
      static byte []key=new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
      public static String encrypt(String message) throws Exception {
          SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
          Cipher cipher = Cipher.getInstance("AES/ECB/NOPADDING");
          cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
          byte[] encrypted = cipher.doFinal(message.getBytes());
          return byteArrayToHex(encrypted);
  //      return encrypted;
      }
      public static String decrypt(String encrypted) throws Exception {
  
          // use key coss2
          SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
  
          Cipher cipher = Cipher.getInstance("AES");
          cipher.init(Cipher.DECRYPT_MODE, skeySpec);
          byte[] original = cipher.doFinal(hexToByteArray(encrypted));
          String originalString = new String(original);
          return originalString;
      }
      public static byte[] hexToByteArray(String hex) {
          if (hex == null || hex.length() == 0) {
              return null;
          }
  
          byte[] ba = new byte[hex.length() / 2];
          for (int i = 0; i &lt; ba.length; i++) {
              ba[i] = (byte) Integer
                      .parseInt(hex.substring(2 * i, 2 * i + 2), 16);
          }
          return ba;
      }
  
      /**
       * 
       * @param ba
       *            byte[]
       * @return
       */
      public static String byteArrayToHex(byte[] ba) {
          if (ba == null || ba.length == 0) {
              return null;
          }
  
          StringBuffer sb = new StringBuffer(ba.length * 2);
          String hexNumber;
          for (int x = 0; x &lt; ba.length; x++) {
              hexNumber = "0" + Integer.toHexString(0xff &amp; ba[x]);
  
              sb.append(hexNumber.substring(hexNumber.length() - 2));
          }
          return sb.toString();
      }
  }
  </code></pre>
  
  <p>Next is c++ code.</p>
  
  <pre><code>#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1   
  
  // Crypto++ Includes
  #include "cryptopp562\cryptlib.h"
  #include "cryptopp562\osrng.h" 
  #include "cryptopp562\hex.h"
  #include "cryptopp562\Base64.h"
  #include "cryptopp562\aes.h"        
  #include "cryptopp562\seed.h"
  #include "cryptopp562\des.h"
  #include "cryptopp562\modes.h"      
  #include "cryptopp562\filters.h"    
  #include &lt;iostream&gt;
  #ifdef _DEBUG
  #  pragma comment ( lib, "cryptlibd" )
  #else
  #  pragma comment ( lib, "cryptlib" )
  #endif
  
  template &lt;class TyMode&gt;
  std::string Encrypt(TyMode &amp;Encryptor, const std::string &amp;PlainText)
  {
      std::string EncodedText;
  
      try {
          CryptoPP::StringSource( PlainText, true,
              new CryptoPP::StreamTransformationFilter(Encryptor, 
                  new CryptoPP::Base64Encoder(
                      new CryptoPP::StringSink( EncodedText ), false
                      ), CryptoPP::BlockPaddingSchemeDef::NO_PADDING
              )
          ); 
      }
      catch (...) {}
  
      return EncodedText;
  }
  
  template &lt;class TyMode&gt;
  std::string Decrypt(TyMode &amp;Decryptor, const std::string &amp;EncodedText)
  {
      std::string RecoveredText;
  
      try {
          CryptoPP::StringSource( EncodedText, true,
              new CryptoPP::Base64Decoder(
                  new CryptoPP::StreamTransformationFilter( Decryptor,
                      new CryptoPP::StringSink( RecoveredText ), 
                      CryptoPP::BlockPaddingSchemeDef::NO_PADDING
                  ) 
              ) 
          ); 
      }
      catch (...) {}
  
      return RecoveredText;
  }
  
  template &lt;class Ty&gt;
  std::string CBC_Encrypt(byte *KEY, byte *IV, const std::string &amp;PlainText)
  {
      CryptoPP::CBC_Mode&lt;typename Ty&gt;::Encryption Encryptor(KEY, typename Ty::DEFAULT_KEYLENGTH, IV);
      return Encrypt(Encryptor, PlainText);
  }
  
  
  template &lt;class Ty&gt;
  std::string CBC_Decrypt(byte *KEY, byte *IV, const std::string &amp;PlainText)
  {
      CryptoPP::CBC_Mode&lt;typename Ty&gt;::Decryption Decryptor(KEY, typename Ty::DEFAULT_KEYLENGTH, IV);
      return Decrypt(Decryptor, PlainText);
  }
  
  template &lt;class Ty&gt;
  std::string ECB_Encrypt(byte *KEY, const std::string &amp;PlainText)
  {
      CryptoPP::ECB_Mode&lt;typename Ty&gt;::Encryption Encryptor(KEY, typename Ty::DEFAULT_KEYLENGTH);
      return Encrypt(Encryptor, PlainText);
  }
  
  
  template &lt;class Ty&gt;
  std::string ECB_Decrypt(byte *KEY, const std::string &amp;PlainText)
  {
      CryptoPP::ECB_Mode&lt;typename Ty&gt;::Decryption Decryptor(KEY, typename Ty::DEFAULT_KEYLENGTH);
      return Decrypt(Decryptor, PlainText);
  }
  
  
  template &lt;class CryptoType&gt;
  void Test()
  {
      using namespace std;
  
      const std::string sText = "1234567890123456";
      std::string sEnc, sDec;
  
      byte KEY[ 16 ] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  //  byte KEY[ CryptoType::DEFAULT_KEYLENGTH ] = {0, }; 
      byte IV[ CryptoType::BLOCKSIZE ] = {0x01, }; 
  
  
      sEnc = CBC_Encrypt&lt;CryptoType&gt;(KEY, IV, sText);
      sDec = CBC_Decrypt&lt;CryptoType&gt;(KEY, IV, sEnc);
  
  //  cout &lt;&lt; CryptoType::StaticAlgorithmName() &lt;&lt; " : " &lt;&lt; "CBC_MODE" &lt;&lt; endl;
  //  cout &lt;&lt; sText &lt;&lt; "\n -&gt; " &lt;&lt; sEnc &lt;&lt; "\n -&gt; " &lt;&lt; sDec &lt;&lt; endl;
  
  
      // ECB 
      sEnc = ECB_Encrypt&lt;CryptoType&gt;(KEY, sText);
  //  sEnc="41f813eca9ddd189f7ff3280ada72c8a";
      sDec = ECB_Decrypt&lt;CryptoType&gt;(KEY, sEnc);
  
      cout &lt;&lt; CryptoType::StaticAlgorithmName() &lt;&lt; " : " &lt;&lt; "ECB_MODE" &lt;&lt; endl;
      cout &lt;&lt; sText ;
      cout&lt;&lt;hex&lt;&lt; "\n -&gt; " &lt;&lt; sEnc &lt;&lt; "\n -&gt; " &lt;&lt; sDec &lt;&lt; endl;
      cout &lt;&lt; endl;
  }
  
  
  int main()
  {
      using namespace std;
  
      // SEED
  //  Test&lt;CryptoPP::SEED&gt;();
  
      // AES 
      Test&lt;CryptoPP::AES&gt;();
  
      // DES 
  //  Test&lt;CryptoPP::DES&gt;();
      system("pause");
      return 0;
  }
  </code></pre>
  
  <p>The result of c++ is maybe encoded. But outcome of decoded cipher text is also different with result of java encryption.  Of course, I thought hex string in java.  However hex, binary, etc encoding or decoding all are not same each other.<br>
  I want that this work well.
  What is problem?  help plz</p>
  
  <p>+
  I searched <a href="http://www.example-code.com/vcpp/aes_javaJceAesCbc.asp" rel="nofollow">http://www.example-code.com/vcpp/aes_javaJceAesCbc.asp</a>
  A library.
  This work well?
  I set environment and run but lnk2019 error...
  I thought i set right Lib, h include setting...
  How can i do this?? </p>