一、在主机上进行openssl编译,并生成动态库:
下载openssl安装包:
root@kwt-virtual-machine:/home/kwt# wget http://distfiles.macports.org/openssl/openssl-1.1.1e.tar.gz
并解压到当前文件夹:
root@kwt-virtual-machine:/home/kwt# tar vxf openssl-1.1.1e.tar.gz
进入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");
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:
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生成可执行文件。