Android的AES用CAndroid、AES

2023-09-08 00:35:07 作者:绿色—小清新

我想我的加密文件的PC(Windows 7中,64位)和解密'时间在Android上。结果结果我用这个算法中对文件进行加密。结果 http://gladman.plushost.co.uk /oldsite/AES/aes-byte-29-08-08.zip 结果结果我在加密电脑我的文件,他们推到SD卡。结果不幸的是,当我尝试解密这些在Android上,结果结果是不同的,结果,文件是完全不可读...!结果结果这有什么错我的code?结果

I wanna encrypt my files on PC(Windows 7, 64bit) and decrypt 'em on Android. I use this algo to encrypt files. http://gladman.plushost.co.uk/oldsite/AES/aes-byte-29-08-08.zip I encrypt my files on PC, push them to SDcard. Unfortunately when I try to decrypt them on Android, the result is different, files are completely unreadable...! What's 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() */

我认识的差异发生在aes.c:

I know the discrepancy happens 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 needed badly!

推荐答案

在一般情况下,你不应该试图实现AES(或任何其他密码算法)自己(相互学习它是如何工作) - 用于生产目的使用一种称为库

In general, you should not try to implement AES (or any other cryptographic algorithm) yourself (other to learn how it works) - use known libraries for production purposes.

对于Java(你在这里使用JNI,不是吗?),使用加密API(javax.crypto中),它自带的JRE。同样的API也可用于Android(或者其中一个自带的引擎,或BouncyCastle的变种)。

For Java (you are using JNI here, aren't you?), use the Cryptography API (javax.crypto) which comes with the JRE. The same API is also available for Android (either the one which comes with the engine, or the BouncyCastle variant).

然后,确保您使用的是

同样的操作模式(例如欧洲央行(不推荐),CBC,CTR,CFB,OFB)的分组密码。我不知道哪个模式是由您的C实现,也许欧洲央行完成。我想为Android默认为CBC。

the same mode of operation (e.g. ECB (not recommended), CBC, CTR, CFB, OFB) for the block cipher. I have no idea which mode is done by your C implementation, maybe ECB. I suppose the default for Android is CBC.

用于加密和解密相同的密钥。

the same key for encrypting and decryption.