很多站长朋友们都不太清楚php公钥转换,今天小编就来给大家整理php公钥转换,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php 如何生成2048的私钥和1024的公钥长度 2、 php 用带公钥的加密方法,然后用java 来解密,英文,可以解开,中文,无法解开,谁有什么好的方法。 3、 php rsa怎么生成公钥和私钥 4、 php 怎么生成rsa加密的公钥和私钥 php 如何生成2048的私钥和1024的公钥长度以下命令来生成密钥对。
$openssl genrsa -out mykey.pem 2048
$openssl pkcs8 -topk8 -inform PEM -outform PEM -in mykey.pem \
-out private_key.pem -nocrypt
这个命令得到的公共密钥。
$ openssl rsa -in mykey.pem -pubout -outform DER -out public_key.der
我写了两方法读取私钥和公钥
分别。public PrivateKey getPemPrivateKey(String filename, String algorithm) throws Exception {
File f = new File(filename);
FileInputStream fis = new FileInputStream(f);
DataInputStream dis = new DataInputStream(fis);
byte[] keyBytes = new byte[(int) f.length()];
dis.readFully(keyBytes);
dis.close();
String temp = new String(keyBytes);
String privKeyPEM = temp.replace("-----BEGIN PRIVATE KEY-----\n", "");
privKeyPEM = privKeyPEM.replace("-----END PRIVATE KEY-----", "");
//System.out.println("Private key\n"+privKeyPEM);
Base64 b64 = new Base64();
byte [] decoded = b64.decode(privKeyPEM);
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decoded);
KeyFactory kf = KeyFactory.getInstance(algorithm);
return kf.generatePrivate(spec);
}
public PublicKey getPemPublicKey(String filename, String algorithm) throws Exception {
File f = new File(filename);
FileInputStream fis = new FileInputStream(f);
DataInputStream dis = new DataInputStream(fis);
byte[] keyBytes = new byte[(int) f.length()];
dis.readFully(keyBytes);
dis.close();
String temp = new String(keyBytes);
String publicKeyPEM = temp.replace("-----BEGIN PUBLIC KEY-----\n", "");
publicKeyPEM = privKeyPEM.replace("-----END PUBLIC KEY-----", "");
Base64 b64 = new Base64();
byte [] decoded = b64.decode(publicKeyPEM);
X509EncodedKeySpec spec =
new X509EncodedKeySpec(decoded);
KeyFactory kf = KeyFactory.getInstance(algorithm);
return kf.generatePublic(spec);
}
php 用带公钥的加密方法,然后用java 来解密,英文,可以解开,中文,无法解开,谁有什么好的方法。这是因为编码的关系吗?你将加密的字符串转成utf-8,试下,
如果这样不行,建议你将汉字转化成别的格式,比喻十六进制,或者串行化成为一个字符串,在进行
php rsa怎么生成公钥和私钥1、加米解米的第一步是生成公钥、私钥对,私钥加米的内容能通过公钥解米(反过来亦可以)下载开源RSA米钥生成工具openssl(通常Linux系统都自带该程序),解压缩至独立的文件夹,进入其中的bin目录,执行以下命令: 复制代码 代码如下: openssl ...
php 怎么生成rsa加密的公钥和私钥附上出处链接:
四,用PHP生成密钥
PEAR::Crypt_RSA的Crypt_RSA_KeyPair类可以生成密钥。调用步骤如下:
require_once('Crypt/RSA.php');
$math_obj = Crypt_RSA_MathLoader::loadWrapper();
$key_pair = new Crypt_RSA_KeyPair($key_lenth);
if (!$key_pair->isError()){
$public_key = $key_pair->getPublicKey();
$private_key = $key_pair->getPrivateKey();
$e =$math_obj->hexstr($math_obj->bin2int($public_key->getExponent()));
$d =$math_obj->hexstr($math_obj->bin2int($private_key->getExponent()));
$n =$math_obj->hexstr($math_obj->bin2int($public_key->getModulus()));
}
hexstr()是自己添加的函数,用来把十进制字符串转换为十六进制。对Crypt_RSA_Math_GMP很简单,只需:
function hexstr($num){
return gmp_strval($num,16);
}
对Crypt_RSA_Math_BCMath略麻烦些:
function hexstr($num){
$result = '';
do{
$result = sprintf('%02x',intval(bcmod($num,256))).$result;
$num = bcdiv($num, 256);
}while(bccomp($num, 0));
return ltrim($result,'0');
}
五,用php生成密钥(二)
为了提高加密速度,一般选一个较小的e。比较常用的是3、17、257、65537几个素数。
generate()生成密钥的算法是依次计算p,q,n,e,d。因此做了如下改动,以便可以自己选e值:
原来的:
function Crypt_RSA_KeyPair($key_len, $wrapper_name = 'default', $error_handler = '')
改后增加一个参数e:
function Crypt_RSA_KeyPair($key_len, $e = null, $wrapper_name = 'default', $error_handler = '')
这个函数调用generate()。效应地:
function generate($key_len = null)
也增加一个参数e:
function generate($key_len = null, $e = null)
把CRYPT_RSA-1.0.0的KeyPair.php中属于generate()的245~271行改动顺序,由e确定p和q:
if($e != null$this->_math_obj->cmpAbs($e,2)>0)
$e = $this->_math_obj->nextPrime($this->_math_obj->dec($e));//取个素数
else
{
while(true)
{
$e = $this->_math_obj->getRand($q_len, $this->_random_generator);
if ($this->_math_obj->cmpAbs($e,2)<=0)
continue;
$e = $this->_math_obj->nextPrime($this->_math_obj->dec($e));
break;
}
}
do{
$p = $this->_math_obj->getRand($p_len, $this->_random_generator, true);
$p = $this->_math_obj->nextPrime($p);
do{
do{
$q = $this->_math_obj->getRand($q_len, $this->_random_generator, true);
$tmp_len = $this->_math_obj->bitLen($this->_math_obj->mul($p, $q));
if ($tmp_len < $key_len)
$q_len++;
elseif ($tmp_len > $key_len)
$q_len--;
} while ($tmp_len != $key_len);
$q = $this->_math_obj->nextPrime($q);
$tmp = $this->_math_obj->mul($p, $q);
} while ($this->_math_obj->bitLen($tmp) != $key_len);
// $n - is shared modulus
$n = $this->_math_obj->mul($p, $q);
// generate public ($e) and private ($d) keys
$pq = $this->_math_obj->mul($this->_math_obj->dec($p), $this->_math_obj->dec($q));
if($this->_math_obj->isZero($this->_math_obj->dec($this->_math_obj->gcd($e, $pq))))
break;
}while(true);
(网易的服务真体贴啊,连pre标记里面的东西都给改。还改不好)这样,如果要生成e为3的1024位密钥,可以如下调用:
$key_pair = new Crypt_RSA_KeyPair(1024,3);
六,干什么用
加密比较重要的数据。比如注册时用户输入的密码。
登录时把密码hmac一下就可以防止重放攻击(replay attack)了。对注册不存在这种攻击,但有密码泄露的危险。上传密码hash那点安全性根本不算什么。这个可以用RSA加密解决。
不过,对中间人攻击还是没办法。
另外一个
关于php公钥转换的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php公钥转换 php转golang的详细内容...