本帖最后由 mops 于 2012-03-31 11:04:19 编辑

解决方案 »

  1.   

    再请问openssl怎么应用在delphi上,先来先得分啊
      

  2.   

    又是楼主啊
     
    去 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;
      

  3.   

    to ansinlee:
    是啊,为了这个东东,搞晕了!
    到这里nRet := RSA_public_encrypt(len - 11, pszClearText, pszCipherText, rsa, RSA_PKCS1_PADDING);语句报错,请指正!
      

  4.   

    nRet := RSA_public_encrypt(len - 11, pszClearText, pszCipherText, rsa, RSA_PKCS1_PADDING);将此句中的len-11改为53或len等,调试通过,但返回的string为空,请指正!
      

  5.   

    楼主可以将上面的代码写一个DLL,在程序中调用即可
    不必转换代码的呀
      

  6.   

    你试一下加入下面的代码, 首先调用初始化{
      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;
      

  7.   

    要崩溃了nRet := RSA_public_encrypt(len - 11, pszClearText, pszCipherText, rsa, RSA_PKCS1_PADDING);又在此句中报错,把len-11改成数值也出错,不知道问题出在什么地方。我返回了len值,只有3,所以len-11肯定是错的。我的M值用的是十六进制字符串:A83362EA88DB07CA5287B967B329248473572FE0D9028D0CA7DE2ADBC80FE1BFC3B222FC1F34E3169225AB5FDCBC0A9D60DDEC66CC4154606326538E82D2861522B2E43D78271AEF2FF80C3605DD0B8E047DDF8E83C7BBA3E9AE45B5F3835B2AB32CCC72480CB899573DDE309843D94D16221D0B59E08ACCC7DDB844AE140EE7
    E值用的是:65537也就是010001。
    是不是这段C++代码本来就有问题,还是我这里出了什么问题?请指点!
      

  8.   

    原因找到了 因为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