Android jni aes加解密,实现文件的加解密,具体实现可以自行修改,上面的代码为简单介绍,下面的是JNI端实现文件加解密,可以修改为字符串加解密

#include "aes.h"
#include "modes.h"
#include "e_os2.h"
#include "aes_locl.h"
#include "opensslconf.h"
AES_KEY aes;

//aes cbc模式加解密用到的向量
unsigned char iv[AES_BLOCK_SIZE];

for (i = 0; i < AES_BLOCK_SIZE; i++) {
   iv[i] = 0;

}
//设置aes加密的key和加密的长度
AES_set_encrypt_key(key, 128, &aes);
//开始aes加密,注意作为cbc加密向量会改变
AES_cbc_encrypt((unsigned char*) string, encryptString, lenBuff, &aes, iv,AES_ENCRYPT);

JNIEXPORT jstring JNICALL Java_com_encrypt_EncryptActivity_aesEncryptFile(
        JNIEnv *env, jobject, jstring pathorg, jstring pathnow) {
    int len;
    char *buff;
    const char *filepathorg;
    const char *filepathnow;
    filepathorg = env->GetStringUTFChars(pathorg, 0);
    filepathnow = env->GetStringUTFChars(pathnow, 0);
    AES_KEY aes;
    unsigned int i;
    //aes加密所输入的字符串
    unsigned char *inputString;
    //aes加密后字符串指针
    unsigned char *encryptString;

    FILE* file = fopen(filepathorg, "rb+");
    if (file) {
        fseek(file, 0L, SEEK_END);
        len = ftell(file);
        buff = (char *) malloc((len + 1) * sizeof(char));
        memset(buff, 0, len + 1);
        fseek(file, 0L, SEEK_SET);
        fread(buff, 1, len + 1, file);
        fclose(file);
        //计算字符串的长度,如果不是16字节的倍数扩展为16字节的倍数
        if ((len) % AES_BLOCK_SIZE == 0) {
            //len = len + 1;
        } else {
            len = ((len) / AES_BLOCK_SIZE + 1) * AES_BLOCK_SIZE;
        }
        //为输入字符串分配空间
        inputString = (unsigned char*) calloc(len + 1, sizeof(unsigned char));
        memset(inputString, 0, len + 1);
        //为aes加密后字符串分配空间
        encryptString = (unsigned char*) calloc(len + 1, sizeof(unsigned char));
        memset(encryptString, 0, len + 1);
        //将从java段接收到的字串拷贝到加密输入字串中,注意类型不一致
        memcpy((char*) inputString, buff, len + 1);
        //设置加密向量,该值为加密初始值
        for (i = 0; i < AES_BLOCK_SIZE; i++) {
            iv[i] = 0;
        }
        //设置aes加密的key和加密的长度
        AES_set_encrypt_key(key, 128, &aes);
        //开始aes加密,注意作为cbc加密向量会改变
        AES_cbc_encrypt((unsigned char*) inputString, encryptString, len, &aes,
                iv, AES_ENCRYPT);
        FILE* filew = fopen(filepathnow, "wb");
        if (filew) {
            fwrite(encryptString, 1, len, filew);
            fclose(filew);
        } else {
            return env->NewStringUTF("Filed to create file!");
        }
    } else {
        return env->NewStringUTF("Filed to read file!");
    }
    //释放出从java端接收的字符串
    env->ReleaseStringUTFChars(pathorg, filepathorg);
    env->ReleaseStringUTFChars(pathnow, filepathnow);
    return 0;
}

 

1.aes加解密 采用openssl开源库进行处理
2.将核心ase的代码取出aes_core.cpp,aes.h,aes_locl.h,e_os2.h,modes.h,opensslconf.h
3.上述文件是openssl Aes加密的核心代码,如下介绍的是aes cbc模式的加密方式,AES_ENCRYPT该值设为AES_DECRYPT则为解密,iv向量初始化加解密必须一致

该代码只是简写,需要按照正常C或者C++格式书写

资源下载:Android jni aes base64加解密
http://192.210.60.138/dz/forum.php?mod=viewthread&tid=4&fromuid=1
(出处: IStudy)

 

Android jni aes加解密,实现文件的加解密,具体实现可以自行修改,上面的代码为简单介绍,下面的是JNI端实现文件加解密,可以修改为字符串加解密,布布扣,bubuko.com

Android jni aes加解密,实现文件的加解密,具体实现可以自行修改,上面的代码为简单介绍,下面的是JNI端实现文件加解密,可以修改为字符串加解密

上一篇:读取IOS的相应路径


下一篇:Android TextView中文字通过SpannableString来设置超链接、颜色、字体等属性