当前位置: 技术问答>linux和unix
有关openssl编程问题,请大家帮忙
来源: 互联网 发布时间:2016-04-02
本文导语: 最近要编写RSA加密算法,由于自己能力有限,想用openssl提供的大素数库,于是安了一个openssl。自己用的是ubuntu系统,按照wiki上的说明, 安了:sudo apt-get install libssl-dev libssl0.9.8 在网上抄了一段源代码(在下面给出)...
最近要编写RSA加密算法,由于自己能力有限,想用openssl提供的大素数库,于是安了一个openssl。自己用的是ubuntu系统,按照wiki上的说明,
安了:sudo apt-get install libssl-dev libssl0.9.8
在网上抄了一段源代码(在下面给出)
用:gcc test.c -0 test编译失败,提示:
test.c:(.text+0x12): undefined reference to `BN_new'
test.c:(.text+0x28): undefined reference to `BN_set_word'
test.c:(.text+0x41): undefined reference to `BN_add_word'
test.c:(.text+0x61): undefined reference to `BN_free'
test.c:(.text+0x75): undefined reference to `BN_free'
于是又从openssl官方网站上下载了源代码,在本机上编译安装,按照上INSTALL上的提示:
# ./config
# make
# make test
# make install
安装完成后,运行:
$ openssl version
显示:OpenSSL 0.9.8g 19 Oct 2007
运行了一些openssl命令:
openssl help
openssl enc -h
openssl ciphers -v
从结果看出都好用,所以似乎openssl已经成功安装上了,但用gcc test.c -o test编译源程序还是显示和上明一样的错误,这个问题我都泡了两天了,还是没能解决,于是在这里贴出详细的过程,希望大侠们给予指点,
以下是test.c中的代码:
#include "openssl/ssl.h"
#include "openssl/rsa.h"
#include "openssl/crypto.h"
#include "openssl/x509.h"
#include "openssl/pem.h"
#include "openssl/err.h"
#include "openssl/rand.h"
#include "openssl/bn.h"
#include
//能包含的头文件我都包上了
int main ()
{
int ret;
BIGNUM* a;
BN_ULONG w;
a = BN_new ();
BN_one (a);
w = 2685550010;
ret = BN_add_word (a, w);
if (ret != 1)
{
printf ("a += w error!n");
BN_free (a);
return -1;
}
BN_free (a);
return 0;
}
我用:gcc test.c -o test进行编译,不知是否是此处的错误,是否还应该带些选项?还是头文件的问题?抑或是openssl库没装好?希望大侠们给予指点!!!!!说一下可能性也行,谢了先!!!
安了:sudo apt-get install libssl-dev libssl0.9.8
在网上抄了一段源代码(在下面给出)
用:gcc test.c -0 test编译失败,提示:
test.c:(.text+0x12): undefined reference to `BN_new'
test.c:(.text+0x28): undefined reference to `BN_set_word'
test.c:(.text+0x41): undefined reference to `BN_add_word'
test.c:(.text+0x61): undefined reference to `BN_free'
test.c:(.text+0x75): undefined reference to `BN_free'
于是又从openssl官方网站上下载了源代码,在本机上编译安装,按照上INSTALL上的提示:
# ./config
# make
# make test
# make install
安装完成后,运行:
$ openssl version
显示:OpenSSL 0.9.8g 19 Oct 2007
运行了一些openssl命令:
openssl help
openssl enc -h
openssl ciphers -v
从结果看出都好用,所以似乎openssl已经成功安装上了,但用gcc test.c -o test编译源程序还是显示和上明一样的错误,这个问题我都泡了两天了,还是没能解决,于是在这里贴出详细的过程,希望大侠们给予指点,
以下是test.c中的代码:
#include "openssl/ssl.h"
#include "openssl/rsa.h"
#include "openssl/crypto.h"
#include "openssl/x509.h"
#include "openssl/pem.h"
#include "openssl/err.h"
#include "openssl/rand.h"
#include "openssl/bn.h"
#include
//能包含的头文件我都包上了
int main ()
{
int ret;
BIGNUM* a;
BN_ULONG w;
a = BN_new ();
BN_one (a);
w = 2685550010;
ret = BN_add_word (a, w);
if (ret != 1)
{
printf ("a += w error!n");
BN_free (a);
return -1;
}
BN_free (a);
return 0;
}
我用:gcc test.c -o test进行编译,不知是否是此处的错误,是否还应该带些选项?还是头文件的问题?抑或是openssl库没装好?希望大侠们给予指点!!!!!说一下可能性也行,谢了先!!!
|
没有提示你openssl/ssl.h头文件找不到么?
另外,编译的时候需要增加参数,具体设置要看你的openssl的安装路径。
-Lopenssl库文件所在路径 -lopenssl库文件名
还有,我查了下openssl的资料,发现你的头文件不太对。提供的头文件是:
[code=BatchFile]ssl.h
That's the common header file for the SSL/TLS API. Include it into your program to make the API of the ssl library available. It internally includes both more private SSL headers and headers from the crypto library. Whenever you need hard-core details on the internals of the SSL API, look inside this header file.
ssl2.h
That's the sub header file dealing with the SSLv2 protocol only. Usually you don't have to include it explicitly because it's already included by ssl.h.
ssl3.h
That's the sub header file dealing with the SSLv3 protocol only. Usually you don't have to include it explicitly because it's already included by ssl.h.
ssl23.h
That's the sub header file dealing with the combined use of the SSLv2 and SSLv3 protocols. Usually you don't have to include it explicitly because it's already included by ssl.h.
tls1.h
That's the sub header file dealing with the TLSv1 protocol only. Usually you don't have to include it explicitly because it's already included by ssl.h.
[/code]
另外,openssl提供的api当中也没有你贴出来的这些东西。请参考官方文档。
官方提供的api都是SSL_开头的函数,绝对不存在BN开头的API。
楼主另想办法吧。
另外,编译的时候需要增加参数,具体设置要看你的openssl的安装路径。
-Lopenssl库文件所在路径 -lopenssl库文件名
还有,我查了下openssl的资料,发现你的头文件不太对。提供的头文件是:
[code=BatchFile]ssl.h
That's the common header file for the SSL/TLS API. Include it into your program to make the API of the ssl library available. It internally includes both more private SSL headers and headers from the crypto library. Whenever you need hard-core details on the internals of the SSL API, look inside this header file.
ssl2.h
That's the sub header file dealing with the SSLv2 protocol only. Usually you don't have to include it explicitly because it's already included by ssl.h.
ssl3.h
That's the sub header file dealing with the SSLv3 protocol only. Usually you don't have to include it explicitly because it's already included by ssl.h.
ssl23.h
That's the sub header file dealing with the combined use of the SSLv2 and SSLv3 protocols. Usually you don't have to include it explicitly because it's already included by ssl.h.
tls1.h
That's the sub header file dealing with the TLSv1 protocol only. Usually you don't have to include it explicitly because it's already included by ssl.h.
[/code]
另外,openssl提供的api当中也没有你贴出来的这些东西。请参考官方文档。
官方提供的api都是SSL_开头的函数,绝对不存在BN开头的API。
楼主另想办法吧。
|
正解,看error message是没有连接library
|
gcc test.c -0 test -lcrypto -ssl
|
是OpenSSL cryptographic library。
编译加-lcrypto选项。
编译加-lcrypto选项。
|
加-lssl
|
OpenSSL的加解密算法示例:
#include
#include
int main()
{
RSA *r;
int bits=1024,ret,len,flen,padding,i;
unsigned long e=RSA_3;
BIGNUM *bne;
unsigned char*key,*p;
BIO *b;
unsigned charfrom[500],to[500],out[500];
bne=BN_new();
ret=BN_set_word(bne,e);
r=RSA_new();
ret=RSA_generate_key_ex(r,bits,bne,NULL);
if(ret!=1)
{
printf("RSA_generate_key_ex err!n");
return -1;
}
/* 私钥i2d */
b=BIO_new(BIO_s_mem());
ret=i2d_RSAPrivateKey_bio(b,r);
key=malloc(1024);
len=BIO_read(b,key,1024);
BIO_free(b);
b=BIO_new_file("rsa.key","w");
ret=i2d_RSAPrivateKey_bio(b,r);
BIO_free(b);
/* 私钥d2i */
/* 公钥i2d */
/* 公钥d2i */
/* 私钥加密 */
flen=RSA_size(r);
printf("please select private enc padding : n");
printf("1.RSA_PKCS1_PADDINGn");
printf("3.RSA_NO_PADDINGn");
printf("5.RSA_X931_PADDINGn");
scanf("%d",&padding);
if(padding==RSA_PKCS1_PADDING)
flen-=11;
else if(padding==RSA_X931_PADDING)
flen-=2;
else if(padding==RSA_NO_PADDING)
flen=flen;
else
{
printf("rsa not surport !n");
return -1;
}
for(i=0;i
#include
#include
int main()
{
RSA *r;
int bits=1024,ret,len,flen,padding,i;
unsigned long e=RSA_3;
BIGNUM *bne;
unsigned char*key,*p;
BIO *b;
unsigned charfrom[500],to[500],out[500];
bne=BN_new();
ret=BN_set_word(bne,e);
r=RSA_new();
ret=RSA_generate_key_ex(r,bits,bne,NULL);
if(ret!=1)
{
printf("RSA_generate_key_ex err!n");
return -1;
}
/* 私钥i2d */
b=BIO_new(BIO_s_mem());
ret=i2d_RSAPrivateKey_bio(b,r);
key=malloc(1024);
len=BIO_read(b,key,1024);
BIO_free(b);
b=BIO_new_file("rsa.key","w");
ret=i2d_RSAPrivateKey_bio(b,r);
BIO_free(b);
/* 私钥d2i */
/* 公钥i2d */
/* 公钥d2i */
/* 私钥加密 */
flen=RSA_size(r);
printf("please select private enc padding : n");
printf("1.RSA_PKCS1_PADDINGn");
printf("3.RSA_NO_PADDINGn");
printf("5.RSA_X931_PADDINGn");
scanf("%d",&padding);
if(padding==RSA_PKCS1_PADDING)
flen-=11;
else if(padding==RSA_X931_PADDING)
flen-=2;
else if(padding==RSA_NO_PADDING)
flen=flen;
else
{
printf("rsa not surport !n");
return -1;
}
for(i=0;i