Android JNI Aes加密
/* * AES 加密工具类 * Created by mazaiting on 2018/7/3. */ #include <jni.h> #include <cstring> #include <android/log.h> //#include "openssl/evp.h" #include <openssl/evp.h> #include <openssl/buffer.h> #define TAG "AES_CPP" #define LOGD(FORMAT, ...) __android_log_print(ANDROID_LOG_DEBUG,TAG,FORMAT,##__VA_ARGS__); // 命名空间 //using namespace std; extern "C" { // 初始化key const unsigned char *_key = (const unsigned char *) "0123456789012345"; // 初始化iv const unsigned char *_iv = (const unsigned char *) "0123456789012345"; /** * Use EVP to Base64 encode the input byte array to readable text */ char *base64(const unsigned char *inputBuffer, int inputLen) { EVP_ENCODE_CTX ctx; int base64Len = (((inputLen + 2) / 3) * 4) + 1; // Base64 text length // int pemLen = base64Len + base64Len/64; // PEM adds a newline every 64 bytes char *base64 = new char[base64Len]; int result; EVP_EncodeInit(&ctx); EVP_EncodeUpdate(&ctx, (unsigned char *) base64, &result, (unsigned char *) inputBuffer, inputLen); EVP_EncodeFinal(&ctx, (unsigned char *) &base64[result], &result); return base64; } /** * Use EVP to Base64 decode the input readable text to original bytes */ char *unbase64(char *input, int length) { EVP_ENCODE_CTX ctx; int orgLen = (((length + 2) / 4) * 3) + 1; char *orgBuf = new char[orgLen]; int result, tmpLen; EVP_DecodeInit(&ctx); EVP_DecodeUpdate(&ctx, (unsigned char *) orgBuf, &result, (unsigned char *) input, length); EVP_DecodeFinal(&ctx, (unsigned char *) &orgBuf[result], &tmpLen); result += tmpLen; return orgBuf; } JNIEXPORT jobjectArray JNICALL Java_com_mazaiting_aesdemo_Encrypt_setValues(JNIEnv *env, jclass type) { // 待加密的数据 const unsigned char *vItem = (const unsigned char *) "mazaiting"; // 源数据长度 int inLen = (int) strlen((const char *) vItem); // 加密长度 int encLen = 0; // 输出长度 int outlen = 0; // 加密数据长度 unsigned char encData[1024] = {0}; LOGD("source: %s\n", vItem); // 创建加密上下文 EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); // 初始化加密上下文 EVP_CipherInit_ex(ctx, EVP_aes_256_ecb(), NULL, _key, _iv, 1); // 加密数据 EVP_CipherUpdate(ctx, encData, &outlen, vItem, inLen); // 拼接长度 encLen = outlen; // 结束加密 EVP_CipherFinal(ctx, encData + outlen, &outlen); // 拼接 encLen += outlen; // 释放 EVP_CIPHER_CTX_free(ctx); // base64编码 char *baseEnc = base64(encData, encLen); LOGD("encrypted : %s\n", baseEnc); // base64解码 char *encData1 = unbase64(baseEnc, (int) strlen(baseEnc)); // 解密长度 int decLen = 0; // 解码数据长度 outlen = 0; // 解码后的数据 unsigned char decData[1024]; // 创建解密上下文 EVP_CIPHER_CTX *ctx2 = EVP_CIPHER_CTX_new(); // 初始化解密 EVP_CipherInit_ex(ctx2, EVP_aes_256_ecb(), NULL, _key, _iv, 0); // 执行解密 EVP_CipherUpdate(ctx2, decData, &outlen, (const unsigned char *) encData1, encLen); // 设置长度 decLen = outlen; // 结束解密 EVP_CipherFinal(ctx2, decData + outlen, &outlen); // 拼接长度 decLen += outlen; // 释放 EVP_CIPHER_CTX_free(ctx2); // 设置字符串结尾标识 decData[decLen] = '\0'; LOGD("decrypt: %s\n", decData); } }

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
2018最全的iOS面试题及答案
最全的iOS面试题及答案 iOS面试小贴士 ———————————————回答好下面的足够了------------------------------------- 多线程、特别是NSOperation 和 GCD 的内部原理。 运行时机制的原理和运用场景。 SDWebImage的原理。实现机制。如何解决TableView卡的问题。 block和代理的,通知的区别。block的用法需要注意些什么。 strong,weak,retain,assign,copy nomatic 等的区别。 设计模式,mvc,单利,工厂,代理等的应用场景。 单利的写法。在单利中创建数组应该注意些什么。 NSString 的时候用copy和strong的区别。 响应值链。 NSTimer 在子线程中应该手动创建NSRunLoop ,否则不能循环执行。 UIScrollView和NSTimer组合做循环广告图轮播的时候有一个属性可以控制当上下滚动tableview的时候广告轮播图依然正常滚动。 Xcode最新的自动布局。。。这个很多公司都用。尽量自学下。 git ,和svn的用法。。。git的几个命令简单的记...
- 下一篇
Android程序员搞Web之CSS(三)
1、行内样式 <body> <table align="center" > <caption><h4 style="color: red; font-size: 20px">数字整理表格</h4></caption> <tr> <th> 数字整理 </th> </tr> <tr> <td style="color: #669900">小数字</td> <td>123</td> </tr> <tr> <td>中数字</td> <td>456</td> </tr> <tr> <td>大数字</td> <td>789</td> </tr> </table> </body> 在标签内输入 style 并在 style 引号内输入样式 css...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- Linux系统CentOS6、CentOS7手动修改IP地址
- Hadoop3单机部署,实现最简伪集群
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- Windows10,CentOS7,CentOS8安装Nodejs环境
- 设置Eclipse缩进为4个空格,增强代码规范
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- CentOS7设置SWAP分区,小内存服务器的救世主
- Docker安装Oracle12C,快速搭建Oracle学习环境