0

感谢您的关注。我最近一直在将我在服务器上设置的旧的基于对称的加密系统更新为基于非对称的 RSA 系统,以获得更好的安全性。我认为自己是一个熟练的 PHP 程序员。话虽如此,我对使用 phpseclib 的这个问题完全感到困惑。

似乎我无法在脚本中的任何位置创建该类的两个实例。请参阅下面的代码:

<?php include_once('Crypt/RSA.php');

        function signRSA($string_to_sign){
        $rsa = new Crypt_RSA();
        $rsa->loadKey(file_get_contents('pvk.txt')); // private key
        $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
        $signature = $rsa->sign($string_to_sign);
        return base64_encode($signature);
        }

        function decryptRSA($string_to_decrypt){
        $rsa = new Crypt_RSA();
        $rsa->loadKey(file_get_contents('pvk.txt')); // private key
        return $rsa->decrypt(base64_decode($string_to_decrypt));
        }

    //Which ever function is on top is the only one that appears to work. The first line below will work, however the second will fail returning me a null value.

    $sig = signRSA($_POST['data']);
    $plain = decryptRSA($_POST['otherdata']);
?>

我也尝试过创建一个单独的“全局”变量。同样的事情发生了。。

我期待着解决这个问题。

更新:

我能够成功地解决我的问题,尽管与我的想法无关。看起来我的客户端应用程序(C#)和上面看到的 PHP 端使用了两种不同的填充算法。这导致客户端应用程序认为数据已损坏。在 PHP 脚本的逻辑中,这仅导致上述功能之一起作用。

4

1 回答 1

0

这对我来说可以:

include_once('Crypt/RSA.php');

        function signRSA($string_to_sign){
        $rsa = new Crypt_RSA();
        $rsa->loadKey('-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUp
wmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ5
1s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQABAoGAFijko56+qGyN8M0RVyaRAXz++xTqHBLh
3tx4VgMtrQ+WEgCjhoTwo23KMBAuJGSYnRmoBZM3lMfTKevIkAidPExvYCdm5dYq3XToLkkLv5L2
pIIVOFMDG+KESnAFV7l2c+cnzRMW0+b6f8mR1CJzZuxVLL6Q02fvLi55/mbSYxECQQDeAw6fiIQX
GukBI4eMZZt4nscy2o12KyYner3VpoeE+Np2q+Z3pvAMd/aNzQ/W9WaI+NRfcxUJrmfPwIGm63il
AkEAxCL5HQb2bQr4ByorcMWm/hEP2MZzROV73yF41hPsRC9m66KrheO9HPTJuo3/9s5p+sqGxOlF
L0NDt4SkosjgGwJAFklyR1uZ/wPJjj611cdBcztlPdqoxssQGnh85BzCj/u3WqBpE2vjvyyvyI5k
X6zk7S0ljKtt2jny2+00VsBerQJBAJGC1Mg5Oydo5NwD6BiROrPxGo2bpTbu/fhrT8ebHkTz2epl
U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ
37sJ5QsW+sJyoNde3xH8vdXhzU7eT82D6X/scw9RZz+/6rCJ4p0=
-----END RSA PRIVATE KEY-----'); // private key
        $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
        $signature = $rsa->sign($code);
        return base64_encode($signature);
        }

        function decryptRSA($string_to_decrypt){
        $rsa = new Crypt_RSA();
        $rsa->loadKey('-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUp
wmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ5
1s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQABAoGAFijko56+qGyN8M0RVyaRAXz++xTqHBLh
3tx4VgMtrQ+WEgCjhoTwo23KMBAuJGSYnRmoBZM3lMfTKevIkAidPExvYCdm5dYq3XToLkkLv5L2
pIIVOFMDG+KESnAFV7l2c+cnzRMW0+b6f8mR1CJzZuxVLL6Q02fvLi55/mbSYxECQQDeAw6fiIQX
GukBI4eMZZt4nscy2o12KyYner3VpoeE+Np2q+Z3pvAMd/aNzQ/W9WaI+NRfcxUJrmfPwIGm63il
AkEAxCL5HQb2bQr4ByorcMWm/hEP2MZzROV73yF41hPsRC9m66KrheO9HPTJuo3/9s5p+sqGxOlF
L0NDt4SkosjgGwJAFklyR1uZ/wPJjj611cdBcztlPdqoxssQGnh85BzCj/u3WqBpE2vjvyyvyI5k
X6zk7S0ljKtt2jny2+00VsBerQJBAJGC1Mg5Oydo5NwD6BiROrPxGo2bpTbu/fhrT8ebHkTz2epl
U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ
37sJ5QsW+sJyoNde3xH8vdXhzU7eT82D6X/scw9RZz+/6rCJ4p0=
-----END RSA PRIVATE KEY-----'); // private key
        return $rsa->decrypt(base64_decode($string_to_decrypt));
        }

    //Which ever function is on top is the only one that appears to work. The first line below will work, however the second will fail returning me a null value.

    $sig = signRSA('zzzzzzzzz');
    $plain = decryptRSA('G0KqVsNP6qsjVkKbnfCkSsiUdDnxBzIjA1tNHCeeKgkzqdkJF/zFuzVdtc8PrEfeR+SfrvdLiR5DqlAA
LReLD5lPrj19+3Ci+igCQebItT8xmOHmKNHM+nQK2l6uL9AW03Vy/15sCTHqZ/dfmQMarzIb05DQUX57
ftrBGu0XLwM=');

echo base64_encode($sig) . "\r\n";
echo $plain;

也许您的密钥格式不受支持?也许您尝试解密的密文不与私钥一起使用?我发布的代码片段对您有用吗?如果是这样,您将需要发布更多信息。比如填写密钥和明文等等。

于 2013-10-12T19:16:03.757 回答