1

我使用带有公钥的 pidcrypt-rsa在客户端加密数据。我想在服务器端使用 php 解密它,我使用phpseclib,但出现以下错误:

第 2503 行 C:\xampp*\Crypt\phpseclib0.3.8\Crypt\RSA.php 中的解密错误

这是我的代码:

<?php
include 'Crypt/phpseclib0.3.8/Crypt/RSA.php';
....
$privateKey="-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUp
wmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ5
1s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQABAoGAFijko56+qGyN8M0RVyaRAXz++xTqHBLh
3tx4VgMtrQ+WEgCjhoTwo23KMBAuJGSYnRmoBZM3lMfTKevIkAidPEcvYCdm5dYq3XToLkkLv5L2
pIIVOFMDG+KESnAFV7l2c+cnzRMW0+b6f8mR1CJzZuxVLL6Q02fvLcs55/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-----";
$rsa = new Crypt_RSA();
$rsa->loadKey($privateKey); // private key
echo $rsa->decrypt($ciphertext);
?>

编辑1:

用于加密的javascript代码:

function Encrypt(value) {
        var public_key  = '-----BEGIN PUBLIC KEY-----\n\
MIGfMA0GCSqGSIb4DQEBAQUAA4GNADCBiQKBgQDVd/gb2ORdLI7nTRHJR8C5EHs4\n\
RkRBcQuQdHkZ6eq0xnV2f0hkWC8h0mYH/bmelb5ribwulMwzFkuktXoufqzoft6Q\n\
6jLQRnkNJGRP6yA4bXqofKYj1yeMusIPyIb3CTJT/gfZ40oli6szwu4DoFs66IZp\n\
JLv4qxU9hqu6NtJ+8QIDAQAB\n\
-----END PUBLIC KEY-----';
        params = certParser(public_key);
        if(params.b64){
        var key = pidCryptUtil.decodeBase64(params.b64);
        //new RSA instance
        var rsa = new pidCrypt.RSA();
        //RSA encryption
        //ASN1 parsing
        var asn = pidCrypt.ASN1.decode(pidCryptUtil.toByteArray(key));
        var tree = asn.toHexTree();
        //setting the public key for encryption
        rsa.setPublicKeyFromASN(tree);
        crypted = rsa.encrypt(value);
        return pidCryptUtil.fragment(pidCryptUtil.encodeBase64(pidCryptUtil.convertFromHex(crypted)),64);
        }
        return 'error';
        }

EDIT2:我编辑我的代码:

$rsa = new Crypt_RSA();
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$rsa->loadKey($privateKey); // private key
echo $rsa->decrypt(base64_decode($ciphertext));

编辑3:

我更新了我的php代码,我没有再收到错误,但是解密后它什么也没返回,这是什么问题?

$rsa = new Crypt_RSA();
        $private="<RSAKeyValue>
          <Modulus>AKoYq6Q7UN7vOFmPr4fSq2NORXHBMKm8p7h4JnQU+quLRxvYll9cn8OBhIXq9SnCYkbzBVBkqN4ZyMM4vlSWy66wWdwLNYFDtEo1RJ6yZBExIaRVvX/eP6yRnpS1b7m7T2Uc2yPq1DnWzVI+sIGR51s1/ROnQZswkPJHh71PThln</Modulus>
          <Exponent>AQAB</Exponent>
          <P>AN4DDp+IhBca6QEjh4xlm3iexzLajXYrJid6vdWmh4T42nar5nem8Ax39o3ND9b1Zoj41F9zFQmuZ8/AgabreKU=</P>
          <Q>AMQi+R0G9m0K+AcqK3DFpv4RD9jGc0Tle98heNYT7EQvZuuiq4XjvRz0ybqN//bOafrKhsTpRS9DQ7eEpKLI4Bs=</Q>
          <DP>FklyR1uZ/wPJjj611cdBcztlPdqoxssQGnh85BzCj/u3WqBpE2vjvyyvyI5kX6zk7S0ljKtt2jny2+00VsBerQ==</DP>
          <DQ>AJGC1Mg5Oydo5NwD6BiROrPxGo2bpTbu/fhrT8ebHkTz2eplU9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhM=</DQ>
          <InverseQ>EaiK5KhKNp9SFXuLVwQalvzyHk0FhnNZcZnfuwnlCxb6wnKg117fEfy91eHNTt5PzYPpf+xzD1FnP7/qsIninQ==</InverseQ>
          <D>Fijko56+qGyN8M0RVyaRAXz++xTqHBLh3tx4VgMtrQ+WEgCjhoTwo23KMBAuJGSYnRmoBZM3lMfTKevIkAidPExvYCdm5dYq3XToLkkLv5L2pIIVOFMDG+KESnAFV7l2c+cnzRMW0+b6f8mR1CJzZuxVLL6Q02fvLi55/mbSYxE=</D>
        </RSAKeyValue>";    // private key paste here

        $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);

        $rsa->loadKey($private, CRYPT_RSA_PRIVATE_FORMAT_XML);

        $de =  $rsa->decrypt(base64_decode($ciphertext));
        echo $de;

编辑4:

解密后什么都不返回的原因:jquery选择错误,没有选择任何内容,因此加密的文本为空,导致衰减的文本为空!

4

1 回答 1

1

在 phpseclib 中,您需要执行$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);并且还需要base64_decode数据(如果还没有的话)。

https://www.pidder.de/pidcrypt/?page=demo_rsa-encryption的示例中,数据是 base64 编码的,因此我提到了。

于 2014-10-30T21:07:34.303 回答