你试一下加入下面的代码, 首先调用初始化{ You must call this procedure before any OpenSSL-related function. When you finish, you can clear environment with FreeOpenSSL prodedure. } procedure InitOpenSSL; begin OpenSSL_add_all_algorithms; OpenSSL_add_all_ciphers; OpenSSL_add_all_digests; ERR_load_crypto_strings; end;
原因找到了 因为libeay32.dll的版本不一样 在 OpenSSL 1.0中 rsa.h 的 RSA结构是多了一个参数,而libeay32.pas中这个结构未定义。 导致结构内存错位。 rsa.hstruct rsa_st { /* The first parameter is used to pickup errors where * this is passed instead of aEVP_PKEY, it is set to 0 */ int pad; long version; const RSA_METHOD *meth; /* functional reference if 'meth' is ENGINE-provided */ ENGINE *engine; /* 添加了一个engine 参数 */ BIGNUM *n; BIGNUM *e; BIGNUM *d; BIGNUM *p; BIGNUM *q; BIGNUM *dmp1; BIGNUM *dmq1; BIGNUM *iqmp; /* be careful using this if the RSA structure is shared */ CRYPTO_EX_DATA ex_data; int references; int flags; /* Used to cache montgomery values */ BN_MONT_CTX *_method_mod_n; BN_MONT_CTX *_method_mod_p; BN_MONT_CTX *_method_mod_q; /* all BIGNUM values are actually in the following data, if it is not * NULL */ char *bignum_data; BN_BLINDING *blinding; BN_BLINDING *mt_blinding; }; libeay32.pas RSA = record // The first parameter is used to pickup errors where // this is passed instead of aEVP_PKEY, it is set to 0 pad: integer; version: integer; meth: pRSA_METHOD; {/* functional reference if 'meth' is ENGINE-provided */} { libeay32.pas 基于 libeay32.dll version 0.9.6b, 而1.0版本增加 engine } engine: Pointer; n: pBIGNUM; e: pBIGNUM; d: pBIGNUM; p: pBIGNUM; q: pBIGNUM; dmp1: pBIGNUM; dmq1: pBIGNUM; iqmp: pBIGNUM; // be careful using this if the RSA structure is shared ex_data: CRYPTO_EX_DATA; references: integer; flags: integer; // Used to cache montgomery values _method_mod_n: pBN_MONT_CTX; _method_mod_p: pBN_MONT_CTX; _method_mod_q: pBN_MONT_CTX; // all BIGNUM values are actually in the following data, if it is not // NULL bignum_data: ^byte; blinding: ^BN_BLINDING; end; 在 RSA结构中增加 engine: Pointer 或者使用 0.96的 libeay32.dll
去 http://www.disi.unige.it/person/FerranteM/delphiopenssl/ 下载一个
libeay32.pas v. 0.7m 下载 libeay32.dll 放在System32目录或者程序目录
libeay32.pas 其中 1654,1655行 未结尾添加;
修改 413行的 pRSA = pointer; 为 pRSA = ^RSA;以下代码未测试function RSA_Encrypt(const modulus: PAnsiChar; publicExponent: integer; const pszClearText: PAnsiChar): AnsiString;
var
bnn, bne: pBIGNUM;
rsa: pRSA;
len, nRet: integer;
pszCipherText, b: PAnsiChar;
begin
bnn := BN_new;
bne := BN_new; BN_hex2bn(bnn, modulus);
BN_set_word(bne, publicExponent);
rsa := RSA_new;
rsa^.n := bnn;
rsa^.e := bne; len := RSA_size(rsa);
pszCipherText := AllocMem(len); nRet := RSA_public_encrypt(len - 11, pszClearText, pszCipherText, rsa, RSA_PKCS1_PADDING);
b := AllocMem(nRet * 2 + 1);
BinToHex(pszCipherText, b, nRet); result := b; BN_clear_free(bnn);
BN_clear_free(bne);
freeMem(pszCipherText);
freeMem(b);end;
是啊,为了这个东东,搞晕了!
到这里nRet := RSA_public_encrypt(len - 11, pszClearText, pszCipherText, rsa, RSA_PKCS1_PADDING);语句报错,请指正!
不必转换代码的呀
You must call this procedure before any OpenSSL-related function.
When you finish, you can clear environment with FreeOpenSSL prodedure.
}
procedure InitOpenSSL;
begin
OpenSSL_add_all_algorithms;
OpenSSL_add_all_ciphers;
OpenSSL_add_all_digests;
ERR_load_crypto_strings;
end;
E值用的是:65537也就是010001。
是不是这段C++代码本来就有问题,还是我这里出了什么问题?请指点!
在 OpenSSL 1.0中 rsa.h 的 RSA结构是多了一个参数,而libeay32.pas中这个结构未定义。
导致结构内存错位。
rsa.hstruct rsa_st
{
/* The first parameter is used to pickup errors where
* this is passed instead of aEVP_PKEY, it is set to 0 */
int pad;
long version;
const RSA_METHOD *meth;
/* functional reference if 'meth' is ENGINE-provided */
ENGINE *engine; /* 添加了一个engine 参数 */
BIGNUM *n;
BIGNUM *e;
BIGNUM *d;
BIGNUM *p;
BIGNUM *q;
BIGNUM *dmp1;
BIGNUM *dmq1;
BIGNUM *iqmp;
/* be careful using this if the RSA structure is shared */
CRYPTO_EX_DATA ex_data;
int references;
int flags; /* Used to cache montgomery values */
BN_MONT_CTX *_method_mod_n;
BN_MONT_CTX *_method_mod_p;
BN_MONT_CTX *_method_mod_q; /* all BIGNUM values are actually in the following data, if it is not
* NULL */
char *bignum_data;
BN_BLINDING *blinding;
BN_BLINDING *mt_blinding;
};
libeay32.pas RSA = record
// The first parameter is used to pickup errors where
// this is passed instead of aEVP_PKEY, it is set to 0
pad: integer;
version: integer;
meth: pRSA_METHOD;
{/* functional reference if 'meth' is ENGINE-provided */}
{ libeay32.pas 基于 libeay32.dll version 0.9.6b, 而1.0版本增加 engine }
engine: Pointer;
n: pBIGNUM;
e: pBIGNUM;
d: pBIGNUM;
p: pBIGNUM;
q: pBIGNUM;
dmp1: pBIGNUM;
dmq1: pBIGNUM;
iqmp: pBIGNUM;
// be careful using this if the RSA structure is shared
ex_data: CRYPTO_EX_DATA;
references: integer;
flags: integer;
// Used to cache montgomery values
_method_mod_n: pBN_MONT_CTX;
_method_mod_p: pBN_MONT_CTX;
_method_mod_q: pBN_MONT_CTX;
// all BIGNUM values are actually in the following data, if it is not
// NULL
bignum_data: ^byte;
blinding: ^BN_BLINDING;
end;
在 RSA结构中增加 engine: Pointer 或者使用 0.96的 libeay32.dll