Directly run the Code:
# Include <stdlib. h> # include <stdio. h> # include <getopt. h> # include <string. h> # include "base64.h" extern "C" {// base64 encoded int base64encode (unsigned char * orgstring, unsigned char * base64string, int orgstringlen) {int base64stringlen = 0; static unsigned char base64encode [] = "[email protected]"; 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 ++ = BAS E64encode [(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 decode char getbase64value (unsigned char ch) // obtain the encoded value {If (CH> = 'A ') & (CH <= 'Z') // ~ Z return ch-'A'; If (CH> = 'A') & (CH <= 'Z') // ~ Z return ch-'A' + 26; If (CH> = '0') & (CH <= '9') // 0 ~ 9 return ch-'0' + 52; Switch (CH) // other characters {case '. ': Return 62; Case' @ ': return 63; Case' _ ': // fill in the base64 character return 0; default: Return 0 ;}} // decoding function int base64decode (unsigned char * orgstring, unsigned char * base64string, int base64stringlen, bool bforcedecode) // decoding function {If (base64stringlen % 4 &&! Bforcedecode) // if it is not a multiple of 4, the base64 encoding is incorrect {orgstring [0] = '\ 0'; Return-1;} unsigned char base64encode [4]; int orgstringlen = 0; while (base64stringlen> 2) // when the number of codes to be decoded is less than three, it will be ignored (valid during forced decoding) {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;} // the completion of the base64 function}
JNI interaction code
/** Class: com_encrypt_encryptactivity * method: base64encrypt * Signature: (ljava/lang/string;) ljava/lang/string; */jniexport jstring jnicall encode (jnienv * ENV, jobject, jstring Str) {// receives string variables passed by the Java end. The const char * string to be encoded; // base64 encrypted string pointer unsigned char * base64string; // receives the Java string = env-> getstringutfchars (STR, 0); // calculates the length of the string int Len = ST Rlen (string) + 1; // allocate space for the string, usually a group of 4 bytes, 4 base64string = new unsigned char [Len * 2 + 4]; // encode base64encode (unsigned char *) string, base64string, len); _ android_log_print (android_log_info, "[info] [base64encyrpt]", "Hello, base64 encode \ n % s", (char *) base64string ); // release the string received from the Java end env-> releasestringutfchars (STR, string); // return the encrypted string to the Java end 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) {// string pointer unsigned char * decodestr after base64 decoding; // receives the string variable const char * datestring from the Java end; // receives the string datestring = env-> getstringutfchars (STR, 0); // the string length int Len = strlen (datestring) + 1; // allocate space for the string decodestr = new unsigned char [Len]; // base64 decode base64decode (decodestr, (unsigned char *) datestring, len, true); _ android_log_print (android_log_info, "[info] [base64decyrpt]", "Hello, base64 decode \ n % s! ", Decodestr); // release the string received from Java-> releasestringutfchars (STR, datestring ); // return the decoded string to the Java return env-> newstringutf (char *) decodestr );}
Java declaration:
public native String base64Encrypt(String str); public native String base64Decrypt(String str);
Loading and receiving:
Static {system. loadlibrary ("generated library name ");}
File Download: base64 encoding
Http: // 192.210.60.138/DZ/Forum. php? MoD = viewthread & tid = 15 & fromuid = 1
(Source: istudy)