RSA加密导致服务器执行超时

发布于 2024-08-31 01:16:15 字数 488 浏览 3 评论 0原文

我正在使用 PHP Crypt_RSA (http://pear.php.net/package/Crypt_RSA)用于加密和解密内容。内容大小为 1kb。结果如下:


  1. keylength = 1024
  2. 加密函数需要时间:225 秒

  1. keylength = 2048
  2. 加密函数需要时间:115 秒

我需要减少这个执行时间,因为大多数实时 apache 服务器的执行时间限制为 120 秒。如何减少这个执行时间? RSA 算法文档表示仅生成 1024 - 2048 个密钥。我实际上尝试生成更大的密钥,但它总是导致执行超时。

如何减少加密-解密执行时间?

谢谢, 尼拉

I am using PHP Crypt_RSA (http://pear.php.net/package/Crypt_RSA) for encrypting and decrypting the contents. Contents are of 1kb size. Following are the results:


  1. keylength = 1024
  2. Encryption function takes time: 225 secs

  1. keylength = 2048
  2. Encryption function takes time: 115 secs

I need to reduce this execution time as most of the live apache servers have 120 sec limit for execution time. How to reduce this execution time? RSA alorithm docs says the only 1024 - 2048 keys are generated. I ACTUALLY tried to generate larger key, but it always results in execution timeout.

How do i work on reducing encryption - decryption execution time?

Thanks,
Nila

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

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

发布评论

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

评论(4

萤火眠眠 2024-09-07 01:16:15

让你的密钥更大:)根据你的数字,你可以通过将密钥大小加倍来减少一半的时间。我确信这是一个错误。

首先要做的是切换到 OpenSSL,它是 C 语言的扩展。如果您使用正确的构建,公钥操作将在汇编中完成,因此它比 PHP 代码快得多。根据我的经验,速度至少快 10 倍。

第二件事是使用像 PKCS#7 这样的标准信封(OpenSSL 支持此)。它将使用对称密钥来加密并使用公钥来加密密钥。对于小消息来说它有很多开销,但从长远来看你会受益。

Make your key bigger :) According to your numbers, you halve the time by doubling the key size. I am sure it's an error.

First thing to do is to switch to OpenSSL, which is an extension in C. If you use the correct build, the public key operations are done in assembly so it's much, much faster than PHP code. In my experience, it's at least 10 times faster.

The 2nd thing to do is to use a standard envelope like PKCS#7 (OpenSSL supports this). It will use symmetric key to encrypt and encrypt the key using public key. It has lots of overhead for small message but you will benefit in long run.

无声情话 2024-09-07 01:16:15

首先,我建议使用 phpseclib - 纯 PHP RSA 实现。 PEAR 的 Crypt_RSA 的问题在于它不支持很多密钥格式,不进行 RSA 致盲(因此容易受到定时攻击)并且不支持 OAEP / PSS。

ZZ Coder 建议使用 PKCS#7。以下 URL 讨论如何在 PKCS#7 的轻量级版本中使用 phpseclib:

http://area51.phpbb.com/phpBB/viewtopic.php?f=84&t=33024

First, I would recommend phpseclib - a pure PHP RSA implementation - be used. The problem with PEAR's Crypt_RSA is that it doesn't support very many key formats, doesn't do RSA blinding (and as such is vulnerable to timing attacks) and doesn't support OAEP / PSS.

ZZ Coder recommends PKCS#7 be used. The following URL discusses how to use phpseclib in a lightweight version of PKCS#7:

http://area51.phpbb.com/phpBB/viewtopic.php?f=84&t=33024

べ繥欢鉨o。 2024-09-07 01:16:15

不要使用 RSA 来加密内容,而是使用 RSA 来加密对称密钥,然后使用该对称密钥来加密内容。

对称密码 AES 使用 256 位的密钥长度,即 32 字节,使用 RSA 加密/解密的数据比您现在加密的千字节数据少大约 30 倍。

因此,115 秒将减少到 3-4 秒,加上 AES 所用的加密/解密时间,这比 RSA 快得多。

Do not use RSA to encrypt content, use RSA to encrypt a symmetric key that is then used to encrypt the content.

The symmetric cipher AES uses a key length of 256 bits, which is 32 bytes, about 30 times less data to encrypt/decrypt using RSA than the kilobyte of data you encrypt now.

So the 115 seconds will be reduced to 3-4 secs plus the encryption/decryption time used for AES which is much faster than RSA.

离鸿 2024-09-07 01:16:15

您可以考虑使用 mcrypt 或 openssl 来满足您的加密/解密需求。有关示例,请参阅 openssl_public_encrypt。这将比 Crypt_* 中完成的 PHP 实现快得多(即使它们使用 bigint 或其他 C 大整数实现,如 gmp)。

You may consider to use mcrypt or openssl instead for your encryption/decryption needs. See openssl_public_encrypt for examples. That will be much faster than the PHP implementation done in Crypt_* (even if they use bigint or other C large integer implementation like gmp).

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