├── .gitignore
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── github
│ │ └── maxwell
│ │ └── nc
│ │ └── nativestrencrypt
│ │ └── MainActivity.java
│ └── res
│ ├── layout
│ └── activity_main.xml
│ ├── mipmap-xhdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ └── values
│ ├── colors.xml
│ ├── strings.xml
│ └── styles.xml
├── build.gradle
├── encryptlib
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── github
│ │ └── maxwell
│ │ └── nc
│ │ └── encryptlib
│ │ └── EncryptUtils.java
│ ├── jni
│ ├── Android.mk
│ ├── Application.mk
│ ├── DecryptAPI.cpp
│ ├── DecryptAPI.h
│ ├── EncryptUtils.cpp
│ ├── EncryptUtils.h
│ ├── Tools.cpp
│ └── Tools.h
│ ├── libs
│ ├── armeabi-v7a
│ │ └── libmaxwell_encrypt.so
│ ├── armeabi
│ │ └── libmaxwell_encrypt.so
│ └── x86
│ │ └── libmaxwell_encrypt.so
│ └── res
│ └── values
│ └── strings.xml
├── gradle.properties
├── gradlew
├── gradlew.bat
├── preview
├── 1.jpg
├── 2.jpg
├── 3.jpg
├── 4.jpg
└── 5.jpg
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the ART/Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 | out/
15 |
16 | # Gradle files
17 | .gradle/
18 | build/
19 |
20 | # Local configuration file (sdk path, etc)
21 | local.properties
22 |
23 | # Proguard folder generated by Eclipse
24 | proguard/
25 |
26 | # Log Files
27 | *.log
28 |
29 | # Android Studio Navigation editor temp files
30 | .navigation/
31 |
32 | # Android Studio captures folder
33 | captures/
34 |
35 | # Intellij
36 | *.iml
37 | .idea/workspace.xml
38 | .idea/tasks.xml
39 | .idea/gradle.xml
40 | .idea/dictionaries
41 | .idea/libraries
42 |
43 | # Keystore files
44 | *.jks
45 |
46 | # External native build folder generated in Android Studio 2.2 and later
47 | .externalNativeBuild
48 |
49 | # Google Services (e.g. APIs or Firebase)
50 | google-services.json
51 |
52 | # Freeline
53 | freeline.py
54 | freeline/
55 | freeline_project_description.json
56 | /.idea
57 | /encryptlib/src/main/obj
58 | /gradle
59 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Android字符串加密库
2 | An Android native library (*.so) which encrypt/decrypt string and proguard it to prevent decompile.
3 |
4 | ## 特点 Feature
5 |
6 | - 正版应用调用校验(防止二次打包等问题)
7 | - 函数名及内容混淆
8 | - 关键代码及字符串隐藏
9 | - 支持自定义加密解密扩展
10 | - 入口防止动态调试等等
11 |
12 |
13 | ## 预览 Preview
14 |
15 | 下面是正版应用调用展示:
16 |
17 | 
18 |
19 |
20 | 下面是非法应用调用结果(包名或签名不一致):
21 |
22 | 
23 |
24 | 下面是反编译后函数列表(混淆):
25 |
26 | 
27 |
28 | 下面是搜索关键数据(如加密密钥、加密后的数据):
29 |
30 | 
31 |
32 | 下面是关键代码片段反生成代码(加大反编译难度):
33 |
34 | 
35 |
36 |
37 | ## 用法 Usage
38 |
39 | 注意:下面均忽略JNI调用的通用步骤
40 |
41 | 1.配置你的应用签名及包名
42 |
43 | 打开Tools.cpp,修改替换sign和validPackage内容,比如签名为:-498576263,包名为:com.github.maxwell.nc.nativestrencrypt,则可以修改为如下代码:
44 | ```c
45 | //正版签名hashcode,换签名请修改这个
46 | //忽略hashcode碰撞问题(另外有包名校验)
47 | //必须是使用tricks写法,直接int可以从so中静态反编译找到(容易被替换)
48 | char* sign = (char[]){'-','4','9','8','5','7','6','2','6','3'};
49 | int vaildSignHash = atoi(sign);
50 |
51 | //正版包名
52 | //可以任意分割,见nstrcat
53 | const char* validPackage = nstrcat("com","co","m",".","gi","thub",".","max","we","ll",
54 | ".","nc",".","native","str","encrypt","com");
55 | ```
56 | 如果你不确定你的包名和签名,可以通过打开日志信息,观察日志中生成的hashcode和sign即为当前应用的签名hashcode及包名。
57 |
58 | 2.配置你的原生函数
59 |
60 | 由于是采用反射获取方法名称,需要通过配置regClazz(调用原生方法的包名)和gMethods(原生方法名、方法签名、函数指针),然后参考getSamplePass函数调用decryptStr方法进行解密:
61 | ```c
62 | __attribute__((section ("datas")))
63 | JNICALL jstring getSamplePass(JNIEnv *env, jclass clazz, jobject object){
64 | //这里的 616263898b8a 为加密后的字符串(可以任意分割)
65 | return decryptStr(env,object,"616","263","8","98","b8a",fakePass);
66 | }
67 |
68 | //要寻找的类:配置你的包名
69 | static char regClazz[] = "com/github/maxwell/nc/encryptlib/EncryptUtils";
70 |
71 | //要寻找的方法:配置你的方法
72 | static JNINativeMethod gMethods[] = {
73 | { "getSamplePass", "(Ljava/lang/Object;)Ljava/lang/String;", (void*)getSamplePass },
74 | };
75 | ```
76 |
77 |
78 | 3.调用原生方法
79 | ```java
80 | String samplePass = EncryptUtils.getSamplePass(this);
81 | ```
82 | 如果错误则会返回预定义错误的字符串
83 |
84 | 4.特别注意!!!
85 |
86 | 源代码中加密解密方法随便写的,十分简单,不能用于实际应用。建议采用高强度加密解密算法(最好修改通用方法,加强安全)
87 |
88 | ## 进阶 Advance
89 |
90 | 打开日志信息
91 |
92 | 可以通过修改Tools.h中的宏定义:
93 | ```c
94 | #define DEBUG true
95 | ```
96 |
97 | 修改预定义错误返回字符串
98 |
99 | 可以通过修改EncryptUtils.cpp中的decryptStr函数中的:
100 | ```c
101 | char* failStr = (char[]){'e','r','r','o','r','\0'};
102 | ```
103 |
104 | 其他事项
105 |
106 | 暂时没想到,以后有空再补充。
107 |
108 | Sorry, I have no empty time to translate this readme file into English.
109 |
110 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.2"
6 | defaultConfig {
7 | applicationId "com.github.maxwell.nc.nativestrencrypt"
8 | minSdkVersion 16
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.0"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | implementation fileTree(include: ['*.jar'], dir: 'libs')
23 | implementation project(':encryptlib')
24 | }
25 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in E:\Development\Android-SDK/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/maxwell/nc/nativestrencrypt/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.github.maxwell.nc.nativestrencrypt;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 | import android.view.View;
6 | import android.widget.TextView;
7 |
8 | import com.github.maxwell.nc.encryptlib.EncryptUtils;
9 |
10 | public class MainActivity extends Activity {
11 |
12 | @Override
13 | protected void onCreate(Bundle savedInstanceState) {
14 | super.onCreate(savedInstanceState);
15 | setContentView(R.layout.activity_main);
16 | }
17 |
18 | /**
19 | * 按钮点击事件
20 | */
21 | public void onButtonClick(View view) {
22 | getPassword();
23 | }
24 |
25 | /**
26 | * so调用例子
27 | */
28 | public void getPassword() {
29 | TextView tvPass = (TextView) findViewById(R.id.tv_pass);
30 |
31 | String samplePass = EncryptUtils.getSamplePass(this);//获取密码
32 | if (samplePass.equals("error")) {
33 | tvPass.setText("获取结果:非合法应用调用!");
34 | } else {
35 | tvPass.setText(String.format("获取结果:%s", samplePass));
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
16 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/maxwell-nc/AndroidNativeStringEncryptLibrary/b07a18e6d80c55de315cc0651812d8afdb5ff0e5/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/maxwell-nc/AndroidNativeStringEncryptLibrary/b07a18e6d80c55de315cc0651812d8afdb5ff0e5/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | NativeStrEncrypt
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 |
5 | repositories {
6 | google()
7 | jcenter()
8 | }
9 | dependencies {
10 | classpath 'com.android.tools.build:gradle:3.0.0-alpha4'
11 |
12 |
13 | // NOTE: Do not place your application dependencies here; they belong
14 | // in the individual module build.gradle files
15 | }
16 | }
17 |
18 | allprojects {
19 | repositories {
20 | google()
21 | jcenter()
22 | }
23 | }
24 |
25 | task clean(type: Delete) {
26 | delete rootProject.buildDir
27 | }
28 |
--------------------------------------------------------------------------------
/encryptlib/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/encryptlib/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.2"
6 |
7 |
8 | defaultConfig {
9 | minSdkVersion 16
10 | targetSdkVersion 25
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | sourceSets.main {
15 | jni.srcDirs = []
16 | jniLibs.srcDir 'src/main/libs'
17 | }
18 | buildTypes {
19 | release {
20 | minifyEnabled false
21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
22 | }
23 | }
24 | }
25 |
26 | dependencies {
27 | implementation fileTree(dir: 'libs', include: ['*.jar'])
28 | }
29 |
--------------------------------------------------------------------------------
/encryptlib/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in E:\Development\Android-SDK/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/encryptlib/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/encryptlib/src/main/java/com/github/maxwell/nc/encryptlib/EncryptUtils.java:
--------------------------------------------------------------------------------
1 | package com.github.maxwell.nc.encryptlib;
2 |
3 | /**
4 | * 字符串加密工具类
5 | */
6 | public class EncryptUtils {
7 |
8 | static {
9 | System.loadLibrary("maxwell_encrypt");
10 | }
11 |
12 | /**
13 | * 获取密码例子
14 | * 原密码:abcefg
15 | */
16 | public static native String getSamplePass(Object context);
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/encryptlib/src/main/jni/Android.mk:
--------------------------------------------------------------------------------
1 | LOCAL_PATH := $(call my-dir)
2 | include $(CLEAR_VARS)
3 | LOCAL_MODULE := maxwell_encrypt
4 | LOCAL_SRC_FILES := EncryptUtils.cpp DecryptAPI.cpp Tools.cpp
5 | LOCAL_CFLAGS := -fvisibility=hidden -fno-stack-protector -Wall -Wno-address-of-array-temporary
6 | LOCAL_LDLIBS += -llog
7 | include $(BUILD_SHARED_LIBRARY)
--------------------------------------------------------------------------------
/encryptlib/src/main/jni/Application.mk:
--------------------------------------------------------------------------------
1 | APP_ABI := armeabi armeabi-v7a x86
--------------------------------------------------------------------------------
/encryptlib/src/main/jni/DecryptAPI.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // 加密API
3 | // 随便写的加密,!!!十分不安全!!!
4 | // 这里只是提供两个接口,可以选择安全的加密方法
5 | // 另外建议不要留下加密方法,防止碰撞
6 | //
7 | #include "DecryptAPI.h"
8 |
9 |
10 | #if DEBUG
11 | char* encrypt(char* content,char* password){
12 | int clen = strlen(content);
13 | int plen = strlen(password);
14 |
15 | logInfo("encrypt 1 clen:%d c:%s",clen,content);
16 | logInfo("encrypt 2 plen:%d p:%s",plen,password);
17 |
18 | for(int i=0;i
6 | #include
7 | #include
8 | #include
9 |
10 | #include "Tools.h"
11 |
12 | #if DEBUG
13 | /**
14 | * 加密接口
15 | */
16 | __attribute__((section (".datas")))
17 | char* encrypt(char* content,char *password);
18 | #endif
19 |
20 | /**
21 | * 解密接口
22 | */
23 | __attribute__((section (".datas")))
24 | char* decrypt(char* content,char *password);
--------------------------------------------------------------------------------
/encryptlib/src/main/jni/EncryptUtils.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // JNI加密工具类
3 | //
4 | #include "EncryptUtils.h"
5 |
6 |
7 | /*
8 | * 解密逻辑
9 | * 可以替换成任意解密算法,调用DecryptAPI
10 | */
11 | __attribute__((section (".datas")))
12 | jstring decryptInside(JNIEnv *env, jstring str){
13 | char* ecpStr;
14 | ecpStr = (char*)env->GetStringUTFChars(str,NULL);
15 |
16 | //解密用key: abc$%#1234
17 | //下面写法是tricks,防止静态编译寻找字符串(DCB指令)
18 | char* strop = (char[]){'a','b','c','$','%','#','\0'};
19 | int intop = 1234;
20 |
21 | //生成操作密码
22 | char opInt[strlen(strop)+sizeof(intop)];
23 | sprintf(opInt, "%s%d", strop, intop);
24 |
25 | //加密用利用日志查看生产的加密密钥
26 | #if DEBUG
27 | char enWord[] = "password";
28 | encrypt(enWord,opInt);
29 | #endif
30 |
31 | return env->NewStringUTF(decrypt(ecpStr,opInt));
32 | }
33 |
34 |
35 | /*
36 | * 解密字符串
37 | */
38 | __attribute__((section ("datas")))
39 | jstring decryptStr(JNIEnv *env, jobject object, ...){
40 | if(validApp(env,object)){//正版应用
41 | char* dest = new char[100];
42 | va_list pi;
43 | char *p;
44 | va_start(pi, object);
45 | int count = 0;
46 | while ((p = va_arg(pi, char *)) != fakePass){//通过va_arg(pi,char *)来提取参数列表中的变量
47 | logInfo("va char:%s",p);
48 | if(count==0){
49 | strcpy(dest,p);
50 | }else{
51 | strcat(dest, p);
52 | }
53 | count++;
54 | }
55 | va_end(pi);
56 | logInfo("va str:%s",dest);
57 | jstring jstr = env->NewStringUTF(dest);
58 | free(dest);//回收
59 | return decryptInside(env,jstr);
60 | }
61 | //tricks 防止根据关键字hack
62 | char* failStr = (char[]){'e','r','r','o','r','\0'};
63 | return env->NewStringUTF(failStr);
64 | }
65 |
66 |
67 | jint JNI_OnLoad(JavaVM* vm,void* reserved){
68 |
69 | //防止调试
70 | ptrace(PTRACE_TRACEME,0,0,0);
71 |
72 | JNIEnv* env;
73 | if (vm->GetEnv((void**)(&env), JNI_VERSION_1_6) != JNI_OK){
74 | return -1;
75 | }
76 | assert(env != NULL);
77 |
78 | //寻找类
79 | jclass clazz = env->FindClass(regClazz);
80 | if (clazz == NULL) {
81 | return -1;
82 | }
83 |
84 | //注册方法
85 | int numMethods = sizeof(gMethods) / sizeof(gMethods[0]);
86 | if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
87 | return -1;
88 | }
89 |
90 | return JNI_VERSION_1_6;
91 | }
92 |
--------------------------------------------------------------------------------
/encryptlib/src/main/jni/EncryptUtils.h:
--------------------------------------------------------------------------------
1 | //
2 | // 加密工具类
3 | //
4 |
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 |
13 | #include "Tools.h"
14 | #include "DecryptAPI.h"
15 |
16 | //假的密码,实际上是用于定位结束
17 | static const char* fakePass = "gg0a07d3kjbkk";
18 |
19 | jstring decryptStr(JNIEnv*, jobject, ...);
20 |
21 | __attribute__((section ("datas")))
22 | JNICALL jstring getSamplePass(JNIEnv *env, jclass clazz, jobject object){
23 | //这里的 616263898b8a 为加密后的字符串(可以任意分割)
24 | return decryptStr(env,object,"616","263","8","98","b8a",fakePass);
25 | }
26 |
27 | //要寻找的类:配置你的包名
28 | static char regClazz[] = "com/github/maxwell/nc/encryptlib/EncryptUtils";
29 |
30 | //要寻找的方法:配置你的方法
31 | static JNINativeMethod gMethods[] = {
32 | { "getSamplePass", "(Ljava/lang/Object;)Ljava/lang/String;", (void*)getSamplePass },
33 | };
34 |
--------------------------------------------------------------------------------
/encryptlib/src/main/jni/Tools.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // 工具函数
3 | // 部分代码修改自 http://blog.csdn.net/jeanphorn/article/details/45560697
4 | //
5 | #include "Tools.h"
6 |
7 |
8 | /*
9 | * 两个字符转换成一个字符,长度为原来的1/2
10 | */
11 | static void Hex2Char(char *szHex, unsigned char *rch){
12 | int i;
13 | for(i=0; i<2; i++)
14 | {
15 | if(*(szHex + i) >='0' && *(szHex + i) <= '9')
16 | *rch = (*rch << 4) + (*(szHex + i) - '0');
17 | else if(*(szHex + i) >='a' && *(szHex + i) <= 'f')
18 | *rch = (*rch << 4) + (*(szHex + i) - 'a' + 10);
19 | else
20 | break;
21 | }
22 | }
23 |
24 |
25 | /*
26 | * 十六进制char* 转 Binary char*函数
27 | */
28 | void HexStr2CharStr(char *pszHexStr, int iSize, char *pucCharStr){
29 | int i;
30 | unsigned char ch;
31 | if (iSize%2 != 0) return;
32 | for(i=0; i= 0 && byte[i] <= 9)
51 | szHex[i] = '0' + byte[i];
52 | else
53 | szHex[i] = 'a' + byte[i] - 10;
54 | }
55 | szHex[2] = 0;
56 | }
57 |
58 |
59 | /*
60 | * 字符串转换函数,中间调用上面的函数
61 | */
62 | void CharStr2HexStr(char *pucCharStr, int iSize, char *pszHexStr){
63 | int i;
64 | char szHex[3];
65 | pszHexStr[0] = 0;
66 | for(i=0; iFindClass("android/content/Context");
80 | jclass signatureClass = env->FindClass("android/content/pm/Signature");
81 | jclass packageNameClass = env->FindClass("android/content/pm/PackageManager");
82 | jclass packageInfoClass = env->FindClass("android/content/pm/PackageInfo");
83 |
84 | jmethodID getPackageManagerId = env->GetMethodID(contextClass, "getPackageManager","()Landroid/content/pm/PackageManager;");
85 | jmethodID getPackageNameId = env->GetMethodID(contextClass, "getPackageName","()Ljava/lang/String;");
86 | jmethodID getPackageInfoId = env->GetMethodID(packageNameClass, "getPackageInfo","(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;");
87 | jmethodID hashCodeId = env->GetMethodID(signatureClass,"hashCode", "()I");
88 |
89 | //获取包信息
90 | jobject packageManagerObject = env->CallObjectMethod(contextObject, getPackageManagerId);
91 | jstring packNameString = (jstring)env->CallObjectMethod(contextObject, getPackageNameId);
92 | jobject packageInfoObject = env->CallObjectMethod(packageManagerObject, getPackageInfoId, packNameString, 64);
93 |
94 | //获取签名
95 | jfieldID signaturefieldID =env->GetFieldID(packageInfoClass,"signatures", "[Landroid/content/pm/Signature;");
96 | jobjectArray signatureArray = (jobjectArray)env->GetObjectField(packageInfoObject, signaturefieldID);
97 | jobject signatureObject = env->GetObjectArrayElement(signatureArray,0);
98 |
99 | //获取当前包名
100 | const char* packName = env->GetStringUTFChars(packNameString, 0);
101 |
102 | //获取当前签名的hashcode
103 | jint hashCode = env->CallIntMethod(signatureObject, hashCodeId);
104 |
105 | logInfo("current hashcode:%d",hashCode);//这是当前应用签名的hashcode,替换下方的正版签名hashcode
106 | logInfo("current packageName:%s",packName);//这是当前应用包名,替换下方的正版签名
107 |
108 | //正版签名hashcode,换签名请修改这个
109 | //忽略hashcode碰撞问题(另外有包名校验)
110 | //必须是使用tricks写法,直接int可以从so中静态反编译找到(容易被替换)
111 | char* sign = (char[]){'-','4','9','8','5','7','6','2','6','3','\0'};
112 | int vaildSignHash = atoi(sign);
113 |
114 | //正版包名
115 | //可以任意分割,见nstrcat
116 | const char* validPackage = nstrcat("com","co","m",".","gi","thub",".","max","we","ll",
117 | ".","nc",".","native","str","encrypt","com");
118 |
119 | //包名并且签名一致
120 | if(hashCode == vaildSignHash && strcmp(packName, validPackage) == 0){
121 | return true;
122 | }
123 |
124 | return false;
125 | }
126 |
127 |
128 | /*
129 | * 合并任意长度字符串
130 | * 第一个slat参数必须要出现在最后一个可变参数中,作为标记起始和结束符号(不能与中间字符重复)
131 | */
132 | __attribute__((section ("datas")))
133 | char* nstrcat(const char* slat,...){
134 | char* dest = new char[128];
135 | va_list pi;
136 | char *p;
137 | va_start(pi, slat);
138 | int count = 0;
139 | while ((p = va_arg(pi, char *)) != slat){//通过va_arg(pi,char *)来提取参数列表中的变量
140 | logInfo("va char:%s",p);
141 | if(count==0){
142 | strcpy(dest,p);
143 | }else{
144 | strcat(dest, p);
145 | }
146 | count++;
147 | }
148 | va_end(pi);
149 | logInfo("va str:%s",dest);
150 | return dest;
151 | }
152 |
--------------------------------------------------------------------------------
/encryptlib/src/main/jni/Tools.h:
--------------------------------------------------------------------------------
1 | //
2 | // 工具类头文件
3 | //
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 |
11 | //是否需要打印Log
12 | #define DEBUG false
13 |
14 | //打印标签
15 | #define TAG "JNI_LOG"
16 |
17 | #if DEBUG
18 | #define logInfo(...) __android_log_print(ANDROID_LOG_INFO,TAG ,__VA_ARGS__)
19 | #define logErr(...) __android_log_print(ANDROID_LOG_ERROR,TAG ,__VA_ARGS__)
20 | #else
21 | #define logInfo(...)
22 | #define logErr(...)
23 | #endif
24 |
25 | void HexStr2CharStr(char *pszHexStr, int iSize, char *pucCharStr);
26 | void CharStr2HexStr(char *pucCharStr, int iSize, char *pszHexStr);
27 | bool validApp(JNIEnv * env,jobject contextObject);
28 | char* nstrcat(const char* ,...);
29 |
--------------------------------------------------------------------------------
/encryptlib/src/main/libs/armeabi-v7a/libmaxwell_encrypt.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/maxwell-nc/AndroidNativeStringEncryptLibrary/b07a18e6d80c55de315cc0651812d8afdb5ff0e5/encryptlib/src/main/libs/armeabi-v7a/libmaxwell_encrypt.so
--------------------------------------------------------------------------------
/encryptlib/src/main/libs/armeabi/libmaxwell_encrypt.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/maxwell-nc/AndroidNativeStringEncryptLibrary/b07a18e6d80c55de315cc0651812d8afdb5ff0e5/encryptlib/src/main/libs/armeabi/libmaxwell_encrypt.so
--------------------------------------------------------------------------------
/encryptlib/src/main/libs/x86/libmaxwell_encrypt.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/maxwell-nc/AndroidNativeStringEncryptLibrary/b07a18e6d80c55de315cc0651812d8afdb5ff0e5/encryptlib/src/main/libs/x86/libmaxwell_encrypt.so
--------------------------------------------------------------------------------
/encryptlib/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | encryptlib
3 |
4 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | ## Project-wide Gradle settings.
2 | #
3 | # For more details on how to configure your build environment visit
4 | # http://www.gradle.org/docs/current/userguide/build_environment.html
5 | #
6 | # Specifies the JVM arguments used for the daemon process.
7 | # The setting is particularly useful for tweaking memory settings.
8 | # Default value: -Xmx1024m -XX:MaxPermSize=256m
9 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
10 | #
11 | # When configured, Gradle will run in incubating parallel mode.
12 | # This option should only be used with decoupled projects. More details, visit
13 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
14 | # org.gradle.parallel=true
15 | #Mon Sep 04 14:30:58 CST 2017
16 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/preview/1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/maxwell-nc/AndroidNativeStringEncryptLibrary/b07a18e6d80c55de315cc0651812d8afdb5ff0e5/preview/1.jpg
--------------------------------------------------------------------------------
/preview/2.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/maxwell-nc/AndroidNativeStringEncryptLibrary/b07a18e6d80c55de315cc0651812d8afdb5ff0e5/preview/2.jpg
--------------------------------------------------------------------------------
/preview/3.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/maxwell-nc/AndroidNativeStringEncryptLibrary/b07a18e6d80c55de315cc0651812d8afdb5ff0e5/preview/3.jpg
--------------------------------------------------------------------------------
/preview/4.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/maxwell-nc/AndroidNativeStringEncryptLibrary/b07a18e6d80c55de315cc0651812d8afdb5ff0e5/preview/4.jpg
--------------------------------------------------------------------------------
/preview/5.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/maxwell-nc/AndroidNativeStringEncryptLibrary/b07a18e6d80c55de315cc0651812d8afdb5ff0e5/preview/5.jpg
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':encryptlib'
2 |
--------------------------------------------------------------------------------