gpt4 book ai didi

php - 将此 php 数字签名转换为 Delphi

转载 作者:行者123 更新时间:2023-12-02 01:12:41 27 4
gpt4 key购买 nike

我想将这个php数字签名函数重写为Delphi代码。

  function SaySig() {

$privKeyFilePath = "c:\temp\myrsakey.pem";

$data = "sign this string";
$fp = fopen($privKeyFilePath, "r");
$priv_key = fread($fp, 8192);
fclose($fp);
$privatekeyid = openssl_get_privatekey($priv_key);
openssl_sign($data, $signature, $privatekeyid, OPENSSL_ALGO_SHA1);
openssl_free_key($privatekeyid);
$sig = base64_encode($signature);
echo "<br>";
echo "Signature:".$sig."<br><br>";
}

我不关心使用哪个组件(密码箱、DelphiOpenSSL、Chilkat Crypt ActiveX 等)。我已经尝试了所有这些,但没有成功;这让我觉得我正在做一些根本错误的事情。

我的 key 是这样生成的:

openssl req -x509 -nodes -days 365 -newkey rsa:1024 -sha1 -subj "/C=US/ST=CA/L=Mountain View/CN=www.mycompany.com" -keyout myrsakey.pem -out c:\temp\myrsacert.pem

key 的内容(不用担心 - 我会生成一个新的):

-----BEGIN RSA PRIVATE KEY----- MIICXgIBAAKBgQCqsR7s4X74LfTiLv1PP6Yn0SBpGBtbzkBSQ95E2b9Haa3Qtf0a KjDJpZLMwXC/IrSP7K2Gxbl2cZotT19GVgw6PcYPTBBWX2gJoVrnQZP8uPdlGAgS plODP55R9f4F0KzIpE6d+dpTGfJ1wysFqYN8fxtlu8K7YO/Mh8tNzN5VOQIDAQAB AoGBAIvCvRyeQlU5Y+JzMSvbZNQDUrNabsRL67SwJ2VemVUCvbQ/3v62fv4M2VdY KFYIN6oE08yfRw0pVWE2NT+lIxqSQx7+qv84Y7duqT7155wpCFj+a/6pYyNTFNFi 5wiTnN13eyHNgKxZm7QcMH67T/noTgz0LoT5p54ynmfNcjyBAkEA3DCEQ6Dm2xYH Nhk3+7sNEVklN20zNqyYvrCunNLAiLioF1jDApdfcT8YtVd29L7tH1ZdJYG5DXJ8 Bs7eKLGekQJBAMZzy0Q7LZHdWQxSRi7wy0eq6SqZMqi0pb9VPuXjWG1y+rtRr1vV vyMaGz4rcE7mkbq/Nkn+AQXc30GOj3GE8CkCQQCMDVwDfBN6pL8/fLjsJ+S+9RnD 8HRTwWKCX/UgkLif/fwEpZOcUVYGvSBlL9XdBJfkh9VFZwaidABJgEk0Tw3RAkBd 6pjMnpDvUeh9e0Y5mr0pGookHcIqsuspxEby9od3rI1aLsslU9+T1hwEbPxGarmW vj0MAUgspR2G4deiqn4ZAkEAnWxV7NhtVPLs5Y2ZYeHz7ipdcSL4/keLW4PwKerF 7LJj4s7/6ZqnHA6Z0yhCcziflYQArWt1ViLMIYZ8grr5Kg== -----END RSA PRIVATE KEY-----

输出如下:

jcIIsr145dTwDrT8g4jb2HZ5FP5UL6/9mK7hF6hC2lCZGlM0W4QqFqytghWaU0w3Z6JkMVUlxxWtQ2R+ vWQVB0F3htAtbVZkiA67x0zor+zmpClBIazmfVJlng4sG1R7CCUZ0gGhdm4JMc08VsWU25utudcG6inpl whQiZgefW0=

这可能看起来像是我在请求某人“做我的工作”。但我已经为此努力了一段时间,但没有成功。谢谢。

有人要求我发布一些代码。以下是我尝试过的一些事情..

function TMainWeb.sign(mstring: String): string;
var
mPrivateKey: TLbRSAKey;
LbRSASSA1: TLbRSASSA;
begin
LbRSASSA1:= TLbRSASSA.create(nil);
LbRSASSA1.PrivateKey.LoadFromFile('C:\temp\myrsakey.der');
LbRSASSA1.HashMethod := TRSAHashMethod(hmSHA1);
LbRSASSA1.SignString(mString);
result := LbRSASSA1.Signature.IntStr;
end;

function TMainWeb.sign1(mstring: String): string;
var
LbDSA1: TLbDSA;
mPrivateKey: TLbRSAKey;
begin
mPrivateKey := TLbRSAKey.Create(aks1024);
mPrivateKey.LoadFromFile('C:\temp\myrsakey.der');
LbDSA1 := TLbDSA.create(application);
lbDSA1.PrivateKey.Assign(mPrivateKey);
LbDSA1.SignString(mString);
end;

function TMainWeb.Sign2(mString: String): string;
var
signer: TMessageSigner;
begin
signer := TMessageSigner.Create;
signer.LoadPrivateKey('C:\temp\myrsakey.pem');
signer.PlainMessage := mString;
signer.MIMESign;
result := signer.SignedMessage;
end;

最佳答案

试试这个。
我并不声称它是完美的代码(!),但它可以编译:-)并给出与您引用的相同的结果。使用您上面提到的 M Ferrante 的 OpenSSL API。它所做的很多事情通常只在启动时执行一次 - 例如加载私钥、InitSSL 等。我使用 Jedi JCL 来处理 base64 内容 - 它更简单。
另外,其中一些看起来有点奇怪(使用 TBytes,而 PChar 会做等等),因为我最初使用修改后的 Delphi 2010 API header 编写它,但后来意识到您使用的是 D2007 并且 TEncoding 不可用,并且需要一些 mod。
(SignStringToBase64 是主要调用,位于列表底部)

uses libeay32, jclmime;

const
LIBEAY_DLL_NAME = 'libeay32.dll';

// These aren't defined in the original libeay32.pas file
procedure EVP_MD_CTX_init(ctx: PEVP_MD_CTX); cdecl; external LIBEAY_DLL_NAME;
function EVP_MD_CTX_cleanup(ctx: PEVP_MD_CTX): integer; cdecl; external LIBEAY_DLL_NAME;

procedure InitSSL;
begin
OpenSSL_add_all_algorithms;
OpenSSL_add_all_ciphers;
OpenSSL_add_all_digests;
ERR_load_crypto_strings;
// Seed the pseudo-random number generator
// This should be something a little more "random"!
RAND_load_file('c:\windows\paint.exe', 512);
end;

procedure FinalizeSSL;
begin
EVP_cleanup;
end;

function GetSSLErrorMessage: string;
const
BUFF_SIZE = 128; // OpenSSL docs state should be >= 120 bytes
var
err: TBytes;
begin
SetLength(err, BUFF_SIZE);
ERR_error_string(ERR_get_error, @err[0]);
result := string(err);
end;

function RSALoadPrivateKey(const AFileName, APassPhrase: string): PRSA;
var
bp: pBIO;
fn, pp: PAnsiChar;
pk: PRSA;
begin
fn := PAnsiChar(AnsiString(AFileName));
pp := PAnsiChar(AnsiString(APassPhrase));
bp := BIO_new(BIO_s_file());
BIO_read_filename(bp, fn);
pk := nil;
result := PEM_read_bio_RSAPrivateKey(bp, pk, nil, pp);
if result = nil then
raise Exception.Create('Private key failure.' + GetSSLErrorMessage);
end;

function LoadPrivateKey(const AFileName, APass: string): PEVP_PKEY;
var
rkey: PRSA;
begin
rkey := RSALoadPrivateKey(AFileName, APass);
result := EVP_PKEY_new;
EVP_PKEY_assign(result, EVP_PKEY_RSA, rkey);
end;

procedure CleanUpKey(AKey: PEVP_PKEY);
begin
if (AKey <> nil) then
begin
EVP_PKEY_free(AKey);
// The OpenSSL docs state that the related rsa key will also
// be freed when the parent key is freed
end;
end;

function EVPSign(ASource: TBytes; const APrivateKey: PEVP_PKEY): TBytes;
var
keysize: integer;
ks: cardinal;
ctx: EVP_MD_CTX;
begin
keysize := EVP_PKEY_size(APrivateKey);
SetLength(result, keysize);

EVP_MD_CTX_init(@ctx);
try
EVP_SignInit(@ctx, EVP_sha1);
EVP_SignUpdate(@ctx, @ASource[0], Length(ASource));
EVP_SignFinal(@ctx, @result[0], ks, APrivateKey);
SetLength(result, ks);
finally
EVP_MD_CTX_cleanup(@ctx);
end;
end;

function Base64EncodeBytes(Input: TBytes): string;
var
b64: TBytes;
begin
SetLength(b64, jclMime.MimeEncodedSizeNoCRLF(Length(Input)));
jclMime.MimeEncodeNoCRLF(Input[0], Length(Input), b64[0]);
result := string(b64);
end;

function SignStringToBase64(const AText: string): string;
var
key: PEVP_PKEY;
src, enc: TBytes;
begin
InitSSL;
try
key := LoadPrivateKey('c:\temp\priv-key.pem', '');
try
SetLength(src, Length(AText));
CopyMemory(@src[0], @AText, Length(AText));
enc := EVPSign(src, key);
result := Base64EncodeBytes(enc);
finally
CleanUpKey(key);
end;
finally
FinalizeSSL;
end;
end;

关于php - 将此 php 数字签名转换为 Delphi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1792541/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com