標籤:加密 aes openssl c++ sha1
Linux Kernel(Android) 密碼編譯演算法總結(三)-應用程式調用核心密碼編譯演算法介面
講到了如何調用核心中的介面的方法。
本節主要是介紹如何Android C/C++應用程式調用Openssl的AES密碼編譯演算法。
crypt_ssl.c
#include <stdio.h>#include <string.h>#include <unistd.h>#include <fcntl.h>#include <aes.h>#include <sys/socket.h>#include <linux/if_alg.h>#include <evp.h>#include <stdio.h>#include <stdlib.h>#ifndef AF_ALG #define AF_ALG 38 #define SOL_ALG 279#endif#define BUF_SIZE 16static void crypt_ssl(char *in, int inlen, char *out,const char *key, char *iv){ AES_KEY akey; AES_set_encrypt_key(key, 128, &akey); AES_cbc_encrypt(in, out, inlen, &akey, iv, 1);}int main(int argc, char **argv){ int i; char out[BUF_SIZE] = {0}; char in[BUF_SIZE] = "Single block msg"; const char key[16] = "x06xa9x21x40x36xb8xa1x5b" "x51x2ex03xd5x34x12x00x06"; char iv[16] = "x3dxafxbax42x9dx9exb4x30" "xb4x22xdax80x2cx9fxacx41"; if(argc != 2) { printf("usage:compare openssl / compare kernel \n"); } if(strncmp(argv[1], "openssl", 7) == 0) { printf("encrypt by openssl...n"); crypt_ssl(in, BUF_SIZE, out, key, iv); } for (i = 0; i < BUF_SIZE; i ) printf("0x", (unsigned char)out[i]); printf("n"); return 0;}
在Android 原始碼編譯檔案:
Android.mk
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := myencryptLOCAL_MODULE_TAGS := optionalLOCAL_SRC_FILES := crypt_ssl.cLOCAL_SHARED_LIBRARIES := libdl liblog libcryptoLOCAL_C_INCLUDES := external/openssl/include/openssl external/openssl/includeinclude $(BUILD_EXECUTABLE)
編譯完成後在
adb push testhash /system/bin/
adb shell chmod a+x /system/bin/testhash
adb shell testhash
驗證輸出結果.
Linux Kernel(Android) 密碼編譯演算法總結(四)-應用程式調用OpenSSL密碼編譯演算法