delphi rsa 加密

2017-08-25 19:29:01来源:CSDN作者:ma_shu_guang人点击

分享
procedure PublickEncrypt(data_to_crypt: string; var data_encrypted_b64: string; var data_encrypted_bin: Pointer);var  str, data: AnsiString;  len, b64len: Integer;  penc64: PAnsiChar;  pkey: pRSA;  b64, mem: pBIO;  size: Integer;  err: Cardinal;  decc: string;begin  pkey := EVP_PKEY_get1_RSA(FPubKey);  size := RSA_size(pkey);  GetMem(Fbuf, size);  str := AnsiString(data_to_crypt);  len := RSA_public_encrypt(Length(str), PAnsiChar(str), Fbuf, pkey, RSA_PKCS1_PADDING);  if len > 0 then  begin    //create a base64 BIO    b64 := BIO_new(BIO_f_base64);    BIO_set_flags(b64, 0);    mem := BIO_push(b64, BIO_new(BIO_s_mem));    try      //encode data to base64      BIO_write(mem, Fbuf, len);      BIO_flush(mem);      b64len := BIO_get_mem_data(mem, penc64);      //copy data to string      SetLength(data, b64len);      Move(penc64^, PAnsiChar(data)^, b64len);      data_encrypted_b64 := string(data);    finally      BIO_free_all(mem);    end;  end else  begin    err := ERR_get_error;    repeat      err := ERR_get_error;    until err = 0;  end;end;procedure LoadPublicKey;var  mem: pBIO;  err: Cardinal;begin   mem := BIO_new_mem_buf(@rsapublicData,rsapublicSize);  try    FPubKey := PEM_read_bio_PUBKEY(mem, FPubKey, nil, nil);    if FPubKey <> nil then    begin    end    else    begin      err := ERR_get_error;      repeat        err := ERR_get_error;      until err = 0;    end;  finally    BIO_free_all(mem);  end;end;function RSAEncrypt(aInput: string): string;var  bs64: string;  crypt: pointer;begin  try    OpenSSL_add_all_algorithms;    OpenSSL_add_all_ciphers;    OpenSSL_add_all_digests;    ERR_load_crypto_strings;    ERR_load_RSA_strings;    LoadPublicKey;    PublickEncrypt(aInput, bs64, crypt);    Result := bs64;  finally    EVP_cleanup;    ERR_free_strings;  end;end;

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台