[mw_shl_code=cpp,true]#include <jni.h>
#include "com_qihoo_firsttest_Res.h"
/**
* @author 花墨
* 2015年1月6日
*
http://www.cnhuamo.net
*/
JNIEXPORT jbyteArray JNICALL Java_com_qihoo_firsttest_Res_decrypt
(JNIEnv *env, jobject obj, jbyteArray src, jstring password){
jclass clazz_random = env->FindClass("java/security/SecureRandom");
jmethodID jm_random = env->GetMethodID(clazz_random,"<init>","()V");
jobject jo_random = env->NewObject(clazz_random,jm_random);
jclass clazz_String = env->FindClass("java/lang/String");
jmethodID jm_string = env->GetMethodID(clazz_String,"getBytes","()[B");
jbyteArray bytearray = (jbyteArray)env->CallObjectMethod(password,jm_string);
jclass jc_DESKeySpec = env->FindClass("javax/crypto/spec/DESKeySpec");
jmethodID jm_DESKeySpec = env->GetMethodID(jc_DESKeySpec,"<init>","([B)V");
jobject jo_DESKeySpec = env->NewObject(jc_DESKeySpec,jm_DESKeySpec,bytearray);
jclass jc_SecretKeyFactory = env->FindClass("javax/crypto/SecretKeyFactory");
jmethodID jm_SecretKeyFactory = env->GetStaticMethodID(jc_SecretKeyFactory,"getInstance","(Ljava/lang/String;)Ljavax/crypto/SecretKeyFactory;");
jobject jo_getInstance = env->CallStaticObjectMethod(jc_SecretKeyFactory,jm_SecretKeyFactory,env->NewStringUTF("DES"));
jmethodID jm_SecretKey = env->GetMethodID(jc_SecretKeyFactory,"generateSecret","(Ljava/security/spec/KeySpec;)Ljavax/crypto/SecretKey;");
jobject jo_SecretKey = env->CallObjectMethod(jo_getInstance,jm_SecretKey,jo_DESKeySpec);
jclass jc_Cipher = env->FindClass("javax/crypto/Cipher");
jmethodID jm_Cipher_getInstance = env->GetStaticMethodID(jc_Cipher,"getInstance","(Ljava/lang/String;)Ljavax/crypto/Cipher;");
jobject jo_Cipher = env->CallStaticObjectMethod(jc_Cipher,jm_Cipher_getInstance,env->NewStringUTF("DES"));
jmethodID jm_init = env->GetMethodID(jc_Cipher,"init","(ILjava/security/Key;Ljava/security/SecureRandom;)V");
env->CallVoidMethod(jo_Cipher,jm_init,2,jo_SecretKey,jo_random);
jmethodID jm_doFinal = env->GetMethodID(jc_Cipher,"doFinal","([B)[B");
jbyteArray a = (jbyteArray)env->CallObjectMethod(jo_Cipher,jm_doFinal,src);
return a;
}[/mw_shl_code]