2015-04-22 102 views
0

我想轉換我的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輸入,我哪裏出錯了?

+0

我在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

回答

1

解決:

問題是它似乎它保持,則需要在解密的結果

使用的預建的OpenSSL之前重置回IV被加密後改變了陣列Android你可以在這裏找到OpenSSL-for-Android-Prebuilt

和上面的代碼只記得在每次調用AES_cbc_encrypt之前設置IV。

+0

以上我需要類似的東西進行加密,我對OpenSSL或NDK也不是很熟悉。你可以請分享一些鏈接,它顯示了在Android NDK的JNI文件夾中使用OpenSSL庫的一些例子嗎?這對我很有幫助。 – Zoombie