Target: Many Android phones already have their own OpenSSL library, libssl.so, libcrypto.so, the following example implements the APK call to the OpenSSL library function Pkcs5_pbkdf2_hmac (key generation algorithm) through the NDK
1) Create a JNI directory in Android project
2) Edit Abcjni.java
Package com.example;
public class Abcjni
{
Public native byte[] HashKey (byte[] key, byte[] salt, int count);
static {
System.loadlibrary ("Abc-jni");
}
}
3) Compile successfully (generate Abcjni.java, e.g. in Bin directory), build with Abcjni.class
Javah-classpath bin/classes-d JNI Com.example.AbcJni
This will generate a com_example_abcjni.h file in the JNI directory
4) Create a new Abc-jni.c file to implement the above header file functions
#include <string.h>
#include <jni.h>
#include <openssl/evp.h>
#include <openssl/sha.h>
Jniexport Jbytearray Jnicall Java_com_example_abcjni_hashkey
(JNIEnv *env, Jobject thiz, Jbytearray Pass, Jbytearray Salt, jint count)
{
jbyte* Pjbytepass = (*env)->getbytearrayelements (env, pass, NULL);
char* Szbytepass = (char *) Pjbytepass;
int ilenpass = (*env)->getarraylength (env, pass);
jbyte* Pjbytesalt = (*env)->getbytearrayelements (env, salt, NULL);
char* Szbytesalt = (char *) Pjbytesalt;
int ilensalt = (*env)->getarraylength (env, salt);
int outsize = 64;
Char buf[64];
memset (buf, 0, sizeof (BUF));
Pkcs5_pbkdf2_hmac (
Szbytepass,
Ilenpass,
Szbytesalt,
Ilensalt,
Count, evp_sha512 (), outsize, buf);
Jbytearray Jarray = (*env)->newbytearray (env, outsize);
(*env)->setbytearrayregion (env, Jarray, 0, OUTSIZE,BUF);
(*env)->releasebytearrayelements (env, pass, Pjbytepass, 0);
(*env)->releasebytearrayelements (env, salt, pjbytesalt, 0);
return jarray;
}
4) Edit android.mk, file, content as follows: Local_path: = $ (call My-dir)
Include $ (clear_vars)
Local_module: = Abc-jni
Local_src_files: = Abc-jni.c
Local_ldlibs: =-lcrypto-lssl
Include $ (build_shared_library)
5) Download one of the latest OpenSSL source code, unzip. If the directory is/my/src/openssl-1.0.1k/after decompression
Note: Android SDK/NDK directory does not have OpenSSL related header files and library files, so we need to get header files from the source code
6) Enter the following directory in the NDK (I use Android 4.4, platform 19) to create a soft link to the header file directory
Cd/opt/android-ndk/platforms/android-19/arch-arm/usr/include
Ln-s/my/src/openssl-1.0.1k/include/openssl.
7) Copy the following files from your phone (in/system/lib, Root oh) into the NDK's lib directory:
For example, my phone is as follows:
[my]# pwd
/system/lib
[my]# Ls-la libcrypto.so libssl.so
-rw-r--r--root root 1249844 2013-02-09 03:36 libcrypto.so
-rw-r--r--root root 224784 2013-02-09 03:36 libssl.so
The NDK directory is:
/opt/android-ndk/platforms/android-19/arch-arm/usr/lib
8) Back to your Android project
CD JNI
Ndk-build
If you succeed, you will find that. /libs Directory has a subdirectory, and libabc-jni.so file
9) Finally you can call the following methods in Abcjni.java in your Android Java code:
Byte[] HashKey (byte[] key, byte[] salt, int count);
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Call the OpenSSL library function that comes with your Android phone with the NDK