Issue
I want to convert my C#(managed) decryption method into Android NDK, C/C++ (NO JAVA)
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.
Here an example in C# which I want to translate, to 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 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
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, &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 the same as the c# and then the decrypt doesn't go back to the input, where have I gone wrong ?
Solution
SOLVED:
Issue is it appears the array which held the IV gets altered after the encryption so you need to reset back before you decrypt for the result
Using the pre-built OpenSSL for Android you can find here OpenSSL-for-Android-Prebuilt
and the code above just remember to set the IV before each call to AES_cbc_encrypt.
Answered By - RJButler
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.