c++暗号化と復号アルゴリズムおよびJNI技術の応用例を実現する
10073 ワード
#include "jiami.h"
#include "jni.h"
#include "com_test_start_CommonClassLoader.h"
static void arraycopy(JNIEnv * env, jbyteArray sb, int spo, jbyteArray db,
int start, int len);
static jbyteArray encrypt(JNIEnv * env, jbyteArray b, jint len);
static jbyteArray getValidateCode(JNIEnv * env);
static jbyteArray getCode(JNIEnv * env);
/* * Clasbs: com_test_start_CommonClassLoader
* Method: defineClass0
* Signature: (Ljava/lang/String;[BII)Ljava/lang/Class;
*/
JNIEXPORT jclass JNICALL Java_com_test_start_CommonClassLoader_defineClass0(
JNIEnv * env, jobject loader, jstring name, jbyteArray buffer,
jint start, jint len) {
jbyteArray temp = env - > NewByteArray(len); //new ,
arraycopy(env, buffer, start, temp, start, len); // System.copy()
jbyteArray byte0 = encrypt(env, temp, len); // class
if (byte0 == NULL) {
env - > DeleteLocalRef(temp); //
return NULL;
}
jsize size = env - > GetArrayLength(byte0); // Array length
jclass classLoader = env - >
GetSuperclass(env - >
GetSuperclass(env - >
GetSuperclass(env - > GetObjectClass(loader)))); //
jmethodID mid = env - >
GetMethodID(classLoader, "defineClass",
"(Ljava/lang/String;[BII)Ljava/lang/Class;"); // defineClass
defineClass jclass cls = (jclass) env - >
CallObjectMethod(loader, mid, name, byte0, start,
size); // Classloader defineClass jvm
env - > DeleteLocalRef(byte0); //
return cls;
}
/*
* Class: com_test_start_CommonClassLoader
* Method: defineClass1
* Signature: (Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;
*/
JNIEXPORT jclass JNICALL Java_com_test_start_CommonClassLoader_defineClass1(
JNIEnv * env, jobject loader, jstring name, jbyteArray buffer,
jint start, jint len, jobject pro) {
jbyteArray temp = env - > NewByteArray(len);
arraycopy(env, buffer, start, temp, start, len);
jbyteArray byte0 = encrypt(env, temp, len);
if (byte0 == NULL) {
env - > DeleteLocalRef(temp);
return NULL;
}
jsize size = env - > GetArrayLength(byte0);
jclass classLoader = env - >
GetSuperclass(env - >
GetSuperclass(env - >
GetSuperclass(env - > GetObjectClass(loader))));
jmethodID mid = env - >
GetMethodID(classLoader, "defineClass",
"(Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;");
jclass cls = (jclass) env - >
CallObjectMethod(loader, mid, name, byte0, start, size, pro);
env - > DeleteLocalRef(byte0);
return cls;
}
/* getCode, , */
static jbyteArray getCode(JNIEnv * env) {
char char0[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
char char1[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 's',
'y', 'z'
};
char char2[36];
int i = 0;
int j = 0;
int k = 0;
while (i < 36) {
if (i >= 12 && j < 10) {
char2[i] = char0[j];
j++;
} else if (i >= 23 && k < 26) {
char2[i] = char1[k];
k++;
} else {
char2[i] = char1[k];
k++;
}
i++;
}
jbyteArray code = env - > NewByteArray(36);
env - > SetByteArrayRegion(code, 0, 36, (jbyte * ) char2);
return code;
}
/* getValidateCode, */
static jbyteArray getValidateCode(JNIEnv * env) {
char char0[] = {'0', '1', '2', '3', '4', '5', '6', '7'};
jbyteArray char1 = env - > NewByteArray(8);
env - > SetByteArrayRegion(char1, 0, 8, (jbyte * ) char0);
return char1;
}
/* encrypt, */
static jbyteArray encrypt(JNIEnv * env, jbyteArray b, jint len) {
int i = 0;
jint j = 0;
int k = len;
jbyte * bb = (env - > GetByteArrayElements(b, JNI_FALSE));
while (i < k) {
j = bb[i];
if ((j >= 48) && (j <= 57)) {
j = (((j - 48) + 5) % 10) + 48;
} else if ((j >= 65) && (j <= 90)) {
j = (((j - 65) + 13) % 26) + 65;
} else if ((j
>= 97) && (j <= 122)) {
j = (((j - 97) + 13) % 26) + 97;
}
bb[i] = (jbyte) j;
i++;
}
env - > SetByteArrayRegion(b, 0, k, bb);
int length = 500; //
int start = 0; //
jbyteArray temp = getCode(env); //
int mode = (k - 8) % (length + 36); //
int count = (k - 8) / (length + 36); //
int spo = 0; //
int dpo = 0; //
int size = count * length + mode; //
jbyteArray byte0 = env - > NewByteArray(size); //
if (count > 0) { //
while (start < count) {
arraycopy(env, b, spo, byte0, dpo, length);
spo = spo + length + 36;
dpo = dpo + length;
start++;
}
}
if (mode > 0) { //
arraycopy(env, b, spo, byte0, dpo, mode);
spo = spo + mode;
} //
jbyteArray validateCode0 = getValidateCode(env);
jbyte *
validateCode = env - >
GetByteArrayElements(validateCode0, JNI_FALSE);
jbyteArray validate0 = env - > NewByteArray(8);
arraycopy(env, b, spo, validate0, 0, 8);
jbyte * validate = env - > GetByteArrayElements(validate0, JNI_FALSE);
for (int index = 0; index < 8; index++) { //
if (validate[index] != validateCode[index]) {
return NULL;
}
}
env - > DeleteLocalRef(validate0);
env - > DeleteLocalRef(validateCode0);
env - > DeleteLocalRef(temp);
return byte0;
}
/* decrypt, */
static jbyteArray decrypt(JNIEnv * env, jbyteArray b, jboolean end) {
int length = 500; //
int start = 0; //
int count = env - > GetArrayLength(b) / length; //
jbyteArray temp = getCode(env); //
int spo = 0; //
int dpo = 0; //
int mode = env - > GetArrayLength(b) % length; //
int size = count * (length + 36) + mode; //
if (end == JNI_TRUE) { //
size = size + 8;
}
jbyteArray byte0 = env - > NewByteArray(size); //
if (count > 0) { //
while (start < count) {
arraycopy(env, b, spo, byte0, dpo, length);
arraycopy(env, temp, 0, byte0, dpo + length, 36);
spo = spo + length;
dpo = dpo + length + 36;
start++;
}
}
if (mode > 0) { //
arraycopy(env, b, spo, byte0, dpo, mode);
dpo = dpo + mode;
}
if (end == JNI_TRUE) { //
jbyteArray validateCode = getValidateCode(env);
arraycopy(env, validateCode, 0, byte0, dpo, 8);
env - > DeleteLocalRef(validateCode);
}
jbyte * byte1 = env - > GetByteArrayElements(byte0, 0); //
int i = 0;
int j = 0;
int k = size;
while (i < k) {
j = byte1[i];
if ((j >= 48) && (j <= 57)) {
j = (((j - 48) + 5) % 10) + 48;
} else if ((j >= 65) && (j <= 90)) {
j = (((j - 65) + 13) % 26) + 65;
} else if ((j >= 97) && (j <= 122)) {
j = (((j - 97) + 13) % 26) + 97;
}
byte1[i] = (jbyte) j;
i++;
}
env - > SetByteArrayRegion(byte0, 0, size, byte1);
env - > DeleteLocalRef(temp);
return byte0;
}
/* arraycopy, System.copy() */
static void arraycopy(JNIEnv * env, jbyteArray sb, int spo, jbyteArray db,
int start, int len) {
jbyte * t = new jbyte[len];
env - > GetByteArrayRegion(sb, spo, len, t);
env - > SetByteArrayRegion(db, start, len, t);
delete t;
}
参照先:http://blog.csdn.net/chenshuang_com/article/details/7681670
カスタムClassLoaderを使用してj 2 eeプログラムを保護
http://www.ibm.com/developerworks/cn/java/l-protectjava/