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");

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What is Identity in SQL? Specific code examples are needed. In SQL, Identity is a special data type used to generate auto-incrementing numbers. It is often used to uniquely identify each row of data in a table. The Identity column is often used in conjunction with the primary key column to ensure that each record has a unique identifier. This article will detail how to use Identity and some practical code examples. The basic way to use Identity is to use Identit when creating a table.

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

An unpatchable Yubico two-factor authentication key vulnerability has broken the security of most Yubikey 5, Security Key, and YubiHSM 2FA devices. The Feitian A22 JavaCard and other devices using Infineon SLB96xx series TPMs are also vulnerable.All

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We
