PHP 使用 HTML keygen 公钥加密返回

发布于 2024-11-19 09:59:08 字数 68 浏览 1 评论 0原文

如何使用从 HTML5 的 keygen 获得的公钥在 PHP 中加密任何内容?有标准函数吗?

提前致谢!

How can I encrypt - anything - within PHP with the public key I get from HTML5's keygen? Is there a standard function for that?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫﹏色ふ单纯 2024-11-26 09:59:08

选择的答案绝对不正确!

如果您使用keygen元素生成对称加密密钥,它将首先否定使用加密提供的任何保护。

通过不安全的通道发送对称加密密钥是使用密码学的首要规则之一。私钥就是这样,私人的。此外,公钥不应该用作加密密码,如果它是 RSA 密钥类型,它可以用于加密数据和/或验证数据。

keygen 元素应用于生成签名的公钥和密钥。首先创建一个私钥(RSA),浏览器注册并使用它来提供基于证书的身份验证

要进一步研究确认此答案,请参阅以下参考文献:

  1. KeyGen w/ PHP
  2. OpenSSL SPKAC 文档
  3. 证书签名请求
  4. PHP OpenSSL SPKAC 补丁(在最新的主分支中可用)
  5. PHP OpenSSL 扩展的独立补丁,可与从浏览器 KenGen 元素发送的 SPKAC 本地工作
  6. SPKI 的 PHP 文档

The answer selected is absolutely incorrect!

If you are using the keygen element to generate a symmetric encryption key, it will negate any protections offered by using encryption in the first place.

Sending a symmetric encryption key over an insecure channel is one the first rules of using cryptography. Private keys are just that, private. What's more a public key should not be used as an encryption password, it can be used to encrypt data and/or verify data if it is an RSA keytype.

The keygen element should be used to generate a signed public key & challenge by first creating a private key (RSA) which gets registered and used by the browser to provide certificate based authentication.

For further research confirming this answer please see the following references:

  1. KeyGen w/ PHP
  2. OpenSSL SPKAC documentation
  3. Certificate Signing Request
  4. PHP OpenSSL SPKAC patch (available within the latest master branch)
  5. Standalone patch for PHP OpenSSL extension for working natively with SPKAC's sent from the browsers KenGen element
  6. PHP documentation for SPKI
机场等船 2024-11-26 09:59:08

生成签名的公钥,也称为 SPKAC。据我所知,使用它们的唯一简单方法是使用 phpseclib,一个纯 PHP SPKAC 实现。例如。

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

$x509 = new File_X509();
$x509->loadSPKAC('MIIBQzCBrTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAwnnEID0RuUfmVbB1rNgsZ6BL8MtITio00wLJhQ075cvxcgllis1M4PhK6SKtm0tmSfXZTZz8jrbeczSFkdl6UjFYCXIrxnmcGmpB4A6fdKsBfFtmMOPLXLr5nGM+4DyOMZCZObbLOnsv7usimDpixk+juZ65Gmhb9rB+2MAKEbECAwEAARYJMTIzNDU2Nzg5MA0GCSqGSIb3DQEBBAUAA4GBAB99Nkdhzeazy0bTCb69Mp8Q3BDOgeMonUEg0ETlPaTX/y9HvwkgWHdMROQmc8JiDNTZZzpssrgdKtzsqQOyEIOHEKDbAXL3+GlglCaQ3g/72PbJPFusYdsPjEPYKXil6U1nCikikjaEZVM1HbzVFSmbEAuLwYwD1Z6LovYYaxr0');

$pubkey = (string) $x509->getPublicKey();
$rsa = new Crypt_RSA();
$rsa->loadKey($pubkey);
echo bin2hex($rsa->encrypt('whatever'));

generates signed public keys, also known as SPKACs. The only easy way I know of to use them is to use phpseclib, a pure PHP SPKAC implementation. eg.

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

$x509 = new File_X509();
$x509->loadSPKAC('MIIBQzCBrTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAwnnEID0RuUfmVbB1rNgsZ6BL8MtITio00wLJhQ075cvxcgllis1M4PhK6SKtm0tmSfXZTZz8jrbeczSFkdl6UjFYCXIrxnmcGmpB4A6fdKsBfFtmMOPLXLr5nGM+4DyOMZCZObbLOnsv7usimDpixk+juZ65Gmhb9rB+2MAKEbECAwEAARYJMTIzNDU2Nzg5MA0GCSqGSIb3DQEBBAUAA4GBAB99Nkdhzeazy0bTCb69Mp8Q3BDOgeMonUEg0ETlPaTX/y9HvwkgWHdMROQmc8JiDNTZZzpssrgdKtzsqQOyEIOHEKDbAXL3+GlglCaQ3g/72PbJPFusYdsPjEPYKXil6U1nCikikjaEZVM1HbzVFSmbEAuLwYwD1Z6LovYYaxr0');

$pubkey = (string) $x509->getPublicKey();
$rsa = new Crypt_RSA();
$rsa->loadKey($pubkey);
echo bin2hex($rsa->encrypt('whatever'));
绝影如岚 2024-11-26 09:59:08

您可以使用 PHP 中提供的 mcrypt 函数(如果您的服务器上有可用的库扩展) - 例如

您的 HTML 表单有一个字段:

您的PHP 可以执行以下操作:(未测试)

$myKey = $_POST['mykey']; // this is your posted keygen value
$encryptThis = 'This is the text I want to encrypt';
$cipher = MCRYPT_BLOWFISH;
$mode = MCRYPT_MODE_ECB;
$iv_size = mcrypt_get_iv_size($cipher, $mode);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$encOutput = mcrypt_encrypt($cipher, $myKey, $encryptThis, $mode, $iv);

// your encrypted text is now in the variable $encOutput

有关 mcrypt 的更多信息,请参阅: https://www.php.net/manual/en/book.mcrypt.php

You can use the mcrypt functions available in PHP (if you have the library extension available on your server) - for example

Your HTML Form has a field: <keygen name="mykey" />

Your PHP could do something like: (not tested)

$myKey = $_POST['mykey']; // this is your posted keygen value
$encryptThis = 'This is the text I want to encrypt';
$cipher = MCRYPT_BLOWFISH;
$mode = MCRYPT_MODE_ECB;
$iv_size = mcrypt_get_iv_size($cipher, $mode);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$encOutput = mcrypt_encrypt($cipher, $myKey, $encryptThis, $mode, $iv);

// your encrypted text is now in the variable $encOutput

For more on mcrypt see: https://www.php.net/manual/en/book.mcrypt.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文