日本a√视频在线,久久青青亚洲国产,亚洲一区欧美二区,免费g片在线观看网站

        <style id="k3y6c"><u id="k3y6c"></u></style>
        <s id="k3y6c"></s>
        <mark id="k3y6c"></mark>
          
          

          <mark id="k3y6c"></mark>

          "); //-->

          博客專欄

          EEPW首頁 > 博客 > openssl動態(tài)庫生成以及交叉編譯

          openssl動態(tài)庫生成以及交叉編譯

          發(fā)布人:電子禪石 時間:2020-01-16 來源:工程師 發(fā)布文章
          虛擬機環(huán)境
          ubuntu12.04
          開發(fā)板
          EasyARM-i.MX280A:   64m  sdram  128M  nandflash   運行官方提供的Linux-2.6.35.3內(nèi)核linux

          首先說一下如何在主機上進行編譯,并生成動態(tài)庫
          在https://www.openssl.org/source/下載最新版的openssl,我下載的是
          openssl-1.1.0c.tar.gz版本
          拷貝到虛擬機中,找地方解壓,
          然后是經(jīng)典三部曲,首先使用 ./config   make   make install
          首先使用指令./config shared --prefix=/home/linux/opt/openssl --openssldir=/home/linux/opt/openssl/ssl
          prefix 是安裝目錄,openssldir 是配置文件目錄,shared 作用是生成動態(tài)連接庫。
          然后就是make
          make install

          這其中可能需要將生成的makefile 中 -m64注釋掉。

          7.png全部完成之后在安裝目錄下會有l(wèi)ib文件夾,里面有我們需要的動態(tài)庫和靜態(tài)庫文件
          libcrypto.a  libcrypto.so  libcrypto.so.1.1  libssl.a  libssl.so  libssl.so.1.1
          然后我們跳轉(zhuǎn)到/home/linux/opt/openssl/lib目錄下,將動態(tài)庫拷貝到系統(tǒng)庫目錄中/lib中
          sudo cp -a libcrypto.so* libssl.so* /lib
          大功告成,我們寫點程序測試一下,我們寫一個使用rc4加解密的程序測試一下
          cryptotest.h

          #ifndef _CRYPTOTEST_H_
          #define _CRYPTOTEST_H_
           
           
          typedef enum {
           GENERAL = 0,
           ECB,
           CBC,
           CFB,
           OFB,
           TRIPLE_ECB,
           TRIPLE_CBC
          }CRYPTO_MODE;
           
          //string DES_Encrypt(const string cleartext, const string key, CRYPTO_MODE mode);
          //string DES_Decrypt(const string ciphertext, const string key, CRYPTO_MODE mode);
           
          char * RC4_Encrypt(const char *cleartext, const char * key, int cleartextlen, int keylen);
          char * RC4_Decrypt(const char * ciphertext, const char * key, int cleartextlen, int keylen);
           
          #endif //_CRYPTOTEST_H_

          openssltest.c

          #include "cryptotest.h"
          #include <string.h>
          #include <stdio.h>
           
          int main()
          {
           char cleartext[] = "中國北京12345$abcde%ABCDE@?。?!";
           char *ciphertext;
           char key[] = "beijingchina1234567890ABCDEFGH!!!";
           
           ciphertext = RC4_Encrypt(cleartext, key, strlen(cleartext), strlen(key));
           char * decrypt = RC4_Decrypt(ciphertext, key, strlen(cleartext), strlen(key));
           
           printf("cleartext:%s\n", cleartext);
           printf("genarate ciphertext:%s\n", ciphertext);
           printf("src ciphertext:%s\n", ciphertext);
           printf("genarate ciphertext:%s\n", decrypt);
           
           if (strcmp(cleartext, decrypt) == 0)
            printf("RC4 crypto ok!!!\n");
           else
            printf("RC4 crypto error!!!\n");
           return 0;
          }

          rc4test.c

          #include <stdlib.h>
          #include <openssl/rc4.h>
          #include <string.h>
          #include "cryptotest.h"
           
          char * RC4_Encrypt(const char *cleartext, const char * key, int cleartextlen, int keylen)
          {
           RC4_KEY rc4key;
           char* tmp = malloc(cleartextlen + 1);
           memset(tmp, 0, cleartextlen + 1);
           
           RC4_set_key(&rc4key, keylen, (const unsigned char*)key);
           RC4(&rc4key, cleartextlen, (const unsigned char*)cleartext, tmp);
           
           return tmp;
          }
           
          char * RC4_Decrypt(const char * ciphertext, const char * key, int cleartextlen, int keylen)
          {
           RC4_KEY rc4key;
           unsigned char* tmp = malloc(cleartextlen + 1);
           memset(tmp, 0, cleartextlen + 1);
           
           RC4_set_key(&rc4key, keylen, (const unsigned char*)key);
           RC4(&rc4key, cleartextlen, (const unsigned char*)ciphertext, tmp);
           
           return tmp;
          }

          makefile
          #####################################################################
          ## file        : test makefile for build current dir .c   ##
          ## author      : jernymy                                  ##
          ## date-time   : 05/06/2010                               ##
          #####################################################################

          CC      = gcc
          CPP     = g++
          RM      = rm -rf
          ## debug flag
          DBG_ENABLE   = 0
          ## source file path
          SRC_PATH   := .
          ## target exec file name
          TARGET     := openssltest
          ## get all source files
          SRCS         += $(wildcard $(SRC_PATH)/*.c)
          ## all .o based on all .c
          OBJS        := $(SRCS:.c=.o)

          ## need libs, add at here
          LIBS := ssl crypto
          ## used headers  file path
          INCLUDE_PATH := /home/linux/opt/openssl/include/
          ## used include librarys file path
          LIBRARY_PATH := /home/linux/opt/openssl/lib/
          ## debug for debug info, when use gdb to debug
          ifeq (1, ${DBG_ENABLE})
           CFLAGS += -D_DEBUG -O0 -g -DDEBUG=1
          endif
          ## get all include path
          CFLAGS  += $(foreach dir, $(INCLUDE_PATH), -I$(dir))
          ## get all library path
          LDFLAGS += $(foreach lib, $(LIBRARY_PATH), -L$(lib))
          ## get all librarys
          LDFLAGS += $(foreach lib, $(LIBS), -l$(lib))

          all: clean build
          build:
           $(CC) -c $(CFLAGS) $(SRCS)
           $(CC) $(CFLAGS) -o $(TARGET) $(OBJS) $(LDFLAGS)
           $(RM) $(OBJS)

          clean:
           $(RM) $(OBJS) $(TARGET)

          準(zhǔn)備好這幾個文件,然后就可以make了,會生成openssltest可執(zhí)行文件,我們執(zhí)行以下這個文件會有輸出。


          linux@ubuntu:~/work/opensslDemo/rc4test$ ./openssltest
          cleartext:中國北京12345$abcde%ABCDE@?。?!
          genarate ciphertext:Zu?)?0Xv??????
          src ciphertext:Zu?)?0Xv??????
          genarate ciphertext:中國北京12345$abcde%ABCDE@?。。?br />RC4 crypto ok!!!


          下面我們要準(zhǔn)備開始交叉編譯
          在openssl解壓目錄下,使用config命令
          CC=arm-linux-gcc 可以使用export 命令。

          ./config no-asm shared --prefix=/home/linux/arm/openssl --openssldir=/home/linux/arm/openssl/ssl

          生成了Makefile
          然后就是make和make install
          之后會在安裝目錄下生成lib文件
          跳轉(zhuǎn)到lib目錄下linux@ubuntu:~$ cd arm/openssl/lib/
          復(fù)制動態(tài)庫文件到開發(fā)板中,因為我是用的nfs文件系統(tǒng),所以復(fù)制到了nfs文件系統(tǒng)下
          linux@ubuntu:~/arm/openssl/lib$ cp -a libcrypto.so* libssl.so* /nfsroot/rootfs/lib/


          然后修改剛剛的代碼中的Makefile文件

          #####################################################################
          ## file        : test makefile for build current dir .c   ##
          ## author      : jernymy                                  ##
          ## date-time   : 05/06/2010                               ##
          #####################################################################
           
          CC      = arm-linux-gcc
          CPP     = g++
          RM      = rm -rf
           
          ## debug flag
          DBG_ENABLE   = 0
           
          ## source file path
          SRC_PATH   := .
           
          ## target exec file name
          TARGET     := openssltest-arm
           
          ## get all source files
          SRCS         += $(wildcard $(SRC_PATH)/*.c)
          ## all .o based on all .c
          OBJS        := $(SRCS:.c=.o)
          ## need libs, add at here
          LIBS := ssl crypto
          ## used headers  file path
          INCLUDE_PATH := /home/linux/arm/openssl/include/
          ## used include librarys file path
          LIBRARY_PATH := /home/linux/arm/openssl/lib/
          ## debug for debug info, when use gdb to debug
          ifeq (1, ${DBG_ENABLE})
           CFLAGS += -D_DEBUG -O0 -g -DDEBUG=1
          endif
          ## get all include path
          CFLAGS  += $(foreach dir, $(INCLUDE_PATH), -I$(dir))
          ## get all library path
          LDFLAGS += $(foreach lib, $(LIBRARY_PATH), -L$(lib))
          ## get all librarys
          LDFLAGS += $(foreach lib, $(LIBS), -l$(lib))
          all: clean build
          build:
           $(CC) -c $(CFLAGS) $(SRCS)
           $(CC) $(CFLAGS) -o $(TARGET) $(OBJS) $(LDFLAGS)
           $(RM) $(OBJS)
          clean:
           $(RM) $(OBJS) $(TARGET)


          這樣用make編譯出來的文件就是針對開發(fā)板的可執(zhí)行文件openssltest-arm
          將可執(zhí)行文件拷貝到開發(fā)板中
          linux@ubuntu:~/work/opensslDemo/rc4test$ cp openssltest-arm /nfsroot/rootfs/root/
          在開發(fā)板中執(zhí)行openssltest-arm文件,效果和在電腦上的效果一樣。


          root@EasyARM-iMX28x ~# ./openssltest-arm
          cleartext:涓浗鍖椾含12345$abcde%ABCDE@錛侊紒錛
          genarate ciphertext:ZuXv冪徛栝
          src ciphertext:ZuXv冪徛栝
          genarate ciphertext:涓浗鍖椾含12345$abcde%ABCDE@錛侊紒錛
          RC4 crypto ok!!!

          開發(fā)板的速度較慢,所以執(zhí)行比電腦慢許多,下面我們來使用靜態(tài)庫編譯一下,看一下效果會不會好一些。
          其實我們在編譯openssl的時候動態(tài)庫和靜態(tài)庫已經(jīng)同時被編譯出來了,都存放在安轉(zhuǎn)目錄下的lib中。
          linux@ubuntu:~/arm/openssl/lib$ ls
          engines-1.1  libcrypto1.so  libcrypto.a  libcrypto.so.1.1  libssl1.so  libssl.a  libssl.so.1.1  pkgconfig
          其中.a結(jié)尾的文件就是動態(tài)庫。其實想要使用靜態(tài)庫編譯很簡單。
          在應(yīng)用程序需要連接外部庫的情況下,Linux默認(rèn)對庫的連接是使用動態(tài)庫,在找不到動態(tài)庫的情況下再選擇靜態(tài)庫。
          所以只要將lib文件夾中的.so文件刪除,系統(tǒng)在編譯的時候就會使用靜態(tài)庫編譯。
          仍然是make,然后拷貝到開發(fā)板中,運行,運行速度確實快了許多,說明靜態(tài)庫真的比動態(tài)庫效率高許多。
          ————————————————
          版權(quán)聲明:本文為CSDN博主「andylauren」的原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
          原文鏈接:https://blog.csdn.net/andylauren/article/details/53456340


          *博客內(nèi)容為網(wǎng)友個人發(fā)布,僅代表博主個人觀點,如有侵權(quán)請聯(lián)系工作人員刪除。



          關(guān)鍵詞:

          相關(guān)推薦

          技術(shù)專區(qū)

          關(guān)閉