我正在使用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函数时,我得到一个空白的结果,错误为“Bad Data”。有人能指出我在正确的方向吗?
答案 0 :(得分:1)
我强烈怀疑$rsa->setEncryptionMode(CRYPT_RSA_PUBLIC_FORMAT_PKCS1);
需要取消注释。
此外,$rsa->paddable = false
......没有做任何事情。 paddable不是Crypt_RSA使用的变量。 Crypt_Base
定义了它,但Crypt_RSA
未扩展Crypt_Base
。