编译和交叉编译openssl
⼀、在主机上进⾏openssl编译,并⽣成动态库:
下载openssl安装包:
root@kwt-virtual-machine:/home/kwt# wget /openssl/openssl-1.1.
并解压到当前⽂件夹:
root@kwt-virtual-machine:/home/kwt# tar vxf openssl-1.1.
进⼊openssl-1.1.1e⽬录:
root@kwt-virtual-machine:/home/kwt# cd openssl-1.1.1e
然后是经典三部曲( ./config make make install)
⾸先使⽤指令./config(PS:prefix 是安装⽬录,openssldir 是配置⽂件⽬录,shared 作⽤是⽣成动态连接库):
root@kwt-virtual-machine:/home/kwt/openssl-1.1.1e# ./config shared --prefix=/home/kwt/openssl --openssldir=/home/kwt/openssl/ssl 然后就是make和make install(PS: 若提⽰-m64错误,则在修改当前⽬录下的Makeifle,删除:-m64 编译选项):
root@kwt-virtual-machine:/home/kwt/openssl-1.1.1e# make
root@kwt-virtual-machine:/home/kwt/openssl-1.1.1e# make install
全部完成之后在安装⽬录/home/kwt/openssl下会有lib⽂件夹,⾥⾯有我们需要的动态库和静态库⽂件。然后我们跳转到/home/kwt/openssl/lib⽬录下,将动态库拷贝到系统库⽬录中/lib中:
root@kwt-virtual-machine:/home/kwt/openssl/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");
return0;
}
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);
cipher命令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:
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/kwt/openssl/include/
## used include librarys file path
LIBRARY_PATH := /home/kwt/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,⽣成openssltest可执⾏⽂件,我们执⾏⼀下这个⽂件会有输出:root@kwt-virtual-machine:/home/kwt/Test/2# make
root@kwt-virtual-machine:/home/kwt/Test/2# ./openssltest
cleartext:中国北京12345$abcde%ABCDE@
genarate ciphertext:Zu▒)▒0Xv▒▒▒▒▒▒
src ciphertext:Zu▒)▒0Xv▒▒▒▒▒▒
genarate ciphertext:中国北京12345$abcde%ABCDE@
RC4 crypto ok
root@kwt-virtual-machine:/home/kwt/Test/2#
⼆、openssl的交叉编译:
在openssl-1.1.1e解压⽬录下,使⽤config命令:
./config no-asm no-shared no-async --prefix=/home/kwt/openssl/aarch/openssl/--cross-compile-prefix=aarch64-linux-gnu-
⽣成了Makefile
然后make(PS: 若提⽰-m64错误,则在修改当前⽬录下的Makeifle,删除:-m64 编译选项)和make install:
root@kwt-virtual-machine:/home/kwt/openssl-1.1.1e# make
root@kwt-virtual-machine:/home/kwt/openssl-1.1.1e# make install
全部完成之后在安装⽬录/home/kwt/openssl/aarch/openssl下会有lib⽂件夹,⾥⾯有我们需要的动态
库和静态库⽂件。修改我们测试⽤的makefile中的部分内容如下:
CC      = aarch64-linux-gnu-gcc
CPP    = aarch64-linux-gnu-g++
INCLUDE_PATH := /home/kwt/openssl/aarch/openssl/include/
LIBRARY_PATH := /home/kwt/openssl/aarch/openssl/lib/
最后make⽣成可执⾏⽂件。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。