title: Encryption (Rijndael Cipher) With C/C++ in Android NDK
body: <p>I want to convert my C#(managed) decryption method into Android NDK, C/C++ (NO JAVA)</p>
  
  <p>I see there was crypto. on the JAVA side but I want to keep away from any JNI, and I also see there's mcrypt and crypt++ but cant find a compiled lib for android.</p>
  
  <p>Here an example in C# which I want to translate, to c/c++</p>
  
  <pre><code>    public byte[] DecryptBytes(byte[] encryptedBytes)           
      {
      RijndaelManaged RijndaelCipher = new RijndaelManaged();
  
  
              RijndaelCipher.Mode = CipherMode.CBC;
  
              ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(Bytes32_KEY, Bytes16_IV);
  
              MemoryStream memoryStream = new MemoryStream(encryptedBytes);
              CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);
              byte[] plainBytes = new byte[encryptedBytes.Length];
  
              int DecryptedCount = cryptoStream.Read(plainBytes, 0, plainBytes.Length);
  
              memoryStream.Close();
              cryptoStream.Close();
  
              return plainBytes;
  
      };
  </code></pre>
  
  <p><strong>UPDATE</strong>
  So the best I've found so far is to use openSSL AES, I have downloaded a pre-compiled lib for Android, I'm just struggling to get it work with the example some already posted as working here is the c code example</p>
  
  <pre><code>void test_enc(){
  
      int keylength = 256;
  
      //  // 256bit KEY
          uint8_t key[32] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
              0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
              0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
              0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  
          //128bit IV
          uint8_t iv[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
              0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  
  
          //input data
          uint8_t input[64] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
              0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
              0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
              0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
              0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
              0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
              0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
              0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  
      size_t inputslength = 10;
      int x;
  
      uint8_t *aes_key = key;
  
      uint8_t *aes_input = input;
  
      uint8_t *iv_enc = iv;
      uint8_t *iv_dec = iv;
  
      // buffers for encryption and decryption
      const size_t encslength = ((inputslength + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
      uint8_t *enc_out = (uint8_t*)malloc(sizeof(uint8_t) *encslength);
      uint8_t *dec_out = (uint8_t*)malloc(sizeof(uint8_t) *inputslength);
      memset(enc_out, 0, encslength);
      memset(dec_out, 0, inputslength);
  
      // so i can do with this aes-cbc-128 aes-cbc-192 aes-cbc-256
      AES_KEY enc_key, dec_key;
      AES_set_encrypt_key(aes_key, keylength, &amp;enc_key);
      AES_cbc_encrypt(input, enc_out, inputslength, &amp;enc_key, iv_enc, AES_ENCRYPT);
  
      AES_set_decrypt_key(aes_key, keylength, &amp;dec_key);  
      AES_cbc_encrypt(enc_out, dec_out, encslength, &amp;dec_key, iv_dec, AES_DECRYPT);
  
      LOGI("Before:");
      for(x=0;x&lt;inputslength;x++)
          LOGI("%02x, ", input[x]);
  
      LOGI("Encrypted:");
      for(x=0;x&lt;encslength;x++)
          LOGI("%02x, ", enc_out[x]);
  
      LOGI("Decrypted:");
      for(x=0;x&lt;encslength;x++)
          LOGI("%02x, ", dec_out[x]);
  };
  </code></pre>
  
  <p>The encrypted bytes aren't the same as the c# and then the decrypt doesn't go back to the input, where have I gone wrong ?</p>