I've got a private key stored on my PHP server. A message is encrypted using the private key on the PHP server. My C# code is meant to receive the ciphertext from PHP's response and decrypt it with the public key. However, I keep running into errors such as
Cryptography.CryptoThrowHelper.WindowsCryptographicException: 'Key does not exist.'
My PHP code is as follows:
$pkey = openssl_pkey_get_private("file://pkey");
openssl_private_encrypt($text, $ciphertext, $pkey, OPENSSL_PKCS1_PADDING);
echo base64_encode($ciphertext);
My C# code includes an implementation of the function mentioned in this thread, where the answer has a comment linking to Github. A snippet is below:
string pubkey = "M...Q==";
RSA rsa = CreateRsaProviderFromPublicKey(pubkey);
byte[] bytes = Convert.FromBase64String(dataEncrypted);
byte[] decryptedBytes = rsa.Decrypt(bytes, RSAEncryptionPadding.Pkcs1);
return Encoding.UTF8.GetString(decryptedBytes);
The error occurs on the line where rsa.Decrypt is being called. I obtained the public key from the PHP function openssl_pkey_get_details, where I then stripped the ----BEGIN PUBLIC KEY----- header and footer and removed all spaces.
Edit 1: Perhaps I should post the code I used to make the private key, since that could be problematic:
$config = array(
"digest_alg" => "sha512",
"private_key_bits" => 4096,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
$res = openssl_pkey_new($config);
Edit 2: I tried using sha256 with 2048 bytes for a new key, it made no difference.
Let me please explain the complete workflow for RSA encryption with 3 small, simple programs. I'm running all programs in Online compiler so you can easily run the codes directly. The key pair is a 4096 bit long RSA one, the online generation will take some seconds and the encryption padding is OAEP for security reasons.
Security warning: all programs run without any exception handling and are for educational purpose only.
Step 1: Key generation
As the encryption is done with the public key of the recipient of the message the recipient has to generate the RSA key pair. As per your definition the PHP-side is the sender and the C#-side is the recipient let's generate the key pair in C#.
The first code will generate a 4096 bit long RSA key pair in XML-encoding - that's the default output encoding of C#:
privateKeyXML: <RSAKeyValue><Modulus>u0pkuoAjII9KQ9OgEILB6MnGBAedO/Ho+O69W5USpQc+pDGxQbqcC9PKKQk4dymGnS70HVswHVUkdiLcEIMCjU8jdXFnOfh5SPKfR9bgmRQ3HhEaY5ejcCerCW96J5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v21Xckbkzo17HZrN+G9C5D1KEeQkdKdg8WVR+3XfhiFLj/S6ANmNjCNbcrR8GKY1GjZahDUyddEza76RuJ3Wqyle0BmNT0RoSHGvjBP+LlY3aOqzsU2sqnzGnItRzr2HnQwVV10MMFo3fbuWopzXqzVjQbnxJM9G79l20Np2eFLDVp4zpiYrGHWSFOnfIDqmy5ke57WnISJwnPz6A+ByhYVJfaxohkk+csbaPCsILQ0ZCzTnCcoh9svw9AIvlHn5YyAa8Gl5o6Ub8lsJXOSftGVtcCsWRiLbD1pg1CTQmoKPPxUFsaU9iPXrfb48+5hFFOVe6+22S8YShFl/HaRq8TMESXmCiRgbgzBV7zmABkJFtH6WUBkXqBTSlUoEsYUHIV61vptroHHui/aVxnynvtkJqTTfto2ukq5ZzpDmQeDE075kM/rOyUuUiJBQ7eszrXKTDKW2xqc6q/OQMfjEiPdtl1Rc44obdRGaTmJhi9h5a/SqH6KPDVoaR+hwLXggnLERroyogElyjbzrkfD5nCE=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>
Step 2: RSA encryption on PHP-side with public key
As we are using the PHP-built-in OpenSSL-engine we need to convert the XML-encoded public key to a PEM encoding first. As it is the public key I'm using an
online service for this task - there are many other options but for the sake of demonstration it's okay. Go to https://superdry.apphb.com/tools/online-rsa-key-converter and copy/paste the publicKeyXML to the first inputbox and press the button "Convert". In the outputbox below you will find this Public key:
-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAu0pkuoAjII9KQ9OgEILB
6MnGBAedO/Ho+O69W5USpQc+pDGxQbqcC9PKKQk4dymGnS70HVswHVUkdiLcEIMC
jU8jdXFnOfh5SPKfR9bgmRQ3HhEaY5ejcCerCW96J5leNEcfkMpWjXy+hvzt0Whn
v21Xckbkzo17HZrN+G9C5D1KEeQkdKdg8WVR+3XfhiFLj/S6ANmNjCNbcrR8GKY1
GjZahDUyddEza76RuJ3Wqyle0BmNT0RoSHGvjBP+LlY3aOqzsU2sqnzGnItRzr2H
nQwVV10MMFo3fbuWopzXqzVjQbnxJM9G79l20Np2eFLDVp4zpiYrGHWSFOnfIDqm
y5ke57WnISJwnPz6A+ByhYVJfaxohkk+csbaPCsILQ0ZCzTnCcoh9svw9AIvlHn5
YyAa8Gl5o6Ub8lsJXOSftGVtcCsWRiLbD1pg1CTQmoKPPxUFsaU9iPXrfb48+5hF
FOVe6+22S8YShFl/HaRq8TMESXmCiRgbgzBV7zmABkJFtH6WUBkXqBTSlUoEsYUH
IV61vptroHHui/aVxnynvtkJqTTfto2ukq5ZzpDmQeDE075kM/rOyUuUiJBQ7esz
rXKTDKW2xqc6q/OQMfjEiPdtl1Rc44obdRGaTmJhi9h5a/SqH6KPDVoaR+hwLXgg
nLERroyogElyjbzrkfD5nCECAwEAAQ==
-----END PUBLIC KEY-----
Copy and paste your public key in the second program (function loadRsaPublicKeyPem), let it run and encrypt the data, giving this Base64 encoded output:
RSA 2048 encryption OAEP-padding string
plaintext: The quick brown fox jumps over the lazy dog
* * * encrypt the plaintext with the RSA public key * * *
ciphertextBase64: Rot82ugHFtc3b/JNC+uUYMUSQgdHl4bwcdpLIpY7EJzrI7xwMdzKMoastjc4IFiAcXExC7YmpVkVXsHYVGDbMb+o/IKAp/LyH5SrlQqAJq65ZG7Fe54UgKatdqAIBMQIawiv+3g2YmrstdA2KIjW1+93FeH1vUgqSYxydSvIkngjqE//1Dwa/DY+bFrDfG9B1HtDEDFvAOOZE7qC4I2XYvD3LQVwp4zBzFXSzeJP9/BkksC6CN6caKDRdaQa+/oyRjZXJiFIx4YAhok4aBwg54er+2jEeKHcUM01eZBTCFtrIZF4Yg9GQRkCA89XsZkGypqPVL/HLZXx6yHF3kR3XVvCuKwzX/IyostUNbPeWWAWieUFbBWhdPSrENC//7M8nmE7dvcBbRHpoX4LWmD+kWYAFV3M+5n9SJhzwayI0rvQY9DQ4U5IiaP32TDjqyPyK2gzimpuYRe7KWYCBgCG503XThdWe4vpcDNYOKp0KO5TwHMNMTg/FLbwuQcvcpiT9lQLbr5Pz84swe1olhle0rTlBXOS6ONn5LWfrKYjfwluBYsxF/XtJcg+HytBZbCqMNHTeeS4a40r0XLxH/Gjdvw1zs7APrA9AN2SObthMeIXJlryLSshmoKsgBDCi3JzZ80kO4729SVtH1SbwElkZtJNqD6IilY2c1M6CV94tHg=
Step 3: RSA decryption on C#-side with Private key
Copy and paste your private key in the third program (function loadRsaPrivateKeyXml), copy and paste the ciphertextBase64 and run the program, you
will get this output:
RSA 2048 decryption OAEP-padding string
* * * decrypt the ciphertext with the RSA private key * * *
ciphertextReceivedBase64: Rot82ugHFtc3b/JNC+uUYMUSQgdHl4bwcdpLIpY7EJzrI7xwMdzKMoastjc4IFiAcXExC7YmpVkVXsHYVGDbMb+o/IKAp/LyH5SrlQqAJq65ZG7Fe54UgKatdqAIBMQIawiv+3g2YmrstdA2KIjW1+93FeH1vUgqSYxydSvIkngjqE//1Dwa/DY+bFrDfG9B1HtDEDFvAOOZE7qC4I2XYvD3LQVwp4zBzFXSzeJP9/BkksC6CN6caKDRdaQa+/oyRjZXJiFIx4YAhok4aBwg54er+2jEeKHcUM01eZBTCFtrIZF4Yg9GQRkCA89XsZkGypqPVL/HLZXx6yHF3kR3XVvCuKwzX/IyostUNbPeWWAWieUFbBWhdPSrENC//7M8nmE7dvcBbRHpoX4LWmD+kWYAFV3M+5n9SJhzwayI0rvQY9DQ4U5IiaP32TDjqyPyK2gzimpuYRe7KWYCBgCG503XThdWe4vpcDNYOKp0KO5TwHMNMTg/FLbwuQcvcpiT9lQLbr5Pz84swe1olhle0rTlBXOS6ONn5LWfrKYjfwluBYsxF/XtJcg+HytBZbCqMNHTeeS4a40r0XLxH/Gjdvw1zs7APrA9AN2SObthMeIXJlryLSshmoKsgBDCi3JzZ80kO4729SVtH1SbwElkZtJNqD6IilY2c1M6CV94tHg=
decryptedData: The quick brown fox jumps over the lazy dog
And voila - we receive the original plaintext from PHP-side here on C#. Here are the online-ressources:
program 1 - generate RSA keys on C#-side: https://repl.it/#javacrypto/CsharpGenerateRsaKeypair#main.cs
program 2 - encrypt on PHP-side: https://repl.it/#javacrypto/PhpRsaOaepPaddingEncryption#main.php
program 3 - decrypt on C#-side: https://repl.it/#javacrypto/CsharpRsaOaepPaddingDecryption#main.cs
code program 1:
using System;
using System.Security.Cryptography;
using System.Text;
class RSACSPSample
{
static void Main()
{
// Create a new instance of RSACryptoServiceProvider to generate
// public and private key data.
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(4096);
rsa.PersistKeyInCsp = false;
string privateKeyXML = rsa.ToXmlString(true);
string publicKeyXML = rsa.ToXmlString(false);
Console.WriteLine("privateKeyXML: " + privateKeyXML);
Console.WriteLine("publicKeyXML: " + publicKeyXML);
Console.WriteLine();
}
}
code program 2:
<?php
function rsaEncryptionOaepSha1($publicKey, $plaintext) {
openssl_public_encrypt($plaintext, $ciphertext, $publicKey, OPENSSL_PKCS1_OAEP_PADDING);
return $ciphertext;
}
function base64Encoding($input) {return base64_encode($input);}
function loadRsaPublicKeyPem() {
// this is a sample key - don't worry !
return '
-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAu0pkuoAjII9KQ9OgEILB
6MnGBAedO/Ho+O69W5USpQc+pDGxQbqcC9PKKQk4dymGnS70HVswHVUkdiLcEIMC
jU8jdXFnOfh5SPKfR9bgmRQ3HhEaY5ejcCerCW96J5leNEcfkMpWjXy+hvzt0Whn
v21Xckbkzo17HZrN+G9C5D1KEeQkdKdg8WVR+3XfhiFLj/S6ANmNjCNbcrR8GKY1
GjZahDUyddEza76RuJ3Wqyle0BmNT0RoSHGvjBP+LlY3aOqzsU2sqnzGnItRzr2H
nQwVV10MMFo3fbuWopzXqzVjQbnxJM9G79l20Np2eFLDVp4zpiYrGHWSFOnfIDqm
y5ke57WnISJwnPz6A+ByhYVJfaxohkk+csbaPCsILQ0ZCzTnCcoh9svw9AIvlHn5
YyAa8Gl5o6Ub8lsJXOSftGVtcCsWRiLbD1pg1CTQmoKPPxUFsaU9iPXrfb48+5hF
FOVe6+22S8YShFl/HaRq8TMESXmCiRgbgzBV7zmABkJFtH6WUBkXqBTSlUoEsYUH
IV61vptroHHui/aVxnynvtkJqTTfto2ukq5ZzpDmQeDE075kM/rOyUuUiJBQ7esz
rXKTDKW2xqc6q/OQMfjEiPdtl1Rc44obdRGaTmJhi9h5a/SqH6KPDVoaR+hwLXgg
nLERroyogElyjbzrkfD5nCECAwEAAQ==
-----END PUBLIC KEY-----
';
}
echo 'RSA 2048 encryption OAEP-padding string' . PHP_EOL;
$dataToEncryptString = "The quick brown fox jumps over the lazy dog";
echo 'plaintext: ' . $dataToEncryptString . PHP_EOL;
// encryption
echo PHP_EOL . '* * * encrypt the plaintext with the RSA public key * * *' .PHP_EOL;
$publicKeyLoad = openssl_pkey_get_public(loadRsaPublicKeyPem());
$ciphertextBase64 = base64Encoding(rsaEncryptionOaepSha1($publicKeyLoad, $dataToEncryptString));
echo 'ciphertextBase64: ' . $ciphertextBase64 . PHP_EOL;
// transport the encrypted data to recipient
?>
code program 3:
using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
class RsaDecryptionOaepPadding {
static void Main() {
Console.WriteLine("RSA 2048 decryption OAEP-padding string");
string ciphertextBase64 = "Rot82ugHFtc3b/JNC+uUYMUSQgdHl4bwcdpLIpY7EJzrI7xwMdzKMoastjc4IFiAcXExC7YmpVkVXsHYVGDbMb+o/IKAp/LyH5SrlQqAJq65ZG7Fe54UgKatdqAIBMQIawiv+3g2YmrstdA2KIjW1+93FeH1vUgqSYxydSvIkngjqE//1Dwa/DY+bFrDfG9B1HtDEDFvAOOZE7qC4I2XYvD3LQVwp4zBzFXSzeJP9/BkksC6CN6caKDRdaQa+/oyRjZXJiFIx4YAhok4aBwg54er+2jEeKHcUM01eZBTCFtrIZF4Yg9GQRkCA89XsZkGypqPVL/HLZXx6yHF3kR3XVvCuKwzX/IyostUNbPeWWAWieUFbBWhdPSrENC//7M8nmE7dvcBbRHpoX4LWmD+kWYAFV3M+5n9SJhzwayI0rvQY9DQ4U5IiaP32TDjqyPyK2gzimpuYRe7KWYCBgCG503XThdWe4vpcDNYOKp0KO5TwHMNMTg/FLbwuQcvcpiT9lQLbr5Pz84swe1olhle0rTlBXOS6ONn5LWfrKYjfwluBYsxF/XtJcg+HytBZbCqMNHTeeS4a40r0XLxH/Gjdvw1zs7APrA9AN2SObthMeIXJlryLSshmoKsgBDCi3JzZ80kO4729SVtH1SbwElkZtJNqD6IilY2c1M6CV94tHg=";
try {
// receiving the encrypted data, decryption
Console.WriteLine("\n* * * decrypt the ciphertext with the RSA private key * * *");
string ciphertextReceivedBase64 = ciphertextBase64;
Console.WriteLine("ciphertextReceivedBase64: " + ciphertextReceivedBase64);
string privateKeyLoad = loadRsaPrivateKeyXml();
byte[] ciphertextReceived = Base64Decoding(ciphertextReceivedBase64);
byte[] decryptedtextByte = rsaDecryptionOaep(privateKeyLoad, ciphertextReceived);
Console.WriteLine("decryptedData: " + Encoding.UTF8.GetString(decryptedtextByte, 0, decryptedtextByte.Length));
}
catch(ArgumentNullException) {
Console.WriteLine("The data was not RSA encrypted");
}
}
public static byte[] rsaDecryptionOaep(string privateKeyXml, byte[] ciphertext) {
RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(2048);
RSAalg.PersistKeyInCsp = false;
RSAalg.FromXmlString(privateKeyXml);
return RSAalg.Decrypt(ciphertext, true);
}
static byte[] Base64Decoding(String input) {
return Convert.FromBase64String(input);
}
public static string loadRsaPrivateKeyXml() {
return "<RSAKeyValue><Modulus>u0pkuoAjII9KQ9OgEILB6MnGBAedO/Ho+O69W5USpQc+pDGxQbqcC9PKKQk4dymGnS70HVswHVUkdiLcEIMCjU8jdXFnOfh5SPKfR9bgmRQ3HhEaY5ejcCerCW96J5leNEcfkMpWjXy+hvzt0Whnv21Xckbkzo17HZrN+G9C5D1KEeQkdKdg8WVR+3XfhiFLj/S6ANmNjCNbcrR8GKY1GjZahDU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dzTG6Gqdf/fkUQXOLmH5eaIxwLOJfmjrKHGzyOpbNqYyjjM+N/g3J1vBkDFhdSAibz35neFsJgMyHETSsFaWKZG3PzR80uQ==</InverseQ><D>gDzC1XPxGVUjS2e2XDe7n7GwNIgmH6pWk0OcQf3DQ12/hVtE/DVSrUJ5VDmtAOJc+njwyrzqJ9tpWL4rod6iO7EpHTvlqyq3mLwPVXgKVqYhyyN4xyJH1P5EPPSoVj7L4s2kDlaMhtoX9oNuLojnKIosauGhvs/esXmSd/1udqqEcdWdJIaKeR6f0KZ3wvY/L7UoOna7VW3MKrDrm1EKYQVAWu+1ujfglnBohFkGaDGuapxkRhcXtCqrQSVbT8zWqa6yFL+TbzR6JSu7euwI0/43Cpd6Rg0vm5Z64KK1hNPMP2+ablLCRm3GzeAXZ/F7MG6n/KZ7sYam3Ez56q5eDzO66UL2Ksi81FLTtq/NNegWnELZ79bJy9D1i2R6GmtDG/Lm6BWYHz4m7e7+DMmBJcQbE3cky6iKVyhYVgJ74cREetbXER6Ifriuv9RqumcE1+dN/9yBkinJqa9jFblIGwJMAK9u5fC/Y7Ddba1DS43TYGvnGuYO26QBZPwlyd78t3utHW+Fn38uN/XfYlxMcxlxUHo6eC//zWxRMga+U7AWAZZWLDgHZ+Jp+7cfYypBsIryDiZMUQSkH3o87SMVE9lhT7tO/waJ8mjcdwfAXmCXLhKkFU9CorsU5bIpGkQgGIXNswAGOh8hDvvW1GHLSruGMhoOM9TqHsoO66Ow77E=</D></RSAKeyValue>";
}
}
Credit to Michael Fehr from the comments.
Apparently C# cannot decrypt using a public key, at least with the libraries I'm using. So the solution is to give the C# code the private key and decrypt all messages from the PHP server with the public key.
It's my understanding that this won't be a security issue, despite the fact that the "private" key is being sent into the wild, because both keys are designed for this exact purpose.
Related
I have been trying to generate a RSA key pair with passphrase in PEM format
Nuget-SSHKeygenerator- can generate keys but no symmetric encryption code for private key passphrase
Chilkat - has what I was looking for but paid
Bouncycastle - can generate keys but no symmetric encryption code found so far for private key
(found JceOpenSSLPKCS8EncryptorBuilder java library but the library is not in C#)
RSACryptoServiceProvider - didnt find anything so far. tried CspParameters . doc says it is for with smartcard-based CSPs.
this is how you generate a RSA private key in that is protected with AES-128 encryption. This uses Chilkat library (paid license). I am posting for those who want to use a paid license. I am yet to find a way to write this without a paid license. This generate a private key with openSSH format(.PEM)
static void Main(string[] args)
{
Chilkat.SshKey key = new Chilkat.SshKey();
int numBits;
int exponent;
numBits = 4096; //this is the key size for RSA
exponent = 65537;
success = key.GenerateRsaKey(numBits, exponent);
if (success != true)
string exportedKey;
bool exportEncrypted;
// Export with encryption to OpenSSH private key format:
key.Password = "password1!"; // this is the passphrase
exportEncrypted = true;
exportedKey = key.ToOpenSshPrivateKey(exportEncrypted);
success = key.SaveText(exportedKey, "privkey_openssh_encrypted.pem");
Console.WriteLine("Finished.");
}
I'm trying to decrypt a cipher, which has been encrypted in PHP, using RSA in C#. Here is my C# code:
public byte[] Decrypt(byte[] dataToDecrypt)
{
byte[] plain;
using (var rsa = new RSACryptoServiceProvider(2048))
{
rsa.PersistKeyInCsp = false;
rsa.ImportParameters(PrivateKey);
plain = rsa.Decrypt(dataToDecrypt, false);
}
return plain;
}
And the PHP code is:
public static function encrypt($dataToEncrypt, $userPublicKey) {
if (openssl_public_encrypt($dataToEncrypt, $encrypted, $userPublicKey, OPENSSL_PKCS1_PADDING)) {
return $encrypted;
} else {
throw new Exception('Unable to encrypt data. Perhaps it is bigger than the key size?');
}
}
I get no exceptions or errors in C#. The problem is that if I convert the byte array output of the Decrypt function to a string (i.e. using System.Text.UTF8Encoding.GetString()) the decrypted string is very different from the original plain text passed to 'encrypt' function in PHP.
Any idea why they are different? What am I missing?
p.s. I'd like to add that I have code to encrypt data in C# and decrypt it back in PHP, in a very similar fashion with no problems. I cannot just get it to work the other way around, as I explained above. Thanks
=== UPDATE ===
The input data - i.e. $dataToEncrypt is:
$sessionKey = mcrypt_create_iv(32, MCRYPT_DEV_URANDOM);
The public and private keys are:
-----BEGIN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YtfdhMVN0Po9Z/UCgYBzyomRX/xzuEOiPJIHNKys
8HVliJfrW46RYvthJDh6YqgtuMjLn5kI7MuwPShJ72cuVTFBkRjO/IWVNx/vkU1s
tb2EEf7DbiG4xgPEWaej4QUscqDVJ3shX4ey5pPuEExA3ifuZo+ARAzzA/XEv95e
8LYI617pzasmqMd6DAmg5wKBgQD3SVr6fe0g6xp94CZ+P+6PZ/sVIuBl/UzX9wbL
g1g4QjcDaQ+gXUArseEKPXBjAyrxyYtcH+zZbh4gko88vjFyQuFzj2PhE6xblENw
jvsdNWJwYkhlPJVZJQCEca2gquJUhyFrw/MFFfa/GtaOITi2SfGjfHdo4ZJ0EUTK
QtJRXQKBgQC8nppgSxh0yy0zXOj04URAt2NLBrBWZVh6kHFgaNPIsLNXqyFQQ+Sy
EX/w3HADQI4PRE90iauZ0DYGJyxY1jHqa0X64W/5nZqH/uv58+Hf6uv47l3n3n0T
58WFfKgXY5AaLCbi1UERdeHKPXNXDFkTKuuOb190M+RNbGgzwN9fcQ==
-----END RSA PRIVATE KEY-----
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA498EWxuZK/KsUgIEusEt
QOJulgTHwb8C4avtzJnzhosTeKooXvyGFPpex6HcQGSRqrWpNr2yhw1BvJvH2UyE
Jisl5BJA5Za+ofmbGifCFwCllZ37U1YpOmqpB2Yt+yYElGh5dp+lqs5Q3u3nPknd
nLS3bxH7qlZBvR9YPWj9x7IuSXJyopAmdJato8xeNHzmBxWD8FgQKICFpLtGsPXq
XRwT0imTs6/EcMqq6fdlp0OyBKyZjw6t47gMeqiuSYz6k41Nf/SbtIC4snUyoUgI
TvnHjWe1cY7js4kY62A9ZpHX0NpG7JXctxVb+aZOv1rS36bUjcP+bug1W3ZKrTG6
hwIDAQAB
-----END PUBLIC KEY-----
So for example if the input ($dataToEncrypt) in base64 string is :
VPA13tGd8HMlaB3yKxlFBb8/dAXlCjX5Rr1SzE4L6TY=
I get this output (plain) in base64 string is:
UTgfMQfl82FV4D4HfHPiwmo7jOygBmtXBap0Hn+fAoT4zZcGmLXiY1rGIhmEr90tNxzTGHhknx6PhgJ5eZDK2c9aWi/gD3FJz8MksRjbOkX09GMabcdekdwcyvu9JEWJZi2wOSaRLTRkf90uOArq7pG1g8CPwPLkghj39nUF+XYdGyAB4SXmKkPHshg+dk1acEaXhj9mnAF2fluDEJnfQEU=
I am trying to create an online database application using PHP for the server and C# form application for the client.
On the server I encrypt a simple string using a public RSA key with the PHPSecLib. Then the C# application receives the string and tries to decrypt it using the corresponding private key.
The bytes are base64 encoded on the server and decoded to bytes again by C#. I created the key pair using the PHPSecLib.
This is the code I use on the client application:
public string rsa_decrypt(string encryptedText, string privateKey) {
byte[] bytesToDecrypt = Convert.FromBase64String(encryptedText);
Pkcs1Encoding decrypter = new Pkcs1Encoding(new RsaEngine());
//the error occurs on this line:
AsymmetricCipherKeyPair RSAParams = (AsymmetricCipherKeyPair)new PemReader(new StringReader(privateKey)).ReadObject();
decrypter.Init(false, RSAParams.Private);
byte[] decryptedBytes = decrypter.ProcessBlock(bytesToDecrypt, 0, bytesToDecrypt.Length);
string decryptedString = Convert.ToBase64String(decryptedBytes);
return decryptedString;
}
But, I get the following error on the line specified above^.
An unhandled exception of type 'System.IO.IOException' occurred in
BouncyCastle.Crypto.dll
Additional information: -----END RSA PRIVATE KEY not found
I believe there's nothing wrong with the key pair combo as I get an error before I even try to decrypt anything.
The privateKey parameter is currently hardcoded into the script using this format:
string privateKey = "-----BEGIN RSA PRIVATE KEY-----XXXXXXXX-----END RSA PRIVATE KEY-----";
So it seems to me the footer actually is included in the string... I have debugged and googled everywhere but I can't seem to solve it. I'm pretty new to RSA&Bouncycastle so maybe I'm just using wrong methods.
Hope you can help, thanks!
- G4A
P.S. This is my first Stackoverflow question, I just created an account, so if you could also give me some feedback on the way I formulated this question; great!
You need to add a new line between the pre/post encapsulation boundary text and the Base64 data, so:
string privateKey = "-----BEGIN RSA PRIVATE KEY-----\r\nXXX\r\n-----END RSA PRIVATE KEY-----";
This is because the pem specification allows for the existence of other textual headers between the two.
If this doesn't work
"-----BEGIN RSA PRIVATE KEY-----\r\nXXXXXXXX\r\n-----END RSA PRIVATE KEY-----"
please try this
"-----BEGIN RSA PRIVATE KEY-----
XXXXXXXX
-----END RSA PRIVATE KEY-----"
We converted the BOX Private Key to Base64 Format and stored the same in Azure Vault.
Convert key to Base64 using Base64Encode method, store in Azure Key Vault.
Retrieve the encoded string in code, decoded back using Base64Decode Method.
public static string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return System.Convert.ToBase64String(plainTextBytes);
}
public static string Base64Decode(string base64EncodedData)
{
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}
I recommend use \x0A instead of \r\n and
.
Because only this option worked for me.
So :
"-----BEGIN RSA PRIVATE KEY-----\x0AXXXXXXXX\x0A-----END RSA PRIVATE KEY-----"
This is a duplicate of an unanswered question here: Using an RSA Public Key to decrypt a string that was encrypted using RSA Private Key
You can see the author found a solution using some code from here:
http://www.codeproject.com/KB/security/PrivateEncryption.aspx
Using code from that link looks very promising. The only thing missing is the padding. I typically use PKCS1.5 padding which is the default for OpenSSL RSA.
I know the answer to this question is very close. I know the only thing holding back decryption is the pkcs1.5 padding on the encrypted openssl ciphertext.
I was surprised to see how little information is out there on this subject because there are many situations where you would need a server to encrypt something, sign something, etc, and have a client application verify, decrypt, etc with the public key.
I also extensively tried using the RSACryptoServiceProvider to verify hash's resulting from the encryption using OpenSSL. For example, I would do a private key encryption using a SHA256 hash of the plaintext, then try to do a RSACryptoServiceProvider verify on that signature, and it does not work. I think the way MS does this is non standard and there are perhaps special customization at work with that.
So, the alternative is this question, which is simply taking private key encrypted ciphertext and using C# to decrypt it, thus, verifying it's authenticity. Hashes can be incorporated to make a simple signature verification system for data objects signed by the server and verified on the client.
I've looked through the PKCS1 RFC's, OpenSSL rsa source code, and other projects, I cannot get a solid answer on how to account for PKCS1 padding when doing my RSA Decrypt. I cannot locate where in the OpenSSL source code they handle the PKCS1 padding, otherwise, I might have an answer by now.
Also, this is my first question, I know it's a duplicate of an unanswered question, so, what to do? I googled that too, and found nothing.
The other thing I don't understand is why my decrypt method doesn't work. Since padding is removed after decryption, my decrypted data should resemble plaintext, and it's not even close. So, I'm almost sure that pkcs1 padding means that other things are happening, specifically, to the ciphertext which means that the ciphertext must be preprocessed prior to decryption to remove padding elements.
Perhaps simply filtering the ciphertext to remove padding elements is the simplest solution here...
Here is my Decrypt method:
public static byte[] PublicDecryption(this RSACryptoServiceProvider rsa, byte[] cipherData)
{
if (cipherData == null)
throw new ArgumentNullException("cipherData");
BigInteger numEncData = new BigInteger(cipherData);
RSAParameters rsaParams = rsa.ExportParameters(false);
BigInteger Exponent = GetBig(rsaParams.Exponent);
BigInteger Modulus = GetBig(rsaParams.Modulus);
BigInteger decData = BigInteger.ModPow(numEncData, Exponent, Modulus);
byte[] data = decData.ToByteArray();
byte[] result = new byte[data.Length - 1];
Array.Copy(data, result, result.Length);
result = RemovePadding(result);
Array.Reverse(result);
return result;
}
private static byte[] RemovePadding(byte[] data)
{
byte[] results = new byte[data.Length - 4];
Array.Copy(data, results, results.Length);
return results;
}
The problem isn't with the padding. In fact, removing padding values from decrypted ciphertext is actually very simple. The problem was with the software at this location:
You can see the author found a solution using some code from here: http://www.codeproject.com/KB/security/PrivateEncryption.aspx
And with Microsoft's implementation of System.Numeric which simply cannot handle larger integers...
To fix the issue, I looked at previous releases of code on the codeproject site and ended up with this PublicDecrypt method.
public static byte[] PublicDecryption(this RSACryptoServiceProvider rsa, byte[] cipherData)
{
if (cipherData == null)
throw new ArgumentNullException("cipherData");
BigInteger numEncData = new BigInteger(cipherData);
RSAParameters rsaParams = rsa.ExportParameters(false);
BigInteger Exponent = new BigInteger(rsaParams.Exponent);
BigInteger Modulus = new BigInteger(rsaParams.Modulus);
BigInteger decData2 = numEncData.modPow(Exponent, Modulus);
byte[] data = decData2.getBytes();
bool first = false;
List<byte> bl = new List<byte>();
for (int i = 0; i < data.Length; ++i)
{
if (!first && data[i] == 0x00)
{
first = true;
}
else if (first)
{
if (data[i] == 0x00)
{
return bl.ToArray();
}
bl.Add(data[i]);
}
}
if (bl.Count > 0)
return bl.ToArray();
return new byte[0];
}
That will perfectly decrypt ciphertext created by openssl using the rsautl utility, or the Perl Crypt::OpenSSL::RSA private_encrypt method.
The other big change was dropping the Microsoft BitInteger library which simply didn't work. I ended up using the one mentioned in the Code Project article , and found here:
http://www.codeproject.com/Articles/2728/C-BigInteger-Class
The key here is to set the maxintsize in the library to a value which is larger based on how big of a key size you are using. For 4096 bit, a value of 500 worked fine (approx length of the modulus).
Here is the calling method:
var encmsg3 = "JIA7qtOrbBthptILxnurAeiQM3JzSoi5WiPCpZrIIqURKfVQMN1BrondF9kyNzbjTs1DaEKEuMBVwKExZe22yCvXXpm8pwcEGc9EHcVK2MPqNo89tIF8LJcaDqBMwLvxdaa/QgebtpmOVN/TIWfuiV8KR+Wn07KwsgV+3SALbNgbOIR3RtCx3IiQ3tZzybJb08+ZKwJEOT011uwvvGmtJskQgq9PC8kr1RPXMgaP6xMX7PHFJ8ORhkuWOFfCh+R4NhY1cItVhnRewKpIC2qVlpzUYRAgKIKdCXuZDqUQdIponR29eTovvLb0DvKQCLTf9WI1SzUm6pKRn0vLsQL7L3UYHWl43ISrTpDdp+3oclhgRF3uITR4WCvoljephbGc6Gelk5z3Vi6lN0oQaazJ7zIen+a/Ts7ZX3KKlwPl4/lAFRjdjoqu7u4IAK7O7u1Jf2xDiGw18C/eGt8UHl09zU4qQf9/u+7gtJ+10z2NERlLSaCDjVqslwmmxu81pG2gCv8LfpR4JlPaFfBZMGfGBihyGryWhJwizUXXo8wgdoYbHRXe8/gL19qro0ea5pA9aAhDjTpX1Zzbwu2rUU7j6wtwQtUDJOGXXCw1VOHsx6WXeW196RkqG72ucVIaSAlx5TFJv8cnj6werEx1Ung5456gth3gj19zHc8E/Mwcpsk=";
byte[] enc = Convert.FromBase64String(encmsg3);
var dec = rsa2.PublicDecryption(enc);
Debug.Print("PLAINTEXT: " + Encoding.UTF8.GetString(dec));
The only last thing someone would need to completely replicate this would be getting the private key into openssl format so that they could pass the private and public keys back and forth between openssl and C#.
I used openssl.net, and created an RSA instance, and set all the variables using bignumbers. Here's the code for that:
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(Properties.Resources.RSAParameters);
RSAParameters par = rsa.ExportParameters(true); // export the private key
using (OpenSSL.Crypto.RSA rsaos = new OpenSSL.Crypto.RSA())
using (BigNumber bnmod = BigNumber.FromArray(par.Modulus))
using (BigNumber bnexp = BigNumber.FromArray(par.Exponent))
using (BigNumber bnD = BigNumber.FromArray(par.D))
using (BigNumber bnP = BigNumber.FromArray(par.P))
using (BigNumber bnQ = BigNumber.FromArray(par.Q))
using (BigNumber bnDmodP = BigNumber.FromArray(par.DP))
using (BigNumber bnDmodQ = BigNumber.FromArray(par.DQ))
using (BigNumber bnInverse = BigNumber.FromArray(par.InverseQ))
{
rsaos.PublicExponent = bnexp;
rsaos.PublicModulus = bnmod;
rsaos.IQmodP = bnInverse;
rsaos.DmodP1 = bnDmodP;
rsaos.DmodQ1 = bnDmodQ;
rsaos.SecretPrimeFactorP = bnP;
rsaos.SecretPrimeFactorQ = bnQ;
rsaos.PrivateExponent = bnD;
string privatekey = rsaos.PrivateKeyAsPEM;
string publickey = rsaos.PublicKeyAsPEM
}
With that you can easily create an RSA key, export everything to OpenSSL, and encrypt/decrypt anything you want within reason. It is enough to handle private key encryption followed by public key decryption.
Cool.
There is a problem in the line in the PublicDecryption function:
BigInteger numEncData = new BigInteger(cipherData);
it shall be:
BigInteger numEncData = GetBig(cipherData);
This line shall also be removed:
Array.Reverse(result);
You may encounter some padding problem, but if you can get the data right, it shall be easy to correct that.
I got a module which RSA encrypts the data and passes on to the C#.
C# needs to decrypt it based on the public key (64 bit encoded) and the passed token.
I have token , 64 bit encoded public key, can some help me get with the sample to get started.
All I know from Java end is, it is using. I have got the result from Java end and need to write a parser in C# to decrypt this. I get both public key and token as a string value.
Cipher cipher = Cipher.getInstance(ALGORITHM); //Algorithm = "RSA"
cipher.init(Cipher.DECRYPT_MODE, key);
Thanks
To get started, you'll need the private key to decrypt the message. By "public key (64 bit encoded)", I'm guessing what you really have is a Base-64–encoded certificate, with a header line that says "----- BEGIN CERTIFICATE-----" and a footer that says "-----END CERTIFICATE-----".
If that's correct, you'll need to find the private key. This is sometimes stored in a PKCS #12 format file, with a ".p12" or ".pfx" extension. You'll need a password to access the private key if it is stored in such a file.
Alternatively, OpenSSL and other utilities use private key files that can be Base-64–encoded or binary. These have a variety of extensions, and may or may not be password-protected. If the file that you have has a header line of "-----BEGIN RSA PRIVATE KEY-----" or "-----BEGIN PRIVATE KEY-----", that is actually the private key.
Finally, Windows can store private keys in its internal key store.
When you clarify the location of the private key, please update your question.
If the private key is used on the Java side, it may be an attempt to perform a digital signature. While all of several Java providers I've tested produce correct results when (ab)used this way, if you are doing a signature, the Signature class should be used. The C# code should use a signature object to "verify" the signature as well.
Encryption is performed with the private key. Since the public key is public, anyone can decrypt the message; i.e., the message is not confidential. Public keys are used by recipients to verify signed messages.
Check this code out.
public static string Decrypt(string inputText)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
byte[] encryptedData = Convert.FromBase64String(inputText.Replace(" ","+"));
PasswordDeriveBytes secretKey = new PasswordDeriveBytes(ENCRYPTION_KEY, SALT);
using (ICryptoTransform decryptor = rijndaelCipher.CreateDecryptor(secretKey.GetBytes(32), secretKey.GetBytes(16)))
{
using (MemoryStream memoryStream = new MemoryStream(encryptedData))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
byte[] plainText = new byte[encryptedData.Length];
int decryptedCount = cryptoStream.Read(plainText, 0, plainText.Length);
return Encoding.Unicode.GetString(plainText, 0, decryptedCount);
}
}
}