Android AES in C

I want to encrypt my files on a PC (Windows 7, 64 bit) and decrypt them on Android.

I use this algorithm to encrypt files.
http://gladman.plushost.co.uk/oldsite/AES/aes-byte-29-08-08.zip

I encrypt my files on the PC, click them on the SD card.
Unfortunately, when I try to decrypt them on Android, the
result is different, the
files are not completely readable ...!

What is wrong with my code?

jbyteArray Java_com_example_hellojni_HelloJni_decrypt(JNIEnv* env, jobject thiz, jstring fileName) {
    ......

    /* read the file into the buffer */
    size_t result = fread (buffer_in, 1, file_size, fin);
    if (result!=file_size) { fputs("Reading error", stderr); exit(3); } /* end if */
    fclose(fin);

    /* decrypt file */
    aes_context ctx[1];
    aes_set_key(key, 16, ctx);
    long i;
    for (i=0; i<num_block; i++) {
        long begin = i*16;
        char *block = copyBlock(buffer_in, file_size, begin, 16), /* copy buffer_in[begin] ~ buffer_in[begin+16-1] to block[] */
             *tmp = (char*)malloc(sizeof(char)*16);
        aes_decrypt(block, tmp, ctx);
        fillBuffer(buffer_out, out_size, tmp, begin, 16); /* copy tmp[] to buffer_out[begin] ~ buffer_out[begin+16-1] */
        free(tmp);
        free(block);
    } /* end for */
    ......
} /* end Java_com_example_hellojni_HelloJni_decrypt() */

I know that the discrepancy occurs in aes.c:

return_type aes_set_key( const unsigned char key[], length_type keylen, aes_context ctx[1] ) {
    ......
    for( cc = keylen, rc = 1; cc < hi; cc += 4 ) {
        uint_8t tt, t0, t1, t2, t3;

        /* difference begins here */
        t0 = ctx->ksch[cc - 4];
        t1 = ctx->ksch[cc - 3];
        t2 = ctx->ksch[cc - 2];
        t3 = ctx->ksch[cc - 1];         
        .......
    } /* end for */
    return 0;
} /* end aes_set_key() */

but why?!
Help is bad!

+5
source share
1 answer

, AES ( ) (, , ) - .

Java ( JNI, ?), API (javax.crypto), JRE. API Android ( , , BouncyCastle).

,

  • (, ECB ( ), CBC, CTR, CFB, OFB) . , C, , . , Android CBC.

  • .

+2

All Articles