我使用OpenSSL和PHP生成PEM格式的公钥/私钥。VB.NET和PHP之间RSA的互操作性
在PHP创建公共/私有密钥:
// generate a 1024 bit rsa private key
$privateKey = openssl_pkey_new(array(
'private_key_bits' => 1024,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
));
// Save Private key
openssl_pkey_export_to_file($privateKey, 'privateKey');
// get the public key
$keyDetails = openssl_pkey_get_details($privateKey);
// Save Public key
file_put_contents('publicKey', $keyDetails['key']);
在VB.NET代码我这个:
'Public Key
Dim sReader As String ="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCxYT5RaHelEBmk4Z7ppiVaPPBns/36sdY12F/AXETJVl2SYkjc672JMz ..... zQwIDAQAB"
Dim PublicKey As Byte() = Encoding.UTF8.GetBytes(sReader)
Dim Exponent As Byte() = {1, 0, 1}
'Create a new instance of RSACryptoServiceProvider.
Dim RSA As New RSACryptoServiceProvider()
'Create a new instance of RSAParameters.
Dim RSAKeyInfo As New RSAParameters()
'Set RSAKeyInfo to the public key values.
RSAKeyInfo.Modulus = PublicKey
RSAKeyInfo.Exponent = Exponent
'Import key parameters into RSA.
RSA.ImportParameters(RSAKeyInfo)
'Create a UnicodeEncoder to convert between byte array and string.
Dim ByteConverter2 As New UnicodeEncoding()
'Create byte arrays to hold original, encrypted, and decrypted data.
bytPlainText = Encoding.UTF8.GetBytes("Data to Encrypt")
bytCipherText = RSA.Encrypt(bytPlainText, False)
Dim sEncrypt99 As String = Convert.ToBase64String(bytCipherText)
,但我不能”解密 “sEncrypt99” PHP中的私钥
对于我在“$ encryptedData1”中复制“sEncrypt99”的测试
<?php
$privateKey = openssl_pkey_get_private('file://privateKey');
openssl_private_decrypt(base64_decode($encryptedData1), $sensitiveData2, $privateKey);
echo "sensitiveData = " . $sensitiveData2 . "<br>";
?>
没有错误,$ sensitiveData2为空。奇怪.... 问题在哪里?
REM:借口,我的英语不好,我是法国人;)
你可以发布一些代码来解释吗? – LeMoussel 2011-09-13 05:08:46