我想转换我的C#(管理)解密方法为Android的NDK,C/C++(NO JAVA)加密(Rijndael算法密码)用C/C++中的Android NDK
我看到有加密。在JAVA方面,但我想远离任何JNI,我也看到有mcrypt和crypt ++,但无法找到android的编译库。
在这里,在C#中的例子,我想翻译,以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 所以我发现到目前为止是使用OpenSSL AES是最好的,我已经下载了一个预-compiled LIB为Android,我只是努力把它与一些已经张贴在这里工作的例子的工作是C代码示例
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]);
};
加密的字节是不一样的C#,然后解密不会回到th e输入,我哪里出错了?
我在C下找到了AES的几个例子,但是无法看到IV会去哪里,于是我发现有人发布了一个编译后的openSSL AES for android,再加上在这里使用AES [post]/questions/18152913/aes-aes-cbc-128-aes-cbc -192-aes-cbc-256-encryption-decryption-with-openssl-c)但是这个例子似乎不起作用!我将发布我必须检查我的代码 – RJButler