在网上找到一篇介绍openssl库的RSA算法的文章,这里给出连接
http://www.cnblogs.com/nadoo/archive/2008/03/14/1105991.html
编译了一下 感觉良好 可以用。
[关于如何编译openssl代码 请参考
http://hi.baidu.com/blueapple_c/blog/item/d73cab2bf5191e3ad52af17a.html]
后来我把 unsigned char *in = "abc"; 修改为 unsigned char *in = "我";
发现RSA_private_encrypt会返回 -1 ,这是为什么
求达人解释 小弟感激不尽。
代码:
加密
/*
gcc -o rsa-encrypt rsa-encrypt.c -lcrypto
*/
#include <openssl/rsa.h>
#include <openssl/err.h>
#define MODULUS "C8FBCF21"
#define PUBLIC_EXPONENT RSA_F4
#define PRIVATE_EXPONENT "97B55D7D"
int main()
{
int ret, flen;
BIGNUM *bnn, *bne, *bnd;
unsigned char *in = "abc";
unsigned char *out;
bnn = BN_new();
bne = BN_new();
bnd = BN_new();
BN_hex2bn(&bnn, MODULUS);
BN_set_word(bne, PUBLIC_EXPONENT);
BN_hex2bn(&bnd, PRIVATE_EXPONENT);
RSA *r = RSA_new();
r->n = bnn;
r->e = bne;
r->d = bnd;
RSA_print_fp(stdout, r, 5);
flen = RSA_size(r);// - 11;
out = (char *)malloc(flen);
bzero(out, flen);
//memset(out, 0, flen);
printf("Begin encrypt... ");
ret = RSA_private_encrypt(flen, in, out, r, RSA_NO_PADDING);
if (ret < 0)
{
printf("Encrypt failed! ");
return 1;
}
printf("Size:%d ", ret);
printf("ClearText:%s ", in);
printf("CipherText(Hex):");
int i;
for (i=0; i<ret; i++)
{
printf("0x%02x, ", *out);
out++;
}
printf(" ");
//free(out);
RSA_free(r);
return 0;
}
解密:
/*
gcc -o rsa-decrypt rsa-decrypt.c -lcrypto
*/
#include <openssl/rsa.h>
#define MODULUS "C8FBCF21"
#define PUBLIC_EXPONENT RSA_F4
#define PRIVATE_EXPONENT "97B55D7D"
int main()
{
int ret, flen;
BIGNUM *bnn, *bne;
unsigned char in[] = {0x98, 0x79, 0xb2, 0x76};
unsigned char *out;
bnn = BN_new();
bne = BN_new();
BN_hex2bn(&bnn, MODULUS);
BN_set_word(bne, PUBLIC_EXPONENT);
RSA *r = RSA_new();
r->n = bnn;
r->e = bne;
RSA_print_fp(stdout, r, 5);
flen = RSA_size(r);
out = (unsigned char *)malloc(flen);
bzero(out, flen);
printf("Begin decrypt... ");
ret = RSA_public_decrypt(sizeof(in), in, out, r, RSA_NO_PADDING);
if (ret < 0)
{
printf("Decrypt failed! ");
return 1;
}
printf("Size:%d ", ret);
printf("ClearText:%s ", out);
free(out);
RSA_free(r);
return 0;
}
编译openssl的方法
0.安装ActivePerl-5.12.3.1204并重启电脑(下载地址 http://bjkdt.newhua.com/down/ActivePerl-5.12.3.1204-MSWin32-x86-294330.zip)
1. 解压openssl-0.9.8r.tar.gz到D:\openssl-0.9.8r
2.在cmd中进入D:\openssl-0.9.8r目录 然后执行perl Configure VC-WIN32
3.执行ms\do_ms
4.进入vc6的bin目录 我的电脑上是 D:\Program files\Microsoft Visual Studio\VC98\Bin
5.执行vcvars32
6.进入D:\openssl-0.9.8r
7.编译动态库:
nmake -f ms\ntdll.mak
编译静态库:
nmake -f ms\nt.mak
测试动态库:
nmake -f ms\ntdll.mak test
测试静态库:
nmake -f ms\nt.mak test
安装动态库:
nmake -f ms\ntdll.mak install
安装静态库:
nmake -f ms\nt.mak install
/如果没编译成功*****
清除上次动态库的编译,以便重新编译:
nmake -f ms\ntdll.mak clean
清除上次静态库的编译,以便重新编译:
nmake -f ms\nt.mak clean
8.如果成功了 d盘根目录下会生成一个usr文件夹, 里面的第n层包含了 bin lib include文件夹
然后 配置一下 vc6的目录 3项 就ok啦
然后随便做个测试程序 测试一下
#include <stdio.h>
#include <string.h>
#include <openssl/rc4.h>
#pragma comment(lib, "libeay32.lib")
int main(int argc, char *argv[])
{
RC4_KEY key;
char szHello[16] = {"hello ~~!\n"};
char szTemp[16] = {0};
RC4_set_key(&key, 16, (unsigned char *)"1234567890123456");
RC4(&key, 16,(unsigned char *)szHello, (unsigned char *)szTemp);
memset(szHello, 0, 16);
RC4_set_key(&key, 16, (unsigned char *)"1234567890123456");
RC4(&key, 16,(unsigned char *)szTemp, (unsigned char *)szHello);
printf(szHello);
return 0;
}
后话:请严格按照本篇文章给出的顺序进行,本人亲测3台电脑都没有问题。 编译后的 include lib bin 可用于VS2008使用。
[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)