Skip to content

Commit

Permalink
android_aec256_cbc_encrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
莫卓文 authored and 莫卓文 committed Sep 30, 2015
0 parents commit 56bddfe
Show file tree
Hide file tree
Showing 59 changed files with 1,749 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
33 changes: 33 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>android_aes256_cbc_encrypt</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.android.ide.eclipse.adt.ApkBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
27 changes: 27 additions & 0 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.biancity.aes256test"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="22" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
27 changes: 27 additions & 0 deletions bin/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.biancity.aes256test"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="22" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Binary file added bin/android_aes256_cbc_encrypt.apk
Binary file not shown.
Binary file added bin/classes.dex
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added bin/classes/com/biancity/aes256test/R$attr.class
Binary file not shown.
Binary file added bin/classes/com/biancity/aes256test/R$dimen.class
Binary file not shown.
Binary file added bin/classes/com/biancity/aes256test/R$drawable.class
Binary file not shown.
Binary file added bin/classes/com/biancity/aes256test/R$id.class
Binary file not shown.
Binary file added bin/classes/com/biancity/aes256test/R$layout.class
Binary file not shown.
Binary file added bin/classes/com/biancity/aes256test/R$string.class
Binary file not shown.
Binary file added bin/classes/com/biancity/aes256test/R$style.class
Binary file not shown.
Binary file added bin/classes/com/biancity/aes256test/R.class
Binary file not shown.
Binary file not shown.
Binary file added bin/res/crunch/drawable-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/res/crunch/drawable-mdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/res/crunch/drawable-xhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/res/crunch/drawable-xxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/resources.ap_
Binary file not shown.
6 changes: 6 additions & 0 deletions gen/com/biancity/aes256test/BuildConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** Automatically generated file. DO NOT MODIFY */
package com.biancity.aes256test;

public final class BuildConfig {
public final static boolean DEBUG = true;
}
57 changes: 57 additions & 0 deletions gen/com/biancity/aes256test/R.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/

package com.biancity.aes256test;

public final class R {
public static final class attr {
}
public static final class dimen {
/** Default screen margins, per the Android Design guidelines.
Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively).
*/
public static final int activity_horizontal_margin=0x7f040000;
public static final int activity_vertical_margin=0x7f040001;
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int text1=0x7f070000;
public static final int text2=0x7f070001;
public static final int text3=0x7f070002;
}
public static final class layout {
public static final int activity_main=0x7f030000;
}
public static final class string {
public static final int action_settings=0x7f050002;
public static final int app_name=0x7f050000;
public static final int hello_world=0x7f050001;
}
public static final class style {
/**
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
*/
public static final int AppBaseTheme=0x7f060000;
/** Application theme.
All customizations that are NOT specific to a particular API-level can go here.
*/
public static final int AppTheme=0x7f060001;
}
}
Binary file added ic_launcher-web.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions jni/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_C_INCLUDES := $(LOCAL_PATH)/include

LOCAL_MODULE := EncryptMsg

LOCAL_SRC_FILES := \
EncryptMsg.c \
aes256.c \
base64.c \
encode.c

LOCAL_LDLIBS := -llog

include $(BUILD_SHARED_LIBRARY)
1 change: 1 addition & 0 deletions jni/Application.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
APP_CFLAGS += -Wno-error=format-security
197 changes: 197 additions & 0 deletions jni/EncryptMsg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/**
* `EncryptMsg.c'
*
* copyright (c) 2015 https://github.com/Mozhuowen
*/
#include "aes256.h"
#include "base64.h"
#include "b64.h"

#ifndef LOGE
#define LOGE(...){\
if (1) {\
__android_log_print(ANDROID_LOG_ERROR,"jni---get---key",__VA_ARGS__);\
}\
}
#endif

jstring getImportInfo(JNIEnv *, jstring,jint);
jstring getDecrypt(JNIEnv *,jstring);
jstring charToJstring(JNIEnv* envPtr, char *src);
jbyteArray charToJbyteArray(JNIEnv* env,char *src,int bufflen);

JNIEXPORT
jbyteArray JNICALL Java_com_biancity_aes256test_EncryptUtil_getImportantInfoByJNI(
JNIEnv *env, jobject thiz, jstring mingwen,jint ji) {

return getImportInfo(env, mingwen,ji);
}

jbyteArray getImportInfo(JNIEnv* envPtr, jstring mingwen,jint ji) {
// LOGE("enter getImportInfo method ok!");
if (ji == 1)
return getDecrypt(envPtr, mingwen);

JNIEnv env = *envPtr;
const int BLOCK_SIZE = 32;

//encrypt key
unsigned char key[32] = {0x6D,0xA7,0x79,0x1D,0xF8,0x21,0x8A,0x39,0x25,0x9A,
0x7A,0x29,0xAA,0xBB,0x2D,0xBA,0xCF,0xFC,0x31,0x2D,0x74,0xDB,0x7E,0x39,
0xEB,0xBF,0x7C,0x10,0x04,0x00,0x83,0x61};

// LOGE(key);

//****************************************开始加密******************************************************
//1.初始化数据
//初始化向量
uint8_t iv[16] = {0x6D,0xA7,0x1D,0x79,0xF8,0x21,0x8A,0x39,0x25,0x9A,0x7A,0x29,0xAA,0xBB,0x2D,0xBA};

//初始化加密参数
aes256_context ctx;
aes256_init(&ctx, key);

//2.将jstring转为char
const char *mwChar = env->GetStringUTFChars(envPtr, mingwen, JNI_FALSE);

//3.分组填充加密
int i;
int mwSize = strlen(mwChar);
int remainder = mwSize % BLOCK_SIZE;
jbyteArray entryptString;
if (mwSize < BLOCK_SIZE) { //小于32字节,填充32字节,后面填充几个几
uint8_t input[BLOCK_SIZE];
for (i = 0; i < BLOCK_SIZE; i++) {
if (i < mwSize) {
input[i] = mwChar[i];
} else {
input[i] = BLOCK_SIZE - mwSize;
}
}
//加密
uint8_t output[BLOCK_SIZE];
aes256_encrypt_cbc(&ctx, input, iv, output);
//base64加密后然后jstring格式输出
char *enc = base64_encode(output, sizeof(output));
// entryptString = charToJstring(envPtr, enc);
entryptString = charToJbyteArray(envPtr,enc,strlen(enc));

free(enc);
} else { //如果是32的倍数,填充32字节,后面填充0x20
int group = mwSize / BLOCK_SIZE;
int size = BLOCK_SIZE * (group + 1);
uint8_t input[size];
for (i = 0; i < size; i++) {
if (i < mwSize) {
input[i] = mwChar[i];
} else {
if (remainder == 0) {
input[i] = 0x20;
} else { //如果不足32位 少多少位就补几个几 如:少4为就补4个4 以此类推
int dif = BLOCK_SIZE - mwSize;
input[i] = dif;
}
}
}
//加密
uint8_t output[size];
aes256_encrypt_cbc(&ctx, input, iv, output);
//base64加密后然后jstring格式输出
char *enc = base64_encode(output, sizeof(output));
// entryptString = charToJstring(envPtr, enc);
entryptString = charToJbyteArray(envPtr,enc,strlen(enc));

free(enc);
}

//释放mwChar
env->ReleaseStringUTFChars(envPtr, mingwen, mwChar);

return entryptString;
}

jbyteArray getDecrypt(JNIEnv *envPtr,jstring encryptstr)
{
// LOGE("enter getDecrypt ok!");
JNIEnv env = *envPtr;

int i;
const int BLOCK_SIZE = 32;
unsigned char key[32] = {0x6D,0xA7,0x79,0x1D,0xF8,0x21,0x8A,0x39,0x25,0x9A,
0x7A,0x29,0xAA,0xBB,0x2D,0xBA,0xCF,0xFC,0x31,0x2D,0x74,0xDB,0x7E,0x39,
0xEB,0xBF,0x7C,0x10,0x04,0x00,0x83,0x61};

// LOGE(key);

//****************************************开始加密******************************************************
//1.初始化数据
//初始化向量
uint8_t iv[16] = {0x6D,0xA7,0x1D,0x79,0xF8,0x21,0x8A,0x39,0x25,0x9A,0x7A,0x29,0xAA,0xBB,0x2D,0xBA};

//初始化加密参数
aes256_context ctx;
aes256_init(&ctx, key);

//2.将jstring转为char
const char *encryptChar = env->GetStringUTFChars(envPtr, encryptstr, JNI_FALSE);
// LOGE("encryptChar lenght: %d",strlen(encryptChar));
basedata decodebase64 = base64_decode(encryptChar,strlen(encryptChar));
// LOGE("decodebase64info lenght: %d",decodebase64.len);
int targetstrlen = decodebase64.len;
// LOGE("decodebase64 ok!");
int len = decodebase64.len;
unsigned char resbuf[len];
int ii;
for (ii=0;ii<len;ii++) {
resbuf[ii] = *(decodebase64.data + ii);
}
// LOGE("resbuf lenght: %d",sizeof(resbuf));
// LOGE("resbuf first and last num is %X %X",resbuf[0],resbuf[len-1]);
//3.解密
uint8_t output[len];
aes256_decrypt_cbc(&ctx, resbuf, iv, output,len);
// LOGE("aes256_decrypt_cbc ok!");
// LOGE("output lenght: %d",sizeof(output));
// LOGE("output last num is %X",output[len-1]);
// return encryptstr;
//4.去除补位padding
uint8_t pad = output[len-1];
if (pad <1 || pad > 0x20)
pad = 0;
int reslen = len - pad;
uint8_t resultChar[reslen];
for (i=0;i<reslen;i++) {
resultChar[i] = output[i];
}
// LOGE("ripe padding ok!");
// LOGE("reslutChar lenght: %d",sizeof(resultChar));
//
// //释放mwChar
env->ReleaseStringUTFChars(envPtr, encryptstr,encryptChar);
free(decodebase64.data);

return charToJbyteArray(envPtr,resultChar,reslen);
}

jstring charToJstring(JNIEnv* envPtr, char src[]) {
JNIEnv env = *envPtr;

jsize len = strlen(src);
jclass clsstring = env->FindClass(envPtr, "java/lang/String");
jstring strencode = env->NewStringUTF(envPtr, "UTF-8");
jmethodID mid = env->GetMethodID(envPtr, clsstring, "<init>",
"([BLjava/lang/String;)V");
jbyteArray barr = env->NewByteArray(envPtr, len);
env->SetByteArrayRegion(envPtr, barr, 0, len, (jbyte*) src);

return (jstring) env->NewObject(envPtr, clsstring, mid, barr, strencode);
}
jbyteArray charToJbyteArray(JNIEnv* env,char *src,int buffer_size) {
int i;
unsigned char buff[buffer_size];
for (i=0;i<buffer_size;i++)
buff[i] = src[i];
jbyteArray array = (*env)->NewByteArray(env,buffer_size);
(*env)->SetByteArrayRegion(env,array,0,buffer_size,buff);
return array;
}
Loading

0 comments on commit 56bddfe

Please sign in to comment.