search
HomeBackend DevelopmentPHP Tutorial php 怎么生成rsa加密的公钥和私钥

php 如何生成rsa加密的公钥和私钥
网上看了哈似乎可以用openssl
但是 似乎都读取的文件

也没看懂 php到底怎么来调用他???不知道哪位大哥能指点一二?给哥示例而代码

或者地址!

现在我的系统可能要求的是用 数据库

得把公钥 和私钥 放到数据库

这个怎么办?

------解决方案--------------------
我也只知道有这么个算法 ,没用上。 存了些资料给你参考参考
rsa非对称加密

http://i.laoer.com/php-rsa.html

http://www.zeali.net/entry/8

http://pear.php.net/package/Math_BigInteger/download


dh非对称加密

http://pear.php.net/package/Crypt_DiffieHellman/download

使用
http://blog.csdn.net/ihefe/archive/2011/01/11/6128386.aspx
------解决方案--------------------
以下内容来自互联网
[url]http://su200909.blog.163.com/blog/static/127808604200910202646745/[/url]

  RSA方法,简单地说,是生成一组数字n、e和d。用n和e加密明文(明文要求小于n),用n和d解密密文。
  寻找一组n、e、d很容易,但当n足够大时,由n和e求出d很难。这是RSA的安全性所在。
  https不需说了。这里说的是,如何在non-https环境使用JavaScript和PHP实现RSA加密/解密:在浏览器使用JavaScript加密信息,在服务器用PHP解密信息。

一,用JavaScript加密

有关RSA,比较好的JavaScript见[url]http://ohdave.com/rsa/[/url](该页面还有密钥生成程序下载)。用法也简单:

1,设置参数

setMaxDigits(d);

函数的参数d跟n的位数有关。关系是

d=n的十六进制位数/2+3

比如n是十六进制16位(对应二进制125~128位),d就设成19。

2,设置密钥

key=new RSAKeyPair(e,d,n);

参数e、d、n是十六进制表示。加密只需要e和n,d给空串。如:

key=new RSAKeyPair('3d','','a090f4fdaba1c60975fb3b9ea6937a27');

3,加密

ct=encryptedString(key,pt+'\x01');

这个函数把明文pt加密为密文ct(十六进制字符串)。对长信息,则分段加密(但PEAR::Crypt_RSA不认可,下文将解决这个问题)。
'\x01'这个字节,是PEAR::Crypt_RSA要求的结尾符。保证PEAR::Crypt_RSA可以成功解密的一组明文长度(字节)为:

n的十六进制位数/2-3

对2的密钥,一次可以加密13个字节(14个也可能成功,看密钥和明文的情况)。

二,用PHP解密

PEAR::Crypt_RSA的安装很简单,这里不说了。

1,打开数学库

require_once('Crypt/RSA.php');//一般是这个路径
$math_obj = &Crypt_RSA_MathLoader::loadWrapper();

PHP 的数学库有GMP、BigInt、BCMath。如果不给loadWrapper指定参数,他会按照这个顺序选择已经加载的数学库(分别对应 Crypt_RSA_Math_GMP类、Crypt_RSA_Math_BigInt类和Crypt_RSA_Math_BCMath类)。 BCMath是php的默认配置,但比起GMP,实在是慢多了。不了解BigInt,不好说。

2,载入密钥

$dd = $math_obj->int2bin('0x'.$d);
$nn = $math_obj->int2bin('0x'.$n);
$pk = new Crypt_RSA_Key($nn, $dd, 'private');

$rsa_obj = new Crypt_RSA;
$rsa_obj->setParams(array('dec_key' => $pk));

3,解密

$pt = $rsa_obj->decryptBinary($math_obj->int2bin('0x'.$ct));

PEAR::Crypt_RSA可以使用十六进制密钥,也可以解密十六进制密文。但对Crypt_RSA_Math_BCMath类必须修改一下int2bin的代码,使他能够把十六进制数字转换为内部格式。
当然,如果是十进制密钥/密文,'0x'前缀就不用加了。

对Crypt_RSA_Math_BCMath::int2bin()的改动是在函数的开头加上:

$p=substr($num,0,2);
if($p=='0x'||$p=='0X'){
$len=strlen($num);
$result = 0;
$factor = 1;
for($i=$len-1;$i>=2;$i--){
$result=bcadd($result,bcmul(hexdec($num[$i]),$factor));
$factor=bcmul($factor,16);
}
$num=$result;
}

三,解决JavaScript加密功能浪费问题

JavaScript的encryptedString()函数对长信息分段加密,密文用空格分割各个段落。而Crypt_RSA类的decryptBinary()函数不能适应这一点。为了利用充分JavaScript的加密功能,只能给Crypt_RSA类增加一个函数:

function decryptString($s, $key = null)
{
$result = ‘’;
if($key==null)
$key = $this->_dec_key;
$exp = $this->_math_obj->bin2int($key->getExponent());
$modulus = $this->_math_obj->bin2int($key->getModulus());
$chunk_len = $key->getKeyLength() - 1;
$block_len = (int) ceil($chunk_len / 8);

$radix=10;
if(substr($s,0,2)=='0x'){
$radix=16;
$s=substr($s,2);
}
$blocks = explode(" ",$s);

for($i = 0; $i $enc_data=$blocks[$i];
if ($radix == 16)
$enc_data='0x'.$enc_data;
$enc_data=$this->_math_obj->int2bin($enc_data);

$data_len = strlen($enc_data);
$curr_pos = 0;
$bit_pos = 0;
$plain_data = $this->_math_obj->bin2int("\0");

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.