前段时间公司使用国密开始保证数据安全,于是找到项目GmSSL,需要进行编译32位和64位的动态库。
1、我是在ubuntu下进行编译的。准备工作就是下载VMware 虚拟机,然后安装ubuntu系统。安装过程不做说明,百度一大堆。。。
2、在ubuntu下下载NDK,我下载的是r17c版本,使用命令:wget https://dl.google.com/android/repository/android-ndk-r17c-linux-x86_64.zip?hl=zh_cn,修改成.zip 使用命令 :mv android-ndk-r17c-linux-x86_64.zip?hl=zh_cn android-ndk-r17c-linux-
x86_64.zip ,解压命令: unzip android-ndk-r17c-linux-x86_64.zip
3、配置GCC环境变量:使用命令 sudo vim /etc/profile 打开页面在文件最后面追加如下内容:
ANDROID_NDK=/home/yanhu-xu/NDK_r17/android-ndk-r17c
export PATH=$PATH:$ANDROID_NDK
export NDK_GCC_arm64=$ANDROID_NDK/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-gcc
export NDK_CFIG_arm64="--sysroot=/home/yanhu-xu/NDK_r17/android-ndk-r17c/platforms/android-28/arch-arm64 -isystem /home/yanhu-xu/NDK_r17/android-ndk-r17c/sysroot/usr/include -isystem /home/yanhu-xu/NDK_r17/android-ndk-r17c/sysroot/usr/include/aarch64-linux-android"
export NDK_GCC_arm=$ANDROID_NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc
export NDK_CFIG_arm="--sysroot=/home/yanhu-xu/NDK_r17/android-ndk-r17c/platforms/android-28/arch-arm -isystem /home/yanhu-xu/NDK_r17/android-ndk-r17c/sysroot/usr/include -isystem /home/yanhu-xu/NDK_r17/android-ndk-r17c/sysroot/usr/include/arm-linux-androideabi"
配置了32位和64位的GCC环境。保存退出后执行命令:source /etc/profile 设置成功。查看命令:echo $NDK_GCC_arm会看到变量值在终端输出
4、下载GmSSL项目:使用命令:wget https://github.com/guanzhi/GmSSL/archive/master.zip
解压、进入目录:/home/yanhu-xu/gmssl-arm64/GmSSL-master
5、修改Configure文件和 test/build.info这两个文件
把
use if $^O ne "VMS", 'File::Glob' => qw/glob/;
改成
use if $^O ne "VMS", 'File::Glob' => qw/:glob/;
6、创建脚本文件gmssl.sh:
armv7a:32位
ANDROID_PATH=/home/yanhu-xu/NDK_r17/android-ndk-r17c
PLATFORM_VERSION=28
MAKE_TOOLCHAIN=$ANDROID_PATH/build/tools/make-standalone-toolchain.sh
export TOOLCHAIN_PATH=$ANDROID_PATH/toolchains
$MAKE_TOOLCHAIN --arch=arm --platform=android-$PLATFORM_VERSION --install-dir=$TOOLCHAIN_PATH
export MACHINE=armv7
export SYSTEM=android
export ARCH=arm
export TOOL_BASENAME=$TOOLCHAIN_PATH/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi
export CROSS_SYSROOT=/home/yanhu-xu/NDK_r17/android-ndk-r17c/platforms/android-28/arch-arm -isystem /home/yanhu-xu/NDK_r17/android-ndk-r17c/sysroot/usr/include -isystem /home/yanhu-xu/NDK_r17/android-ndk-r17c/sysroot/usr/include/arm-linux-androideabi
export CC=$TOOL_BASENAME-gcc
export CXX=$TOOL_BASENAME-g++
export LD=$TOOL_BASENAME-ld
export LINK=$CXX
export AR=$TOOL_BASENAME-ar
export RANLIB=$TOOL_BASENAME-ranlib
export STRIP=$TOOL_BASENAME-strip
./config --prefix=/home/yanhu-xu/build_libs
arm64:64位的
ANDROID_PATH=/home/yanhu-xu/NDK_r17/android-ndk-r17c
PLATFORM_VERSION=28
MAKE_TOOLCHAIN=$ANDROID_PATH/build/tools/make-standalone-toolchain.sh
export TOOLCHAIN_PATH=$ANDROID_PATH/toolchains
$MAKE_TOOLCHAIN --arch=aarch64 --platform=android-$PLATFORM_VERSION --install-dir=$TOOLCHAIN_PATH
export MACHINE=armv8
export SYSTEM=android-v8
export ARCH=aarch64
export CROSS_SYSROOT=/home/yanhu-xu/NDK_r17/android-ndk-r17c/platforms/android-28/arch-arm64 -isystem /home/yanhu-xu/NDK_r17/android-ndk-r17c/sysroot/usr/include -isystem /home/yanhu-xu/NDK_r17/android-ndk-r17c/sysroot/usr/include/aarch64-linux-android
export TOOL_BASENAME=$TOOLCHAIN_PATH/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android
export CC=$TOOL_BASENAME-gcc
export CXX=$TOOL_BASENAME-g++
export LD=$TOOL_BASENAME-ld
export LINK=$CXX
export AR=$TOOL_BASENAME-ar
export RANLIB=$TOOL_BASENAME-ranlib
export STRIP=$TOOL_BASENAME-strip
./config --prefix=/home/yanhu-xu/build_libs
选择需要的版本后执行:
sh gmssl.sh
make
执行完make后我们会发现在目录下生成了两个so文件
8、编译gmssljni.so
定位到java目录下:先执行命令 echo $NDK_GCC_arm64 查看下是否输出我们配置的值,如果没有 再执行一次命令:source /etc/profile
随后执行命令:
$NDK_GCC_arm64 $NDK_CFIG_arm64 -fPIC -shared -Wall -I./jni -I /home/yanhu-xu/gmssl-arm64/GmSSL-master/include -L /home/yanhu-xu/gmssl-arm64/GmSSL-master GmSSL.c -lcrypto -o libgmssljni.so
到此编译库生成好了。
9、将编译好的libcrypto.so和libssl.so、libgmssljni.so拷贝到项目中去,并且在main下创建一个org.gmssl的package,然后将GmSSL.java拷贝到项目中去。代码如下:
package org.gmssl;
public class GmSSL {
public native String[] getVersions();
public native String[] getCiphers();
public native String[] getDigests();
public native String[] getMacs();
public native String[] getSignAlgorithms();
public native String[] getPublicKeyEncryptions();
public native String[] getDeriveKeyAlgorithms();
public native byte[] generateRandom(int length);
public native int getCipherIVLength(String cipher);
public native int getCipherKeyLength(String cipher);
public native int getCipherBlockSize(String cipher);
public native byte[] symmetricEncrypt(String cipher, byte[] in, byte[] key, byte[] iv);
public native byte[] symmetricDecrypt(String cipher, byte[] in, byte[] key, byte[] iv);
public native int getDigestLength(String digest);
public native int getDigestBlockSize(String digest);
public native byte[] digest(String algor, byte[] data);
public native String[] getMacLength(String algor);
public native byte[] mac(String algor, byte[] data, byte[] key);
public native byte[] sign(String algor, byte[] data, byte[] privateKey);
public native int verify(String algor, byte[] digest, byte[] signature, byte[] publicKey);
public native byte[] publicKeyEncrypt(String algor, byte[] in, byte[] publicKey);
public native byte[] publicKeyDecrypt(String algor, byte[] in, byte[] privateKey);
public native byte[] deriveKey(String algor, int keyLength, byte[] peerPublicKey, byte[] privateKey);
public native String[] getErrorStrings();
static {
System.loadLibrary("crypto");
System.loadLibrary("ssl");
System.loadLibrary("gmssljni");
}
}