RSA/ECB/ pkcs1用模数和指数填充c#解密

时间:2021-02-13 18:23:12

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服务器的加密值。我需要解密它。该服务的文档给了我以下解密算法,提供者会将“m”和“e”的值发给我:

Java Code:

Java代码:

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):

我需要把它转换成c#。到目前为止,我尝试了以下c#代码(使用。net的BigInteger类):

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.

但是,当我想在rsa1实例中导入keyInfo时,我得到了一个“坏数据”消息的异常。

I have also tried:

我也试过:

  • setting info.Exponent instead of info.D
  • 设置信息。指数代替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).
  • 使用Chew Keong提供的BigInteger类和getBytes方法获取模量和指数的字节(同样,在两个信息中设置数组)。指数和信息。有和没有倒转)。

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 ?

谁能帮我翻译c#等价物中的java代码吗?

1 个解决方案

#1


1  

unfortunately the standard RSACryptoServiceProvider is not suitable for this specific case ... see for yourself:

不幸的是,标准RSACryptoServiceProvider并不适合这个特定的情况……你自己看:

if you try to import RSAParameters, there are several fields for you to fill ...

如果您试图导入rsaparameter,有几个字段供您填写……

Modulus ... the common modulus for your keypair... m in your case
Exponent ... the public exponent ... part of the public key, used for encryption

模量……关键对的公共模量…m在你的情况下是指数……公众指数……部分公钥,用于加密

D ... the private exponent ... part of the private key, used for decryption, e in your case

D…私人指数……私钥的一部分,用于解密,在你的例子中是e

P and Q ... the 2 Primes of the keypair ... P*Q=Modulus
DP and DQ ... intermediate values for a mathematical shortcut (chinese remainder theorem) for a faster way of decryption

P和Q……关键对的两个素数…P*Q=模DP和DQ…一个数学捷径(中国剩余定理)的中间值,用于更快的解密。

so ... you got the modulus and the private exponent ... while that's generally enough to be able to decrypt, it is not the full keypair ... P and Q are missing ... with P and Q we could calculate the rest...

所以…得到了模和私有指数。虽然这通常足以解密,但它不是完整的密钥对……P和Q缺失…有了P和Q,我们就可以计算出剩下的。

the RSACryptoServiceProvider expects you to provide the full keypair if you want to decrypt ... one missing value, and you will get that nasty cryptographicexception telling you "bad data"

如果要解密,RSACryptoServiceProvider希望您提供完整的密钥对……一个缺失的值,你会得到一个讨厌的密码异常告诉你“坏数据”

soo ... everything lost? no ... some time ago there was another question regarding RSA for which i wrote a few lines that handle RSA by only using bigintegers, and no RSACryptoServiceProvider at all ... Public key encryption with RSACryptoServiceProvider

秀……一切都失去了吗?不…不久前,有一个关于RSA的问题,我为它写了几行,只使用bigintegers处理RSA,没有RSACryptoServiceProvider…使用RSACryptoServiceProvider的公钥加密

maybe that helps you getting your value decrypted

也许这可以帮助您将值解密

#1


1  

unfortunately the standard RSACryptoServiceProvider is not suitable for this specific case ... see for yourself:

不幸的是,标准RSACryptoServiceProvider并不适合这个特定的情况……你自己看:

if you try to import RSAParameters, there are several fields for you to fill ...

如果您试图导入rsaparameter,有几个字段供您填写……

Modulus ... the common modulus for your keypair... m in your case
Exponent ... the public exponent ... part of the public key, used for encryption

模量……关键对的公共模量…m在你的情况下是指数……公众指数……部分公钥,用于加密

D ... the private exponent ... part of the private key, used for decryption, e in your case

D…私人指数……私钥的一部分,用于解密,在你的例子中是e

P and Q ... the 2 Primes of the keypair ... P*Q=Modulus
DP and DQ ... intermediate values for a mathematical shortcut (chinese remainder theorem) for a faster way of decryption

P和Q……关键对的两个素数…P*Q=模DP和DQ…一个数学捷径(中国剩余定理)的中间值,用于更快的解密。

so ... you got the modulus and the private exponent ... while that's generally enough to be able to decrypt, it is not the full keypair ... P and Q are missing ... with P and Q we could calculate the rest...

所以…得到了模和私有指数。虽然这通常足以解密,但它不是完整的密钥对……P和Q缺失…有了P和Q,我们就可以计算出剩下的。

the RSACryptoServiceProvider expects you to provide the full keypair if you want to decrypt ... one missing value, and you will get that nasty cryptographicexception telling you "bad data"

如果要解密,RSACryptoServiceProvider希望您提供完整的密钥对……一个缺失的值,你会得到一个讨厌的密码异常告诉你“坏数据”

soo ... everything lost? no ... some time ago there was another question regarding RSA for which i wrote a few lines that handle RSA by only using bigintegers, and no RSACryptoServiceProvider at all ... Public key encryption with RSACryptoServiceProvider

秀……一切都失去了吗?不…不久前,有一个关于RSA的问题,我为它写了几行,只使用bigintegers处理RSA,没有RSACryptoServiceProvider…使用RSACryptoServiceProvider的公钥加密

maybe that helps you getting your value decrypted

也许这可以帮助您将值解密