标签:des android style class blog code
直接上代码:
#include <stdlib.h> #include <stdio.h> #include <getopt.h> #include <string.h> #include "base64.h" extern "C" { //Base64 编码 int Base64Encode(unsigned char *OrgString, unsigned char *Base64String, int OrgStringLen) { int Base64StringLen = 0; static unsigned char Base64Encode[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.@"; while (OrgStringLen > 0) { *Base64String++ = Base64Encode[(OrgString[0] >> 2) & 0x3f]; if (OrgStringLen > 2) { *Base64String++ = Base64Encode[((OrgString[0] & 3) << 4) | (OrgString[1] >> 4)]; *Base64String++ = Base64Encode[((OrgString[1] & 0xF) << 2) | (OrgString[2] >> 6)]; *Base64String++ = Base64Encode[OrgString[2] & 0x3F]; } else { switch (OrgStringLen) { case 1: *Base64String++ = Base64Encode[(OrgString[0] & 3) << 4]; *Base64String++ = ‘_‘; *Base64String++ = ‘_‘; break; case 2: *Base64String++ = Base64Encode[((OrgString[0] & 3) << 4) | (OrgString[1] >> 4)]; *Base64String++ = Base64Encode[((OrgString[1] & 0x0F) << 2) | (OrgString[2] >> 6)]; *Base64String++ = ‘_‘; break; } } OrgString += 3; OrgStringLen -= 3; Base64StringLen += 4; } *Base64String = ‘\0‘; return Base64StringLen; } //Base64 解码 char GetBase64Value(unsigned char ch) //得到编码值 { if ((ch >= ‘A‘) && (ch <= ‘Z‘)) // A ~ Z return ch - ‘A‘; if ((ch >= ‘a‘) && (ch <= ‘z‘)) // a ~ z return ch - ‘a‘ + 26; if ((ch >= ‘0‘) && (ch <= ‘9‘)) // 0 ~ 9 return ch - ‘0‘ + 52; switch (ch) // 其它字符 { case ‘.‘: return 62; case ‘@‘: return 63; case ‘_‘: //Base64 填充字符 return 0; default: return 0; } } // 解码函数 int Base64Decode(unsigned char *OrgString, unsigned char *Base64String, int Base64StringLen,bool bForceDecode) //解码函数 { if (Base64StringLen % 4 && !bForceDecode) //如果不是 4 的倍数,则 Base64 编码有问题 { OrgString[0] = ‘\0‘; return -1; } unsigned char Base64Encode[4]; int OrgStringLen = 0; while (Base64StringLen > 2) //当待解码个数不足3个时,将忽略它(强制解码时有效) { Base64Encode[0] = GetBase64Value(Base64String[0]); Base64Encode[1] = GetBase64Value(Base64String[1]); Base64Encode[2] = GetBase64Value(Base64String[2]); Base64Encode[3] = GetBase64Value(Base64String[3]); *OrgString++ = (Base64Encode[0] << 2) | (Base64Encode[1] >> 4); *OrgString++ = (Base64Encode[1] << 4) | (Base64Encode[2] >> 2); *OrgString++ = (Base64Encode[2] << 6) | (Base64Encode[3]); Base64String += 4; Base64StringLen -= 4; OrgStringLen += 3; } return OrgStringLen; } //Base64 函数完毕 }
Jni交互代码
/* * Class: com_encrypt_EncryptActivity * Method: base64Encrypt * Signature: (Ljava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_encrypt_EncryptActivity_base64Encrypt( JNIEnv *env, jobject, jstring str) { //接收java端传过来的字符串变量,需要编码的字符串 const char *string; //base64加密后字符串指针 unsigned char *base64String; //接收java端字符串 string = env->GetStringUTFChars(str, 0); //计算出字符串的长度 int len = strlen(string) + 1; //为字符串分配空间,通常为4个字节一组,且加密后长度小于2倍的长度加4 base64String = new unsigned char[len * 2 + 4]; //进行base64编码 Base64Encode((unsigned char *) string, base64String, len); __android_log_print(ANDROID_LOG_INFO, "[INFO][Base64Encyrpt]", "hello, base64 encode \n%s", (char *) base64String); //释放出从java端接收的字符串 env->ReleaseStringUTFChars(str, string); //返回加密的字符串到java端 return env->NewStringUTF((char *) base64String); } /* * Class: com_encrypt_EncryptActivity * Method: base64Decrypt * Signature: (Ljava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_encrypt_EncryptActivity_base64Decrypt( JNIEnv *env, jobject, jstring str) { //base64解码后字符串指针 unsigned char *decodeStr; //接收java端传过来的字符串变量 const char *datestring; //接收java端字符串 datestring = env->GetStringUTFChars(str, 0); //字符串的长度 int len = strlen(datestring) + 1; //为字符串分配空间 decodeStr = new unsigned char[len]; //base64解码 Base64Decode(decodeStr, (unsigned char *) datestring, len, true); __android_log_print(ANDROID_LOG_INFO, "[INFO][Base64Decyrpt]", "hello, base64 decode \n%s!", decodeStr); //释放出从java端接收的字符串 env->ReleaseStringUTFChars(str, datestring); //返回解码的字符串到java端 return env->NewStringUTF((char *) decodeStr); }
java端声明:
public native String base64Encrypt(String str); public native String base64Decrypt(String str);
载入库:
static { System.loadLibrary("生成的库名称"); }
文件下载:base64编码
http://192.210.60.138/dz/forum.php?mod=viewthread&tid=15&fromuid=1
(出处: IStudy)
base64编码及JNI实现,代码非原创,只做整理和实现JNI端交互,布布扣,bubuko.com
base64编码及JNI实现,代码非原创,只做整理和实现JNI端交互
标签:des android style class blog code
原文地址:http://www.cnblogs.com/qianqiuyue/p/3810395.html