如何使用 PHP 向任何文件添加数字签名(RSA、证书等)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10407523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:05:39  来源:igfitidea点击:

How to add digital signature (RSA, Certificate, etc) to any of file, using PHP?

phpfilersadigital-signature

提问by David

I need to know if any kind of file can be signed digitally, using RSA, a certificate, and that stuff, or if only certain kind of files can be signed. All this, using PHP.

我需要知道是否可以使用 RSA、证书等方式对任何类型的文件进行数字签名,或者是否只能对某些类型的文件进行签名。这一切,使用PHP。

In example: Can a plain text file be signed digitally?, What about images (png, jpeg, bmp)?

例如:可以对纯文本文件进行数字签名吗?图像(png、jpeg、bmp)呢?

I don't need to "attach" an image with a graphic signature.

我不需要“附加”带有图形签名的图像。

Thank you for your help.

感谢您的帮助。

采纳答案by neubert

Using phpseclib, a pure PHP RSA implementation(updated here):

使用phpseclib,一个纯 PHP RSA 实现在此处更新):

<?php
include('Crypt/RSA.php');

$rsa = new Crypt_RSA();
extract($rsa->createKey());

$plaintext = 'terrafrost';

$rsa->loadKey($privatekey);
$signature = $rsa->sign($plaintext);

$rsa->loadKey($publickey);
echo $rsa->verify($plaintext, $signature) ? 'verified' : 'unverified';
?>

The analog to that with the CLI openssl is as follows:

与 CLI openssl 的模拟如下:

openssl dgst -sha1 -sigopt rsa_padding_mode:pss -sigopt rsa_pss_saltlen:-1 -out signature.txt -sign privatekey.txt plaintext.txt 
openssl dgst -sha1 -sigopt rsa_padding_mode:pss -sigopt rsa_pss_saltlen:-1 -prverify privatekey.txt -signature signature.txt plaintext.txt 

回答by Larry K

Re: can any kind of file be signed digitally, using RSA, a certificate, and that stuff, or if only certain kind of files can be signed?

回复:是否可以使用 RSA、证书等方式对任何类型的文件进行数字签名,或者是否只能对某些类型的文件进行签名?

A: Yes and no. On the one hand, a standard digital signature can be computed for any bundle of bits, including a plain text file, image files, binary files, anything you can imagine.

答:有也没有。一方面,可以为任何比特束计算标准数字签名,包括纯文本文件、图像文件、二进制文件,以及您可以想象的任何内容。

But then the questions become:

但随后问题就变成了:

  1. How do you associate the digital signature (which is itself a binary run of bits) with the data file? Does the data file format allow for appending the digital signature to the data? Or do you need to manage the digital signature yourself, perhaps as a separate file, perhaps using your own invented data format?

  2. Once you have the digitally signed data and its signature, how does a recipient verify the data and its signature to assure the recipient of who signed the data (identity) and that the data has not been changed since it was signed (integrity)?

  1. 您如何将数字签名(它本身是位二进制运行)与数据文件相关联?数据文件格式是否允许将数字签名附加到数据?或者您是否需要自己管理数字签名,也许作为一个单独的文件,也许使用您自己发明的数据格式?

  2. 一旦您拥有数字签名的数据及其签名,接收者如何验证数据及其签名以向接收者保证谁签署了数据(身份),并且数据自签名后没有更改(完整性)?

File formats that support digital signatures

支持数字签名的文件格式

The big advantage of file formats that intrinsically support digital signatures is that recipients can verifythe digital signature and the file's integrity simply by receiving the signed file and then using their own verification software. The recipient does not need to install anything from the sender.

本质上支持数字签名的文件格式的一大优势在于,接收者只需接收签名文件,然后使用他们自己的验证软件即可验证数字签名和文件的完整性。收件人不需要安装来自发件人的任何东西。

There are many capable file formats that support digital signatures. Eg, pdf, Word .doc, .docx. Excel .xls, .xlsx. There is a standard for signing xml files. Its benefit is that xml can be used as an envelope for any type of data. For example, a pdf file can be digitally signed and sent to someone. The recipient can then use the standard/free Adobe Reader to open the pdf and verify its digital signature(s).

有许多功能强大的文件格式支持数字签名。例如,pdf、Word .doc、.docx。Excel .xls、.xlsx。有一个用于签署 xml 文件标准。它的好处是 xml 可以用作任何类型数据的信封。例如,可以对 pdf 文件进行数字签名并发送给某人。然后收件人可以使用标准/免费的 Adob​​e Reader 打开 pdf 并验证其数字签名。

The "format" for plain text files (file filled with characters) does not support digital signatures. So you'd need an envelope for the text and its digital signature or deal with the digital signature separately. In any case, the recipient would need your software to verify the data. (Or would need to write his own after you provide a specification for the plain text and the signature.)

纯文本文件(用字符填充的文件)的“格式”不支持数字签名。因此,您需要为文本及其数字签名提供一个信封,或者分别处理数字签名。在任何情况下,收件人都需要您的软件来验证数据。(或者在您提供纯文本和签名的规范后需要自己编写。)

S/MIME offers a standard way of digitally signing text or other email/mime organized data. See rfc 5751. But it is not widely used beyond email agents that can generate or receive/verify signed email messages. Outlook does support this.

S/MIME 提供了一种对文本或其他电子邮件/mime 组织数据进行数字签名的标准方式。见RFC 5751。但是,除了可以生成或接收/验证签名电子邮件的电子邮件代理之外,它并没有被广泛使用。Outlook 确实支持这一点。

回答by nvuono

Why do you want to digitally sign these different files?

为什么要对这些不同的文件进行数字签名?

There is no standard for actually altering text files or images so that the files themselves contain some sort of digital signature.

没有实际更改文本文件或图像以便文件本身包含某种数字签名的标准。

If you want to assure users that files have not been tampered with you can give them an MD5 hash of the file. They can use free tools to check the MD5 hash of a file they downloaded and compare it to the one you've given to make sure their file was not altered. This would typically be used for software/binary packages that a third party could insert malicious code into.

如果你想向用户保证文件没有被篡改,你可以给他们一个文件的 MD5 哈希值。他们可以使用免费工具检查他们下载的文件的 MD5 哈希值,并将其与您提供的文件进行比较,以确保他们的文件未被更改。这通常用于第三方可以插入恶意代码的软件/二进制包。

Another possibility is that you want users to be able to verify the authenticity of certain files. In this case a user might want to be certain that a text file or image came from YOU specifically. This functions almost like getting an MD5 hash of a file with a private key known only to you so that the end user can compare the file signature to your public key to ensure the file has not been tampered with and is coming from a specific source. PGP (pretty good privacy) provides a framework for this.

另一种可能性是您希望用户能够验证某些文件的真实性。在这种情况下,用户可能希望确定一个文本文件或图像是专门来自您的。这几乎就像使用只有您知道的私钥获取文件的 MD5 哈希,以便最终用户可以将文件签名与您的公钥进行比较,以确保文件未被篡改并且来自特定来源。PGP(相当好的隐私)为此提供了一个框架。

I suggest you check out the PGP intro documents for more information on this. http://www.pgpi.org/doc/pgpintro/

我建议您查看 PGP 介绍文档以获取更多信息。http://www.pgpi.org/doc/pgpintro/