JNI 在实际项目中的使用方法
生活随笔
收集整理的這篇文章主要介紹了
JNI 在实际项目中的使用方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文主要講述JNI在實際項目中的使用方法,主要目的是記錄,不做太多說明,代碼如下:
1、CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 3.5)project("device-authentication")INCLUDE_DIRECTORIES(/usr/include ../../include ../../net ../../net/mbedtls/include ../../smalgo/sms4/include)SET(my_src_crypto../../net/mbedtls/library/aes.c../../net/mbedtls/library/aesni.c../../net/mbedtls/library/base64.c../../net/mbedtls/library/rsa.c../../net/mbedtls/library/rsa_internal.c../../net/mbedtls/library/entropy.c../../net/mbedtls/library/entropy_poll.c../../net/mbedtls/library/bignum.c../../net/mbedtls/library/sha1.c../../net/mbedtls/library/sha256.c../../net/mbedtls/library/sha512.c../../net/mbedtls/library/md.c../../net/mbedtls/library/md5.c../../net/mbedtls/library/md_wrap.c../../net/mbedtls/library/ripemd160.c../../net/mbedtls/library/platform_util.c../../net/mbedtls/library/oid.c../../net/mbedtls/library/timing.c../../net/mbedtls/library/net_sockets.c../../smalgo/sms4/cbc128.c../../smalgo/sms4/sms4_cbc.c../../smalgo/sms4/sms4_common.c../../smalgo/sms4/sms4_enc.c../../smalgo/sms4/sms4_setkey.c)SET(my_src_crypto_dbg../../net/mbedtls/library/ctr_drbg.c)SET(SRC_LIST_AUTH_DEV${my_src_crypto}${my_src_crypto_dbg})SET(my_src_cloud./authserverjni.c)ADD_DEFINITIONS(-fPIC) ADD_LIBRARY(cloud SHARED ${SRC_LIST_AUTH_DEV} ${my_src_cloud})2、頭文件(com_openailab_oascloud_security_jni_eaidk_EaidkAuthJNI.h)
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_openailab_oascloud_security_jni_eaidk_EaidkAuthJNI */#ifndef _Included_com_openailab_oascloud_security_jni_eaidk_EaidkAuthJNI #define _Included_com_openailab_oascloud_security_jni_eaidk_EaidkAuthJNI #ifdef __cplusplus extern "C" { #endif /* * Class: com_openailab_oascloud_security_jni_eaidk_EaidkAuthJNI * Method: decrypUidOnServer * Signature: (Ljava/lang/String;[C)Ljava/lang/String; */ JNIEXPORT jbyteArray JNICALL Java_com_openailab_oascloud_security_jni_cloud_CloudAuthJNI_decrypUidOnServer(JNIEnv *, jobject, jstring, jbyteArray);/* * Class: com_openailab_oascloud_security_jni_eaidk_EaidkAuthJNI * Method: parseUidOnServer * Signature: (Ljava/lang/String;)Lcom/openailab/oascloud/security/model/UidMeanBO; */ JNIEXPORT jobject JNICALL Java_com_openailab_oascloud_security_jni_cloud_CloudAuthJNI_parseUidOnServer(JNIEnv *, jobject, jbyteArray);/* * Class: com_openailab_oascloud_security_jni_eaidk_EaidkAuthJNI * Method: getSnByUidOnServer * Signature: (Ljava/lang/String;[B[B)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_openailab_oascloud_security_jni_cloud_CloudAuthJNI_getSnByUidOnServer(JNIEnv *, jobject, jbyteArray, jbyteArray, jbyteArray);/* * Class: com_openailab_oascloud_security_jni_eaidk_EaidkAuthJNI * Method: aesCbcEncrypOnServer * Signature: (Ljava/lang/String;[B[B)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_openailab_oascloud_security_jni_cloud_CloudAuthJNI_aesCbcEncrypOnServer(JNIEnv *, jobject, jstring, jbyteArray, jbyteArray);/* * Class: com_openailab_oascloud_security_jni_eaidk_EaidkAuthJNI * Method: aesCbcDecrypOnServer * Signature: (Ljava/lang/String;[B[B)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_openailab_oascloud_security_jni_cloud_CloudAuthJNI_aesCbcDecrypOnServer(JNIEnv *, jobject, jstring, jbyteArray, jbyteArray);/* * Class: com_openailab_oascloud_security_jni_eaidk_EaidkAuthJNI * Method: genMD5 * Signature: (Ljava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_openailab_oascloud_security_jni_cloud_CloudAuthJNI_genMD5(JNIEnv *, jobject, jstring);/* * Class: com_openailab_oascloud_security_jni_eaidk_EaidkAuthJNI * Method: genRsaKey * Signature: ()Lcom/openailab/oascloud/security/model/RsaBO; */ JNIEXPORT jobject JNICALL Java_com_openailab_oascloud_security_jni_cloud_CloudAuthJNI_genRsaKey(JNIEnv *, jobject);/* * Class: com_openailab_oascloud_security_jni_cloud_CloudAuthJNI * Method: sm4Encrypt * Signature: ([B[B[B)[B */ JNIEXPORT jbyteArray JNICALL Java_com_openailab_oascloud_security_jni_cloud_CloudAuthJNI_sm4Encrypt(JNIEnv *, jobject, jbyteArray, jbyteArray, jbyteArray);/* * Class: com_openailab_oascloud_security_jni_cloud_CloudAuthJNI * Method: sm4Decrypt * Signature: ([B[B[B)[B */ JNIEXPORT jbyteArray JNICALL Java_com_openailab_oascloud_security_jni_cloud_CloudAuthJNI_sm4Decrypt(JNIEnv *, jobject, jbyteArray, jbyteArray, jbyteArray);#ifdef __cplusplus } #endif #endif3、authserverjni.c
#include "com_openailab_oascloud_security_jni_eaidk_EaidkAuthJNI.h" #include <stdio.h> #include <string.h> #include <stdint.h> #include <stdlib.h> #include <sys/time.h> #include "mbedtls/entropy.h" #include "mbedtls/ctr_drbg.h" #include "mbedtls/rsa.h" #include "mbedtls/aes.h" #include "mbedtls/base64.h" #include "mbedtls/md5.h" #include "sms4.h"#define USERID_LEN 16 #define CONTRACTID_LEN 16 #define UID_LEN 64 #define SN_LEN 128 #define FINAL_UID_LEN 156 #define SWID_LEN 12 #define RSA_KEY_SIZE 1024 #define EXPONENT 65537 #define BUFFER_SIZE 1024 #define RSA_KEY_LEN 256 #define AES_KEY_DEC_LEN 8 #define LICENSE_DEC_LEN 64 #define AES_KEY_SIZE 48 #define AES_IV_LEN 16typedef struct __rsa {uint8_t buf[BUFFER_SIZE * 8];uint8_t* rsa_n;uint8_t* rsa_e;uint8_t* rsa_d;uint8_t* rsa_p;uint8_t* rsa_q;uint8_t* rsa_dp;uint8_t* rsa_dq;uint8_t* rsa_qp;uint32_t n_len;uint32_t e_len;uint32_t d_len;uint32_t p_len;uint32_t q_len;uint32_t dp_len;uint32_t dq_len;uint32_t qp_len; } T_rsa;static void init_rsa_keys(T_rsa* rsa) {memset(rsa->buf, 0, BUFFER_SIZE * 8);rsa->rsa_n = &rsa->buf[BUFFER_SIZE * 0];rsa->rsa_e = &rsa->buf[BUFFER_SIZE * 1];rsa->rsa_d = &rsa->buf[BUFFER_SIZE * 2];rsa->rsa_p = &rsa->buf[BUFFER_SIZE * 3];rsa->rsa_q = &rsa->buf[BUFFER_SIZE * 4];rsa->rsa_dp = &rsa->buf[BUFFER_SIZE * 5];rsa->rsa_dq = &rsa->buf[BUFFER_SIZE * 6];rsa->rsa_qp = &rsa->buf[BUFFER_SIZE * 7];rsa->n_len = BUFFER_SIZE;rsa->e_len = BUFFER_SIZE;rsa->d_len = BUFFER_SIZE;rsa->p_len = BUFFER_SIZE;rsa->q_len = BUFFER_SIZE;rsa->dp_len = BUFFER_SIZE;rsa->dq_len = BUFFER_SIZE;rsa->qp_len = BUFFER_SIZE; }void rsa_encrypt(const T_rsa *r,const unsigned char* plaintext,unsigned int plaintext_size,unsigned char *ciphertext,size_t *ciphertext_size) {mbedtls_rsa_context rsa;mbedtls_entropy_context entropy;mbedtls_ctr_drbg_context ctr_drbg;mbedtls_entropy_init(&entropy);mbedtls_ctr_drbg_init(&ctr_drbg);mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0);mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, 0);mbedtls_mpi_read_binary(&rsa.N, r->rsa_n, BUFFER_SIZE);mbedtls_mpi_read_binary(&rsa.E, r->rsa_e, BUFFER_SIZE);*ciphertext_size = rsa.len = (mbedtls_mpi_bitlen(&rsa.N) + 7) >> 3;mbedtls_rsa_pkcs1_encrypt(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg, MBEDTLS_RSA_PUBLIC, plaintext_size, plaintext, ciphertext);mbedtls_rsa_free(&rsa);mbedtls_ctr_drbg_free(&ctr_drbg);mbedtls_entropy_free(&entropy); }void rsa_decrypt(const T_rsa *r,const unsigned char* ciphertext,//unsigned int ciphertext_size,unsigned char *plaintext,size_t *plaintext_size) {mbedtls_rsa_context rsa;mbedtls_entropy_context entropy;mbedtls_ctr_drbg_context ctr_drbg;mbedtls_entropy_init(&entropy);mbedtls_ctr_drbg_init(&ctr_drbg);mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0);mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, 0);mbedtls_mpi_read_binary(&rsa.N, r->rsa_n, BUFFER_SIZE);mbedtls_mpi_read_binary(&rsa.E, r->rsa_e, BUFFER_SIZE);mbedtls_mpi_read_binary(&rsa.D, r->rsa_d, BUFFER_SIZE);mbedtls_mpi_read_binary(&rsa.P, r->rsa_p, BUFFER_SIZE);mbedtls_mpi_read_binary(&rsa.Q, r->rsa_q, BUFFER_SIZE);mbedtls_mpi_read_binary(&rsa.DP, r->rsa_dp, BUFFER_SIZE);mbedtls_mpi_read_binary(&rsa.DQ, r->rsa_dq, BUFFER_SIZE);mbedtls_mpi_read_binary(&rsa.QP, r->rsa_qp, BUFFER_SIZE);rsa.len = (mbedtls_mpi_bitlen(&rsa.N) + 7) >> 3;mbedtls_rsa_pkcs1_decrypt(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg, MBEDTLS_RSA_PRIVATE, plaintext_size, ciphertext, plaintext, *plaintext_size);mbedtls_rsa_free(&rsa);mbedtls_ctr_drbg_free(&ctr_drbg);mbedtls_entropy_free(&entropy); }static uint8_t hex_lookup[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8','9', 'a', 'b', 'c', 'd', 'e', 'f' }; static void array2hex(uint8_t* in, int inlen, uint8_t* out) {int i;for (i = 0; i < inlen; ++i){out[2 * i] = hex_lookup[in[i] >> 4];out[2 * i + 1] = hex_lookup[in[i] & 0x0f];} }static void hex2array(uint8_t* in, int inlen, uint8_t* out) {int i;unsigned char r;for (i = 0; i < inlen; i += 2){r = in[i] - '0';if (r > 9) r += '0' + 10 - 'a';//printf("%c(%x): %x\n", in[i], in[i], r);out[i / 2] = r << 4;r = in[i + 1] - '0';if (r > 9) r += '0' + 10 - 'a';out[i / 2] += r;//printf("%c(%x): %x <%x>\n", in[i+1], in[i+1], r, out[i/2]);} }//len should be 16*N bytes int aes_cbc_encryp(uint8_t *aes_key, uint8_t *iv, uint8_t *plaintext, uint8_t *ciphertext, uint32_t len) {int i;int blk = (len + 15) >> 4;mbedtls_aes_context aes_ctx;mbedtls_aes_init(&aes_ctx);//setkey_decmbedtls_aes_setkey_enc(&aes_ctx, aes_key, 256);for (i = 0; i < blk; ++i){mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_ENCRYPT, 16, iv, plaintext + (i * 16), ciphertext + (i * 16));}mbedtls_aes_free(&aes_ctx);return 0; //OK } //len should be 16*N bytes int aes_cbc_decryp(uint8_t *aes_key, uint8_t *iv, uint8_t *ciphertext, uint8_t *plaintext, uint32_t len) {int i;int blk = (len + 15) >> 4;mbedtls_aes_context aes_ctx;mbedtls_aes_init(&aes_ctx);//setkey_decmbedtls_aes_setkey_dec(&aes_ctx, aes_key, 256);for (i = 0; i < blk; ++i){mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_DECRYPT, 16, iv, ciphertext + (i * 16), plaintext + (i * 16));}mbedtls_aes_free(&aes_ctx);return 0; //OK }static int SetServerPrivateKey(T_rsa *r, uint8_t *prv_key) {memcpy(r->buf, prv_key, 8192);//memset(&r->buf[0], 0, 1024-128);//memset(&r->buf[1024], 0, 1024-4);//memset(&r->buf[2048], 0, 1024-128);//memset(&r->buf[3072], 0, 1024-64);//memset(&r->buf[4096], 0, 1024-64);//memset(&r->buf[5120], 0, 1024-64);//memset(&r->buf[6144], 0, 1024-64);//memset(&r->buf[7168], 0, 1024-64);return 0; //OK }static int SetServerPublicKey(T_rsa *r, uint8_t *pub_key) {memcpy(r->buf, pub_key, 2048);//memset(&r->buf[0], 0, 1024-128);//memset(&r->buf[1024], 0, 1024-4);//memset(&r->buf[2048], 0, 1024-128);//memset(&r->buf[3072], 0, 1024-64);//memset(&r->buf[4096], 0, 1024-64);//memset(&r->buf[5120], 0, 1024-64);//memset(&r->buf[6144], 0, 1024-64);//memset(&r->buf[7168], 0, 1024-64);return 0; //OK } #if 0 //len should be 16*N bytes int aes_ecb_encryp(uint8_t *aes_key, uint8_t *plaintext, uint8_t *ciphertext, uint32_t len) {int i;int blk = (len + 15) >> 4;mbedtls_aes_context aes_ctx;mbedtls_aes_init(&aes_ctx);//setkey_decmbedtls_aes_setkey_enc(&aes_ctx, aes_key, 256);for (i = 0; i < blk; ++i){mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, plaintext + (i * 16), ciphertext + (i * 16));}mbedtls_aes_free(&aes_ctx);return 0; //OK } //len should be 16*N bytes int aes_ecb_decryp(uint8_t *aes_key, uint8_t *ciphertext, uint8_t *plaintext, uint32_t len) {int i;int blk = (len + 15) >> 4;mbedtls_aes_context aes_ctx;mbedtls_aes_init(&aes_ctx);//setkey_decmbedtls_aes_setkey_dec(&aes_ctx, aes_key, 256);for (i = 0; i < blk; ++i){mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_DECRYPT, ciphertext + (i * 16), plaintext + (i * 16));}mbedtls_aes_free(&aes_ctx);return 0; //OK } #endif int GetMD5(uint8_t *text, int len, uint8_t md5[16]) {mbedtls_md5_context md5_ctx;mbedtls_md5_init(&md5_ctx);mbedtls_md5_starts(&md5_ctx);mbedtls_md5_update(&md5_ctx, text, len);mbedtls_md5_finish(&md5_ctx, md5);mbedtls_md5_free(&md5_ctx);return 0; //OK } static int reoder(uint8_t *src, uint8_t *dst, int len) {int i;int m = 0;int n = len - 1;for (i = 0; i < len; i++){if (i % 3 == 0){dst[m++] = src[i];}else{dst[n--] = src[i];}}return 0; }static int recover(uint8_t *src, uint8_t *dst, int len) {int i;int m = 0;int n = len - 1;for (i = 0; i < len; i++){if (i % 3 == 0){dst[i] = src[m++];}else{dst[i] = src[n--];}}return 0; }//@server //sn's length should >= AES_KEY_SIZE + AES_IV_LEN int genSN(uint8_t *sn, size_t* sn_len, uint8_t* key, uint8_t* iv, uint8_t uid_local[UID_LEN]) {int i;for (i = 0; i < AES_KEY_SIZE; ++i) {sn[i] = key[i] ^ uid_local[i];}for (i = 0; i < AES_IV_LEN; ++i) {sn[i + AES_KEY_SIZE] = iv[i] ^ uid_local[i];}*sn_len = AES_KEY_SIZE + AES_IV_LEN;return 0; //OK }void generate_rsa(T_rsa* r) {mbedtls_rsa_context rsa;mbedtls_entropy_context entropy;mbedtls_ctr_drbg_context ctr_drbg;mbedtls_entropy_init(&entropy);mbedtls_ctr_drbg_init(&ctr_drbg);mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0);mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, 0);mbedtls_rsa_gen_key(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg, RSA_KEY_SIZE, EXPONENT);mbedtls_mpi_write_binary(&rsa.N, r->rsa_n, BUFFER_SIZE);mbedtls_mpi_write_binary(&rsa.E, r->rsa_e, BUFFER_SIZE);mbedtls_mpi_write_binary(&rsa.D, r->rsa_d, BUFFER_SIZE);mbedtls_mpi_write_binary(&rsa.P, r->rsa_p, BUFFER_SIZE);mbedtls_mpi_write_binary(&rsa.Q, r->rsa_q, BUFFER_SIZE);mbedtls_mpi_write_binary(&rsa.DP, r->rsa_dp, BUFFER_SIZE);mbedtls_mpi_write_binary(&rsa.DQ, r->rsa_dq, BUFFER_SIZE);mbedtls_mpi_write_binary(&rsa.QP, r->rsa_qp, BUFFER_SIZE);mbedtls_rsa_free(&rsa);mbedtls_ctr_drbg_free(&ctr_drbg);mbedtls_entropy_free(&entropy); }JNIEXPORT jbyteArray JNICALL Java_com_openailab_oascloud_security_jni_cloud_CloudAuthJNI_decrypUidOnServer (JNIEnv *env, jobject obj, jstring uidEnc, jbyteArray privateKey) {uint8_t *uid_enc = (*env)->GetStringUTFChars(env, uidEnc, NULL);uint8_t *private_key = (*env)->GetByteArrayElements(env, privateKey, NULL);jbyteArray ret = NULL;T_rsa r;uint8_t plaintext[UID_LEN];size_t plaintext_len = 256;uint8_t UID_Recover[SN_LEN];uint8_t uid_enc_c[FINAL_UID_LEN];init_rsa_keys(&r);SetServerPrivateKey(&r, private_key);hex2array(uid_enc, FINAL_UID_LEN * 2, uid_enc_c);recover(&uid_enc_c[FINAL_UID_LEN - SN_LEN], UID_Recover, SN_LEN);rsa_decrypt(&r, UID_Recover, plaintext, &plaintext_len);/*printf("plaintext=\n");for (size_t i = 0; i < UID_LEN; i++){if (i > 0 && i % 16 == 0) {printf("\n");}printf("%02x,", plaintext[i]);}printf("\n");*/ret = (*env)->NewByteArray(env, UID_LEN);(*env)->SetByteArrayRegion(env, ret, 0, UID_LEN, (jbyte*)plaintext);(*env)->ReleaseByteArrayElements(env, privateKey, private_key, 0);(*env)->ReleaseStringUTFChars(env, uidEnc, uid_enc);return ret; }JNIEXPORT jobject JNICALL Java_com_openailab_oascloud_security_jni_cloud_CloudAuthJNI_parseUidOnServer (JNIEnv *env, jobject obj, jbyteArray uid) {jclass jclassResultInfo = (*env)->FindClass((env), "com/openailab/oascloud/security/model/UidMeanBO");jmethodID jidResultInfo = (*env)->GetMethodID(env, jclassResultInfo, "<init>", "([B[B[B[B[B[B[B[B[B)V");uint8_t *uid_c = (*env)->GetByteArrayElements(env, uid, NULL);/*printf("uid_c=\n");for (size_t i = 0; i < UID_LEN; i++){if (i > 0 && i % 16 == 0) {printf("\n");}printf("%02x,", uid_c[i]);}printf("\n");*/uint8_t md5[16];uint8_t contractid[CONTRACTID_LEN];uint8_t mac[6];uint8_t deviceid[10];uint8_t softwareversion[4];uint8_t productfeature[8];uint8_t softwareMD5[16];uint8_t userpswd[USERID_LEN];uint8_t userpswdMD5[16];uint8_t time[4];//1. uid from chipmemcpy(contractid, &uid_c[0], CONTRACTID_LEN);jbyteArray contractid_j = (*env)->NewByteArray(env, CONTRACTID_LEN);(*env)->SetByteArrayRegion(env, contractid_j, 0, CONTRACTID_LEN, (jbyte*)contractid);//2. MAC, 6 bytesmemcpy(mac, &uid_c[16], 6);jbyteArray mac_j = (*env)->NewByteArray(env, 6);(*env)->SetByteArrayRegion(env, mac_j, 0, 6, (jbyte*)mac);//3. user id from efpg UA(start 1447, num 601), 4 bytesmemcpy(deviceid, &uid_c[22], 10);jbyteArray deviceid_j = (*env)->NewByteArray(env, 10);(*env)->SetByteArrayRegion(env, deviceid_j, 0, 10, (jbyte*)deviceid);//4. software_version, 4 bytesmemcpy(softwareversion, &uid_c[32], 4);jbyteArray softwareversion_j = (*env)->NewByteArray(env, 4);(*env)->SetByteArrayRegion(env, softwareversion_j, 0, 4, (jbyte*)softwareversion);//5. get product_serial, 8 bytesmemcpy(productfeature, &uid_c[36], 8);jbyteArray productfeature_j = (*env)->NewByteArray(env, 8);(*env)->SetByteArrayRegion(env, productfeature_j, 0, 8, (jbyte*)productfeature);//6. gen md5 for software_version+product_serial, 12 bytesGetMD5(&uid_c[32], 12, md5);memcpy(softwareMD5, md5, 16);jbyteArray softwareMD5_j = (*env)->NewByteArray(env, 16);(*env)->SetByteArrayRegion(env, softwareMD5_j, 0, 16, (jbyte*)softwareMD5);//7. get user password, 16 bytesmemcpy(userpswd, &uid_c[44], USERID_LEN);jbyteArray userpswd_j = (*env)->NewByteArray(env, USERID_LEN);(*env)->SetByteArrayRegion(env, userpswd_j, 0, USERID_LEN, (jbyte*)userpswd);//8. gen md5 for user password, 16 bytesGetMD5(&uid_c[44], USERID_LEN, md5);memcpy(userpswdMD5, md5, 16);jbyteArray userpswdMD5_j = (*env)->NewByteArray(env, USERID_LEN);(*env)->SetByteArrayRegion(env, userpswdMD5_j, 0, USERID_LEN, (jbyte*)userpswdMD5);//9. get time now, 4 bytesmemcpy(time, &uid_c[60], 4);jbyteArray time_j = (*env)->NewByteArray(env, 4);(*env)->SetByteArrayRegion(env, time_j, 0, 4, (jbyte*)time);jobject ret = (*env)->NewObject(env, jclassResultInfo, jidResultInfo, contractid_j, mac_j, deviceid_j, softwareversion_j, productfeature_j, softwareMD5_j, userpswd_j, userpswdMD5_j, time_j);(*env)->DeleteLocalRef(env, contractid_j);(*env)->DeleteLocalRef(env, mac_j);(*env)->DeleteLocalRef(env, deviceid_j);(*env)->DeleteLocalRef(env, softwareversion_j);(*env)->DeleteLocalRef(env, productfeature_j);(*env)->DeleteLocalRef(env, softwareMD5_j);(*env)->DeleteLocalRef(env, userpswd_j);(*env)->DeleteLocalRef(env, userpswdMD5_j);(*env)->DeleteLocalRef(env, time_j);return ret; }int fake_rsa_encrypt(const unsigned char* plaintext,unsigned int plaintext_size,unsigned char *ciphertext,size_t *ciphertext_size) {struct timeval time;gettimeofday(&time, NULL);// microsecond has 1 000 000// Assuming you did not need quite that accuracy// Also do not assume the system clock has that accuracy.srand((time.tv_sec * 1000) + (time.tv_usec / 1000));if ((plaintext_size < 128) && (!(plaintext_size & 3))) {int i, j;for (i = 0; i < (int)(128 - plaintext_size); i += 4) {long rdm = random();ciphertext[i + 0] = (rdm >> 24) & 0xff;ciphertext[i + 1] = (rdm >> 16) & 0xff;ciphertext[i + 2] = (rdm >> 8) & 0xff;ciphertext[i + 3] = (rdm)& 0xff;}for (i = 128 - plaintext_size, j = 0; i < 128; ++i, ++j) {ciphertext[i] = plaintext[j] ^ ciphertext[j];}}else {return -1;}*ciphertext_size = 128;return 0; }JNIEXPORT jstring JNICALL Java_com_openailab_oascloud_security_jni_cloud_CloudAuthJNI_getSnByUidOnServer (JNIEnv *env, jobject obj, jbyteArray uidLocal, jbyteArray key, jbyteArray iv) {uint8_t *uidLocal_c = (*env)->GetByteArrayElements(env, uidLocal, NULL);uint8_t *key_c = (*env)->GetByteArrayElements(env, key, 0);uint8_t *iv_c = (*env)->GetByteArrayElements(env, iv, 0);uint8_t sn[SN_LEN];uint8_t sn_reorder[SN_LEN];uint8_t sn_final[SN_LEN * 2];uint8_t SN[AES_KEY_SIZE + AES_IV_LEN];size_t SN_len = AES_KEY_SIZE + AES_IV_LEN;size_t ciphertext_len = SN_LEN;/*printf("uidLocal_c=\n");for (int i = 0; i < UID_LEN; i++){if (i > 0 && i % 16 == 0) {printf("\n");}printf("0x%02x,", uidLocal_c[i]);}printf("\n");printf("key_c=\n");for (int i = 0; i < AES_KEY_SIZE; i++){if (i > 0 && i % 16 == 0) {printf("\n");}printf("0x%02x,", key_c[i]);}printf("\n");printf("iv_c=\n");for (int i = 0; i < AES_IV_LEN; i++){if (i > 0 && i % 16 == 0) {printf("\n");}printf("0x%02x,", iv_c[i]);}printf("\n");*/// generate SNgenSN(SN, &SN_len, key_c, iv_c, uidLocal_c);/*printf("SN=\n");for (int i = 0; i < SN_len; i++){if (i > 0 && i % 16 == 0) {printf("\n");}printf("0x%02x,", SN[i]);}printf("\n");*/fake_rsa_encrypt(SN, SN_len, sn, &ciphertext_len);/*printf("sn=\n");for (int i = 0; i < ciphertext_len; i++){if (i > 0 && i % 16 == 0) {printf("\n");}printf("0x%02x,", sn[i]);}printf("\n");*/reoder(sn, sn_reorder, SN_LEN);/*printf("sn_reorder=\n");for (int i = 0; i < SN_LEN; i++){if (i > 0 && i % 16 == 0) {printf("\n");}printf("0x%02x,", sn_reorder[i]);}printf("\n");*/array2hex(sn_reorder, SN_LEN, sn_final);/*printf("sn_final=\n");for (int i = 0; i < SN_LEN * 2; i++){if (i > 0 && i % 16 == 0) {printf("\n");}printf("0x%02x,", sn_final[i]);}printf("\n");*/(*env)->ReleaseByteArrayElements(env, key, key_c, 0);(*env)->ReleaseByteArrayElements(env, iv, iv_c, 0);(*env)->DeleteLocalRef(env, uidLocal_c);return (*env)->NewStringUTF(env, sn_final); }JNIEXPORT jbyteArray JNICALL Java_com_openailab_oascloud_security_jni_cloud_CloudAuthJNI_aesCbcEncrypOnServer (JNIEnv *env, jobject obj, jbyteArray plaintext, jbyteArray key, jbyteArray iv) {jsize plaintext_len = (*env)->GetArrayLength(env, plaintext);uint8_t *plaintext_c = (*env)->GetByteArrayElements(env, plaintext, NULL);uint8_t *key_c = (*env)->GetByteArrayElements(env, key, 0);uint8_t *iv_c = (*env)->GetByteArrayElements(env, iv, 0);uint8_t *ciphertext = (uint8_t*)malloc(plaintext_len);int ret = aes_cbc_encryp(key_c, iv_c, plaintext_c, ciphertext, plaintext_len);if (ret) {(*env)->ReleaseByteArrayElements(env, key, key_c, 0);(*env)->ReleaseByteArrayElements(env, iv, iv_c, 0);(*env)->DeleteLocalRef(env, plaintext);return NULL;}jbyteArray retarr = (*env)->NewByteArray(env, plaintext_len);(*env)->SetByteArrayRegion(env, retarr, 0, plaintext_len, (jbyte*)ciphertext);free(ciphertext);(*env)->ReleaseByteArrayElements(env, key, key_c, 0);(*env)->ReleaseByteArrayElements(env, iv, iv_c, 0);(*env)->ReleaseByteArrayElements(env, plaintext, plaintext_c, 0);return retarr; }JNIEXPORT jbyteArray JNICALL Java_com_openailab_oascloud_security_jni_cloud_CloudAuthJNI_aesCbcDecrypOnServer (JNIEnv *env, jobject obj, jbyteArray ciphertext, jbyteArray key, jbyteArray iv) {jsize ciphertext_len = (*env)->GetArrayLength(env, ciphertext);uint8_t *ciphertext_c = (*env)->GetByteArrayElements(env, ciphertext, NULL);uint8_t *key_c = (*env)->GetByteArrayElements(env, key, 0);uint8_t *iv_c = (*env)->GetByteArrayElements(env, iv, 0);uint8_t *plaintext;plaintext = (uint8_t*)malloc(ciphertext_len);int ret = aes_cbc_decryp(key_c, iv_c, ciphertext_c, plaintext, ciphertext_len);if (ret) {(*env)->ReleaseByteArrayElements(env, key, key_c, 0);(*env)->ReleaseByteArrayElements(env, iv, iv_c, 0);(*env)->DeleteLocalRef(env, ciphertext_c);return NULL;}jbyteArray retarr = (*env)->NewByteArray(env, ciphertext_len);(*env)->SetByteArrayRegion(env, retarr, 0, ciphertext_len, (jbyte*)plaintext);free(plaintext);(*env)->ReleaseByteArrayElements(env, key, key_c, 0);(*env)->ReleaseByteArrayElements(env, iv, iv_c, 0);(*env)->ReleaseByteArrayElements(env, ciphertext, ciphertext_c, 0);return retarr; }JNIEXPORT jstring JNICALL Java_com_openailab_oascloud_security_jni_cloud_CloudAuthJNI_genMD5 (JNIEnv *env, jobject obj, jstring text) {jsize text_len = (*env)->GetStringUTFLength(env, text);uint8_t *text_c = (*env)->GetStringUTFChars(env, text, NULL);uint8_t md5[16];GetMD5(text_c, text_len, md5);(*env)->DeleteLocalRef(env, text);return (*env)->NewStringUTF(env, md5); }JNIEXPORT jobject JNICALL Java_com_openailab_oascloud_security_jni_cloud_CloudAuthJNI_genRsaKey (JNIEnv *env, jobject obj) {jclass jclassResultInfo = (*env)->FindClass((env), "com/openailab/oascloud/security/model/RsaBO");jmethodID jidResultInfo = (*env)->GetMethodID(env, jclassResultInfo, "<init>", "([B[B)V");T_rsa r;uint8_t pub_key[2048] = { 0 };uint8_t prv_key[8192] = { 0 };init_rsa_keys(&r);generate_rsa(&r);//1. public keymemcpy(pub_key, r.buf, 2048);//2. private keymemcpy(prv_key, r.buf, 8192);jbyteArray j_pub_key = (*env)->NewByteArray(env, 2048);(*env)->SetByteArrayRegion(env, j_pub_key, 0, 2048, (const jbyte*)pub_key);jbyteArray j_prv_key = (*env)->NewByteArray(env, 8192);(*env)->SetByteArrayRegion(env, j_prv_key, 0, 8192, (const jbyte*)prv_key);jobject ret = (*env)->NewObject(env, jclassResultInfo, jidResultInfo, j_pub_key, j_prv_key);(*env)->DeleteLocalRef(env, j_pub_key);(*env)->DeleteLocalRef(env, j_prv_key);return ret; }JNIEXPORT jbyteArray JNICALL Java_com_openailab_oascloud_security_jni_cloud_CloudAuthJNI_sm4Encrypt(JNIEnv *env, jobject obj, jbyteArray j_plain_text_arr, jbyteArray j_key_arr, jbyteArray j_iv_arr) {unsigned char enc_text[64] = { 0 };jbyteArray ret = NULL;jbyte *j_plain_text = NULL;jbyte *j_key = NULL;jbyte *j_iv = NULL;sms4_key_t sms4_key;int chars_len = 0;unsigned char *plain_text = { 0 };unsigned char *key = { 0 };unsigned char *iv = { 0 };uint8_t sm4_iv[AES_IV_LEN] = {0};//j_plain_text_arr to char *j_plain_text = (*env)->GetByteArrayElements(env, j_plain_text_arr, 0);chars_len = (*env)->GetArrayLength(env, j_plain_text_arr);plain_text = malloc(chars_len + 1);memset(plain_text, 0, chars_len + 1);memcpy(plain_text, j_plain_text, chars_len);//j_key_arr to char *chars_len = (*env)->GetArrayLength(env, j_key_arr);if (chars_len != 48) {printf("%s", "Fail to sm4 encrypt,key must be 48 bits.");exit(1);}j_key = (*env)->GetByteArrayElements(env, j_key_arr, 0);key = malloc(chars_len + 1);memset(key, 0, chars_len + 1);memcpy(key, j_key, chars_len);//j_iv_arr to char *chars_len = (*env)->GetArrayLength(env, j_iv_arr);if (chars_len != 16) {printf("%s", "Fail to sm4 encrypt,iv must be 16 bits.");exit(1);}j_iv = (*env)->GetByteArrayElements(env, j_iv_arr, 0);iv = malloc(chars_len + 1);memset(iv, 0, chars_len + 1);memcpy(iv, j_iv, chars_len);//sm4 encryptmemcpy(sms4_key.rk, key, 32);memcpy(sm4_iv, key + 16, 16);sms4_set_encrypt_key(&sms4_key, sm4_iv);sms4_cbc_encrypt(plain_text, enc_text, 64, (sms4_key_t *) sms4_key.rk, sm4_iv, 1);ret = (*env)->NewByteArray(env, 64);(*env)->SetByteArrayRegion(env, ret, 0, 64, (jbyte*)enc_text);//free memoryfree(key);free(iv);free(plain_text);(*env)->ReleaseByteArrayElements(env, j_iv_arr, j_iv, 0);(*env)->ReleaseByteArrayElements(env, j_key_arr, j_key, 0);(*env)->ReleaseByteArrayElements(env, j_plain_text_arr, j_plain_text, 0);return ret; }JNIEXPORT jbyteArray JNICALL Java_com_openailab_oascloud_security_jni_cloud_CloudAuthJNI_sm4Decrypt(JNIEnv *env, jobject obj, jbyteArray j_enc_text_arr, jbyteArray j_key_arr, jbyteArray j_iv_arr) {unsigned char dec_text[1024] = { 0 };jbyteArray ret = NULL;jbyte *j_enc_text = NULL;jbyte *j_key = NULL;jbyte *j_iv = NULL;sms4_key_t sms4_key;int enc_len = 0;int chars_len = 0;unsigned char *enc_text = { 0 };unsigned char *key = { 0 };unsigned char *iv = { 0 };//j_plain_text_arr to char *j_enc_text = (*env)->GetByteArrayElements(env, j_enc_text_arr, 0);chars_len = (*env)->GetArrayLength(env, j_enc_text_arr);enc_text = malloc(chars_len + 1);memset(enc_text, 0, chars_len + 1);memcpy(enc_text, j_enc_text, chars_len);//j_key_arr to char *chars_len = (*env)->GetArrayLength(env, j_key_arr);if (chars_len != 48) {printf("%s", "Fail to sm4 decrypt,key must be 48 bits.");exit(1);}j_key = (*env)->GetByteArrayElements(env, j_key_arr, 0);key = malloc(chars_len + 1);memset(key, 0, chars_len + 1);memcpy(key, j_key, chars_len);//j_iv_arr to char *chars_len = (*env)->GetArrayLength(env, j_iv_arr);if (chars_len != 16) {printf("%s", "Fail to sm4 decrypt,iv must be 16 bits.");exit(1);}j_iv = (*env)->GetByteArrayElements(env, j_iv_arr, 0);iv = malloc(chars_len + 1);memset(iv, 0, chars_len + 1);memcpy(iv, j_iv, chars_len);//sm4 decrypmemcpy(sms4_key.rk, key, 32);sms4_set_decrypt_key(&sms4_key, iv);sms4_cbc_encrypt(enc_text, dec_text, 64, sms4_key.rk, iv, 0);ret = (*env)->NewByteArray(env, strlen(dec_text));(*env)->SetByteArrayRegion(env, ret, 0, strlen(dec_text), (jbyte*)dec_text);//free memoryfree(key);free(iv);free(enc_text);(*env)->ReleaseByteArrayElements(env, j_iv_arr, j_iv, 0);(*env)->ReleaseByteArrayElements(env, j_key_arr, j_key, 0);(*env)->ReleaseByteArrayElements(env, j_enc_text_arr, j_enc_text, 0);return ret; }注意:頭文件的方法名需要與Java文件的全限定名一致,Java代碼目錄結構如下:
到此JNI在實際項目中的使用方法就介紹完了。
總結
以上是生活随笔為你收集整理的JNI 在实际项目中的使用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 初学Java-接口
- 下一篇: WinAPI: midiOutGetID