0

我正在使用带有 PHPSECLIB 和 VB.NET 的 RSA 加密。PHP的代码是:

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

$rsa = new Crypt_RSA();
$key='-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0
FPqri0cb2JZfXJ/DgYSF6vUpwmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/
3j+skZ6UtW+5u09lHNsj6tQ51s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQAB
-----END PUBLIC KEY-----';
$rsa->loadKey($key); // public key

$plaintext = 'HELLO';
//$rsa->setEncryptionMode(CRYPT_RSA_PUBLIC_FORMAT_PKCS1);
$rsa->paddable = false;
$ciphertext = $rsa->encrypt($plaintext);

echo base64_encode($ciphertext);
?>

VB.NET 代码:

  Public Function DecryptText(ByVal Str As String) As String
        Try

            'Convert data to byte array
            Dim Enc As Encoding = Encoding.UTF8
            Dim dataToDecrypt() As Byte = Convert.FromBase64String(Str)


            'Make our RSA Container
            Dim RSA As New RSACryptoServiceProvider

            'Import PRIVATE key into container
            RSA.FromXmlString("<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>")

            'Decrypt the data
            Dim decryptedData() As Byte = RSA.Decrypt(dataToDecrypt, False)

            'Convert output byte array to a string
            DecryptText = Enc.GetString(decryptedData)



        Catch ex As Exception
            Return ""

        End Try
    End Function

我遇到的问题是,每次我从 PHP 复制 Base64 输出并将其子传递到 VB.NET DecryptText 函数时,我都会得到一个空白结果,并显示错误“错误数据”。有人可以在这里指出我正确的方向吗?

4

1 回答 1

1

我强烈怀疑$rsa->setEncryptionMode(CRYPT_RSA_PUBLIC_FORMAT_PKCS1);需要取消注释。

另外,$rsa->paddable = false......这没有做任何事情。paddable 不是 Crypt_RSA 使用的变量。Crypt_Base定义它但Crypt_RSA不扩展Crypt_Base.

于 2013-07-07T22:38:13.950 回答