Linux Kernel(Android) 密碼編譯演算法總結(四)-應用程式調用OpenSSL密碼編譯演算法

來源:互聯網
上載者:User

標籤:加密   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密碼編譯演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.