2013-07-06 371 views
0

我對PHPSECLIB和VB.NET使用RSA加密。 PHP的代碼是:PHPSecLIB加密字符串和VB.NET解密字符串

<?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」。有人可以在這裏指出我正確的方向嗎?

回答

1

我強烈懷疑$rsa->setEncryptionMode(CRYPT_RSA_PUBLIC_FORMAT_PKCS1);需要取消註釋。

另外,$rsa->paddable = false ......這沒什麼用。 paddable不是Crypt_RSA使用的變量。 Crypt_Base定義它,但Crypt_RSA不延伸Crypt_Base