I have an encrypted value from a java server. I need to decrypt it. The documentation of the service gives me the following algorithm for decryption, and the provider will send me the "m" and "e" values:
Java Code:
private String RSA_Decryption(String encryptedData)
throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IOException, IllegalBlockSizeException, BadPaddingException, GeneralSecurityException
{
BigInteger m = new BigInteger("verylongnumber1");
BigInteger e = new BigInteger("verylongnumber2");
RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(m, e);
KeyFactory fact = KeyFactory.getInstance("RSA");
PrivateKey privateKey = fact.generatePrivate(keySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
byte[] decValue = cipher.doFinal(decordedValue);
String decryptedValue = new String(decValue, "UTF-8");
return decryptedValue;
}
I need to convert this in C#. So far, I tried the following C# code (Using BigInteger class from .NET):
string m = "verylongnumber1";
string e = "verylongnumber2";
RSAParameters info = new RSAParameters();
var modulus = BigInteger.Parse(m).ToByteArray();
if (modulus.Length == 129) Array.Resize(ref modulus, 128); // it gives me 129 characters, the last being 0
var exponent = BigInteger.Parse(e).ToByteArray();
info.Modulus = modulus.Reverse().ToArray();
info.D = exponent.Reverse().ToArray();
var encryptedData = Convert.FromBase64String(@"gEQwzXpaARzC2pz9ahiyji8G/K9xecMzh6qi7hMmih4kR4hBwwjfcX83lNet91/hzHX9if1XwAe7/fO5xgXR8qLY+sZu9mj+iXiaSgYyQO3VyxcMD6q/wiVBXpOCX/LmG6qCVbFgn6LZvvcx9fUjVEn3FJFpqUhQh9PvNjmg8ks=");
var decryptedValue = this.Decrypt(encryptedData, info);
string decryptedBarcode = Encoding.Default.GetString(decryptedValue);
byte[] decryptedBytes;
using (RSACryptoServiceProvider rsa1 = new RSACryptoServiceProvider())
{
rsa1.ImportParameters(info); // throws exception with "Bad data" message
decryptedBytes = rsa1.Decrypt(encryptedData, false);
}
However, I get an exception with "Bad Data" message when I want to import the keyInfo in rsa1 instance.
I have also tried:
- setting info.Exponent instead of info.D
- using BigInteger class provided by Chew Keong and the getBytes method to obtain bytes for modulus and exponent (also, set the arrays in both info.Exponent and info.D with and without reverse).
Everything leads to the same exception with "Bad Data" message.
Can anyone help me with the translation of this java code in a C# equivalent ?