Encryption (Rijndael Cipher) With C/C++ in Android NDK -
i want convert c#(managed) decryption method android ndk, c/c++ (no java)
i see there crypto. on java side want keep away jni, , see there's mcrypt , crypt++ cant find compiled lib android.
here example in c# want translate, c/c++
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; };
update best i've found far use openssl aes, have downloaded pre-compiled lib android, i'm struggling work example posted working here c code example
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 encryption , 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); // can aes-cbc-128 aes-cbc-192 aes-cbc-256 aes_key enc_key, dec_key; aes_set_encrypt_key(aes_key, keylength, &enc_key); aes_cbc_encrypt(input, enc_out, inputslength, &enc_key, iv_enc, aes_encrypt); aes_set_decrypt_key(aes_key, keylength, &dec_key); aes_cbc_encrypt(enc_out, dec_out, encslength, &dec_key, iv_dec, aes_decrypt); logi("before:"); for(x=0;x<inputslength;x++) logi("%02x, ", input[x]); logi("encrypted:"); for(x=0;x<encslength;x++) logi("%02x, ", enc_out[x]); logi("decrypted:"); for(x=0;x<encslength;x++) logi("%02x, ", dec_out[x]); };
the encrypted bytes aren't same c# , decrypt doesn't go input, have gone wrong ?
solved:
issue appears array held iv gets altered after encryption need reset before decrypt result
using pre-built openssl android can find here openssl-for-android-prebuilt
and code above remember set iv before each call aes_cbc_encrypt.
Comments
Post a Comment