在 php 中使用 RSA 的一个很好的例子

发布于 2024-12-12 04:15:25 字数 3327 浏览 0 评论 0原文

我正在尝试找出一种在 php 中使用 RSA 的方法。

我已经生成了公钥和私钥

ssh-keygen -t rsa

,现在我尝试使用我的 php 代码中的公钥和私钥。这是我正在使用的代码:

<?php
$string=$_GET["i"];
//$fp=fopen ("public.pem","r");
$pub_key='ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAtTLDrnS+MyPb/MGHzgcS90DgaoPiDNattlL0Xo7keAjfqz5/yCXU0L8ilkAWIwME5DdXK04MskRtupYPwxPGB4Y7bG/4wvArx5FEgDSq6FUMNvQZ/Vqj13uvcjadLctC2ypAUPRo39qO1IGLEDEWNOS49UgLxzfnr1dZF/Ox5m5UjhOjsALcTiv4eR6ClGcCrAHE7Jp5nL2BwVu46nx3eFlpe7cljQVUOQDgUa5yWaRoJE6qcYgQu1TDo6b/pcAWU1WVu+HzJY4g0aoV2VqamCnl3oyWoPz7ykvxbJbHv7/qTbV+IBNzSx4R7KSppvnPKsrbeiWROvZ4JF84leX+Iw== root@ip-10-83-11-72';

//fclose($fp);
$PK="";
$PK=openssl_get_publickey($pub_key);
if (!$PK) {
    echo "Cannot get public key";
    echo $pub_key;
}
$finaltext="";
openssl_public_encrypt($string,$finaltext,$PK);
if (!empty($finaltext)) {
    openssl_free_key($PK);
    echo "Encryption OK!";
    echo $finaltext;
    echo "<br/><br/><br/>";
}else{
    echo "Cannot Encrypt";
}

// STEP 2: Decription (Using Private Key)

//$fp=fopen ("private.pem","r");
$priv_key2='-----BEGIN RSA PRIVATE KEY-----
MIIEoAIBAAKCAQEAtTLDrnS+MyPb/MGHzgcS90DgaoPiDNattlL0Xo7keAjfqz5/
yCXU0L8ilkAWIwME5DdXK04MskRtupYPwxPGB4Y7bG/4wvArx5FEgDSq6FUMNvQZ
/Vqj13uvcjadLctC2ypAUPRo39qO1IGLEDEWNOS49UgLxzfnr1dZF/Ox5m5UjhOj
sALcTiv4eR6ClGcCrAH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2bmBCgTmpa7W10irx
P0hPIQRSOEAkoWNGaebX9zbM6En3K2ly3tgyg8eEMkSu0zg4Ck5g2XvQzYnfpleS
ShT+cXeG+yl6ijZeNmJB+LDYhC0bpwkfbE97AtQoPW5DnLRVQI9uanJiiix1PvHr
iQKBgBj/RiXsDHvVJ28xZPUffZntrAD/TWXMHZWVMFgfsf8Ti0O7ypXqY43+Aoph
nfDH6xJFqU9azsiv5xQVY7X1h4+DuOamqdawIV5LtYod+ip0dxduQMfrqLld/p0m
E+jAaxvC4b6zqomFnZ9MB9eAzUg9i8UQkc4H3cH/bO6MKPG3
-----END RSA PRIVATE KEY-----
';//fread ($fp,8192);
//fclose($fp);
$PK2=openssl_get_privatekey($priv_key2);
$Crypted=openssl_private_decrypt($Data,$Decrypted,$PK2);
if (!$Crypted) {
    $MSG.="<p class='error'>Cannot Decrypt ($CCID).</p>";
}else{
    echo "Decrypted Data: " . $Decrypted;
}

最后,应用程序崩溃了:

Cannot get public key
Warning: openssl_public_encrypt() [function.openssl-public-encrypt]: key parameter is not a valid public key in /home/menkaur/public_html/iathao.com/bookmarks/lcn.php on line 13
Cannot Encrypt

我做错了什么?

I'm trying to figure out a way to use RSA in php.

I've generated public and private keys with

ssh-keygen -t rsa

And now I'm trying to use those from my php code. Here's the code I'm using:

<?php
$string=$_GET["i"];
//$fp=fopen ("public.pem","r");
$pub_key='ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAtTLDrnS+MyPb/MGHzgcS90DgaoPiDNattlL0Xo7keAjfqz5/yCXU0L8ilkAWIwME5DdXK04MskRtupYPwxPGB4Y7bG/4wvArx5FEgDSq6FUMNvQZ/Vqj13uvcjadLctC2ypAUPRo39qO1IGLEDEWNOS49UgLxzfnr1dZF/Ox5m5UjhOjsALcTiv4eR6ClGcCrAHE7Jp5nL2BwVu46nx3eFlpe7cljQVUOQDgUa5yWaRoJE6qcYgQu1TDo6b/pcAWU1WVu+HzJY4g0aoV2VqamCnl3oyWoPz7ykvxbJbHv7/qTbV+IBNzSx4R7KSppvnPKsrbeiWROvZ4JF84leX+Iw== root@ip-10-83-11-72';

//fclose($fp);
$PK="";
$PK=openssl_get_publickey($pub_key);
if (!$PK) {
    echo "Cannot get public key";
    echo $pub_key;
}
$finaltext="";
openssl_public_encrypt($string,$finaltext,$PK);
if (!empty($finaltext)) {
    openssl_free_key($PK);
    echo "Encryption OK!";
    echo $finaltext;
    echo "<br/><br/><br/>";
}else{
    echo "Cannot Encrypt";
}

// STEP 2: Decription (Using Private Key)

//$fp=fopen ("private.pem","r");
$priv_key2='-----BEGIN RSA PRIVATE KEY-----
MIIEoAIBAAKCAQEAtTLDrnS+MyPb/MGHzgcS90DgaoPiDNattlL0Xo7keAjfqz5/
yCXU0L8ilkAWIwME5DdXK04MskRtupYPwxPGB4Y7bG/4wvArx5FEgDSq6FUMNvQZ
/Vqj13uvcjadLctC2ypAUPRo39qO1IGLEDEWNOS49UgLxzfnr1dZF/Ox5m5UjhOj
sALcTiv4eR6ClGcCrAHE7Jp5nL2BwVu46nx3eFlpe7cljQVUOQDgUa5yWaRoJE6q
cYgQu1TDo6b/pcAWU1WVu+HzJY4g0aoV2VqamCnl3oyWoPz7ykvxbJbHv7/qTbV+
IBNzSx4R7KSppvnPKsrbeiWROvZ4JF84leX+IwIBIwKCAQAparBiY9Oz6vB0SX4g
dqU/1FCNX/kngYbKlp5Bf78xYR0udK9+NIhqOlEMZmt1t4wW6BPspCAovy8GE6vU
0VHV1Yns5mS+189+EpNQgREfKWHgrNK9kQ+Btd71iNN4LnWucA6zaxCoMfTC28gD
s3LKQuhylB/zBXbI/gW8VPV2fzvFd35iA6QqNKWbjkaRd2y0TOv+RAcnCrYFwV5O
LscgjUNS2Hk8dlyXoneB8SqxiFzrXgSTihUelsoPQCj7mZLZ87UFF6sLU85gxoNT
tw0VoKD3Ve4poDEkWH28v39MCA/ZOPtNu68oimgVxRWyswvr2X9UvQ8joy2QaQZs
0mRLAoGBAN+dc6J8BgzmCr1yUQejU9nPAIFseH0Y+Jd/kHMV0CcSTnJwEak+6H5x
aTVQVNVMxdBd3xLG6GLYUWz01ptj3ubkO3qisqeVWvXhp7thHBaj640jV33jUY8p
eSrYVqPTDXGb+Jq56Tu0y+9sZeqXJBDeu2vLi7wlK2iAjQ9GHHYdAoGBAM9wtThH
LOEveuZ+PwY0qK1IMLsHuF049g3onUk+n6k3oPCBMmnllwCwMOAo2oJEKgFAtEOl
I5F9L6KsF7D1IUz1j+Mi578+pIQIndvp/aq1n5iBHSbHjpWhz6B6elcADzNbY5fy
FRlE9JshAIAiBmokN+Kb3rLZ4ShMBq0zsRE/AoGAOYBCTlpn9LAupb5PWbxIwvq2
/LV+EYoUCbMWg/5Ld8Lg+Nr9OiYehun2e2xtle8rjVn3iHxKYo9lZSkD/BJd45nG
JtlhI8dR53SKN39e/oHsHPp1lWZWzQqqIPXNIs/e4rMUCoeTvuVZAw0+x0tvrJEL
n2A53+xM/Z1mGd7UHl8CgYARx9UE0uaW9XDvL2R83/EzbJZ2bmBCgTmpa7W10irx
P0hPIQRSOEAkoWNGaebX9zbM6En3K2ly3tgyg8eEMkSu0zg4Ck5g2XvQzYnfpleS
ShT+cXeG+yl6ijZeNmJB+LDYhC0bpwkfbE97AtQoPW5DnLRVQI9uanJiiix1PvHr
iQKBgBj/RiXsDHvVJ28xZPUffZntrAD/TWXMHZWVMFgfsf8Ti0O7ypXqY43+Aoph
nfDH6xJFqU9azsiv5xQVY7X1h4+DuOamqdawIV5LtYod+ip0dxduQMfrqLld/p0m
E+jAaxvC4b6zqomFnZ9MB9eAzUg9i8UQkc4H3cH/bO6MKPG3
-----END RSA PRIVATE KEY-----
';//fread ($fp,8192);
//fclose($fp);
$PK2=openssl_get_privatekey($priv_key2);
$Crypted=openssl_private_decrypt($Data,$Decrypted,$PK2);
if (!$Crypted) {
    $MSG.="<p class='error'>Cannot Decrypt ($CCID).</p>";
}else{
    echo "Decrypted Data: " . $Decrypted;
}

At the end, the app crashes with:

Cannot get public key
Warning: openssl_public_encrypt() [function.openssl-public-encrypt]: key parameter is not a valid public key in /home/menkaur/public_html/iathao.com/bookmarks/lcn.php on line 13
Cannot Encrypt

what am I doing wrong?

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

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

发布评论

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

评论(1

无语# 2024-12-19 04:15:25

openssl 使用的密钥是 PEM 格式。要生成它,请使用以下代码:

openssl genrsa -des3 -out private.pem 1024

它将生成一个名为“private.pem”的文件。然后您可以使用以下方法生成公钥:

openssl rsa -in private.pem -out public.pem -outform PEM -pubout

在 PHP 中,您可以按如下方式使用这些密钥:

<?php

    $pub = file_get_contents('public.pem');
    $pk  = openssl_get_publickey($pub);
    if(!$pk) ...

然后使用您的逻辑执行您想要的操作。

The key that openssl uses is in PEM format. To generate it use the following code:

openssl genrsa -des3 -out private.pem 1024

It will generate a file called "private.pem". Then you can generate the public key using:

openssl rsa -in private.pem -out public.pem -outform PEM -pubout

In PHP you can use these keys as follows:

<?php

    $pub = file_get_contents('public.pem');
    $pk  = openssl_get_publickey($pub);
    if(!$pk) ...

Then use your logic to do whatever you want.

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