“附加到系统的设备未响应”错误在RSA加密中是什么?

huangapple go评论125阅读模式
英文:

What is "A device attached to the system is not functioning" error in RSA encryption?

问题

使用RSA,希望在Java中使用公钥加密,并在C#中使用公钥和私钥解密。

在Java中,加密运作正常,但在C#中解密时出现以下错误:"系统连接的设备无法运行",这是什么意思?

Java加密方法:

static String publickey = "2rRVVVFJRbH/wAPDtnwZwu+nxU+AZ6uXxh/sW+AMCBogg7vndZsnRiHoLttYYPqOyOhfgaBOQogrIfrKL4lipK4m52SBzw/FfcM9DsKs/rYR83tBLiIAfgdnVjF27tZID+HJMFTiI30mALjr7+tfp+2lIACXA1RIKTk7S9pDmX8=";

static String Encrypt(String encodedString) {
      try {
        byte[] modulusBytes = Base64.getDecoder().decode(publickey);
        byte[] exponentBytes = Base64.getDecoder().decode("AQAB");
        BigInteger modulus = new BigInteger(1, modulusBytes);
        BigInteger exponent = new BigInteger(1, exponentBytes);

        RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, exponent);
        KeyFactory fact = KeyFactory.getInstance("RSA");
        PublicKey pubKey = fact.generatePublic(rsaPubKey);

        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);

        byte[] plainBytes = new String("big kitty dancing").getBytes("UTF-8");
        byte[] cipherData = cipher.doFinal(plainBytes);
        String encryptedString = Base64.getEncoder().encodeToString(cipherData);

        return encryptedString;
    } catch (InvalidKeySpecException | NoSuchAlgorithmException | NoSuchPaddingException |
            InvalidKeyException | BadPaddingException | IllegalBlockSizeException | UnsupportedEncodingException err) {
    }
    return "Error";
}

C#解密方法:

static string Decrypt()
{
    const int PROVIDER_RSA_FULL = 1;
    const string CONTAINER_NAME = "Tracker";

    CspParameters cspParams;
    cspParams = new CspParameters(PROVIDER_RSA_FULL);
    cspParams.KeyContainerName = CONTAINER_NAME;
    RSACryptoServiceProvider rsa1 = new RSACryptoServiceProvider(cspParams);
    //string str = rsa1.ToXmlString(true);

    rsa1.FromXmlString("<RSAKeyValue><Modulus>2rRVVVFJRbH/wAPDtnwZwu+nxU+AZ6uXxh/sW+AMCBogg7vndZsnRiHoLttYYPqOyOhfgaBOQogrIfrKL4lipK4m52SBzw/FfcM9DsKs/rYR83tBLiIAfgdnVjF27tZID+HJMFTiI30mALjr7+tfp+2lIACXA1RIKTk7S9pDmX8=</Modulus><Exponent>AQAB</Exponent><P>+lXMCEwIN/7+eMpBrq87kQppxu3jJBTwztGTfXNaPUTx+A6uqRwug5oHBbSpYXKNDNCBzVm/0VxB3bo4FJx+ZQ==</P><Q>yasOGaJaE9xlF9T2xRuKeG9ZxCiyjhYaYB/mbtL+SIbtkRLi/AxaU4g2Il/UxhxhSXArKxIzV28zktispPJx1Q==</Q><DP>ueRgQIEFUV+fY979a1RgrVHIPpqEI1URhOMH3Q59oiXCcOumM5njyIHmWQxRAzXnG+7xlKXi1PrnRll0L4oOKQ==</DP><DQ>dfEMNgG1HJhwpxdtmqkYuoakwQvsIRzcIAuIAJh1DoWaupWJGk8/JEstHb1d+t7uJrzrAi2KyT/HscH2diE0YQ==</DQ><InverseQ>YoYF9PF6FiC0YngVeaC/eqt/ea8wMYNN3YO1LuzWpcy2exPRj2U0ZbWMvHXMUb4ea2qmhZGx1QlK4ULAuWKpXQ==</InverseQ><D>g1WAWI4pEK9TA7CA2Yyy/2FzzNiu0uQCuE2TZYRNiomo96KQXpxwqAzZLw+VDXfJMypwDMAVZe/SqzSJnFEtZxjdxaEo3VLcZ1mnbIL0vS7D6iFeYutF9kF231165qGd3k2tgymNMMpY7oYKjS11Y6JqWDU0WE5hjS2X35iG6mE=</D></RSAKeyValue>");

    string data2Decrypt = "pt0DMKQM34rIp/aw64NBRhj8HZlp31i3Nu/9SP6o2Iwl9plu8k1TDbe04cQRkIDx/h6hsAKM0j13HdauidnEEvtm0yp9WrOMGut2XJUTUxWt5/y4bosm06tYtfw5HnlNHMMBtY22caCppwfdOJbiwp9rwEtWV9tXHU7VMenlVuw=";

    byte[] encyrptedBytes = Convert.FromBase64String(data2Decrypt);
    //Error occured in this line
    byte[] plain = rsa1.Decrypt(encyrptedBytes, false); 
    string decryptedString = System.Text.Encoding.UTF8.GetString(plain);
    return decryptedString;
}

更新的内容:

异常堆栈:

System.Security.Cryptography.CryptographicException
     .ThrowCryptographicException(Int32 hr)
   在 System.Security.Cryptography.RSACryptoServiceProvider.DecryptKey(SafeKeyHandle
     pKeyContext, Byte[] pbEncryptedKey, Int32 cbEncryptedKey, Boolean fOAEP, ObjectHandleOnStack ohRetDecryptedKey)
   在 System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] rgb, Boolean fOAEP)
   在 CJSecurity.Program.Decrypt() in C:\CJSecurity\CJSecurity\Program.cs
英文:

Using RSA and want to encrypt with the public key in JAVA and using public and private keys to decrypt in c#.

Encryption works fine in java but decryption in c# has this error:
"A device attached to the system is not functioning", What does it mean?

Java encryption method:

static String publickey = &quot;2rRVVVFJRbH/wAPDtnwZwu+nxU+AZ6uXxh/sW+AMCBogg7vndZsnRiHoLttYYPqOyOhfgaBOQogrIfrKL4lipK4m52SBzw/FfcM9DsKs/rYR83tBLiIAfgdnVjF27tZID+HJMFTiI30mALjr7+tfp+2lIACXA1RIKTk7S9pDmX8=&quot;;

static String Encrypt(String encodedString) {
      try {
        byte[] modulusBytes = Base64.getDecoder().decode(publickey);
        byte[] exponentBytes = Base64.getDecoder().decode(&quot;AQAB&quot;);
        BigInteger modulus = new BigInteger(1, modulusBytes);
        BigInteger exponent = new BigInteger(1, exponentBytes);

        RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, exponent);
        KeyFactory fact = KeyFactory.getInstance(&quot;RSA&quot;);
        PublicKey pubKey = fact.generatePublic(rsaPubKey);

        Cipher cipher = Cipher.getInstance(&quot;RSA/ECB/PKCS1Padding&quot;);
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);

        byte[] plainBytes = new String(&quot;big kitty dancing&quot;).getBytes(&quot;UTF-8&quot;);
        byte[] cipherData = cipher.doFinal(plainBytes);
        String encryptedString = Base64.getEncoder().encodeToString(cipherData);

        return encryptedString;
    } catch (InvalidKeySpecException | NoSuchAlgorithmException | NoSuchPaddingException |
            InvalidKeyException | BadPaddingException | IllegalBlockSizeException | UnsupportedEncodingException err) {
    }
    return &quot;Error&quot;;
}

C# decryption method:

   static string Decrypt()
    {
        const int PROVIDER_RSA_FULL = 1;
        const string CONTAINER_NAME = &quot;Tracker&quot;;

        CspParameters cspParams;
        cspParams = new CspParameters(PROVIDER_RSA_FULL);
        cspParams.KeyContainerName = CONTAINER_NAME;
        RSACryptoServiceProvider rsa1 = new RSACryptoServiceProvider(cspParams);
        //string str = rsa1.ToXmlString(true);


        rsa1.FromXmlString(&quot;&lt;RSAKeyValue&gt;&lt;Modulus&gt;2rRVVVFJRbH/wAPDtnwZwu+nxU+AZ6uXxh/sW+AMCBogg7vndZsnRiHoLttYYPqOyOhfgaBOQogrIfrKL4lipK4m52SBzw/FfcM9DsKs/rYR83tBLiIAfgdnVjF27tZID+HJMFTiI30mALjr7+tfp+2lIACXA1RIKTk7S9pDmX8=&lt;/Modulus&gt;&lt;Exponent&gt;AQAB&lt;/Exponent&gt;&lt;P&gt;+lXMCEwIN/7+eMpBrq87kQppxu3jJBTwztGTfXNaPUTx+A6uqRwug5oHBbSpYXKNDNCBzVm/0VxB3bo4FJx+ZQ==&lt;/P&gt;&lt;Q&gt;yasOGaJaE9xlF9T2xRuKeG9ZxCiyjhYaYB/mbtL+SIbtkRLi/AxaU4g2Il/UxhxhSXArKxIzV28zktispPJx1Q==&lt;/Q&gt;&lt;DP&gt;ueRgQIEFUV+fY979a1RgrVHIPpqEI1URhOMH3Q59oiXCcOumM5njyIHmWQxRAzXnG+7xlKXi1PrnRll0L4oOKQ==&lt;/DP&gt;&lt;DQ&gt;dfEMNgG1HJhwpxdtmqkYuoakwQvsIRzcIAuIAJh1DoWaupWJGk8/JEstHb1d+t7uJrzrAi2KyT/HscH2diE0YQ==&lt;/DQ&gt;&lt;InverseQ&gt;YoYF9PF6FiC0YngVeaC/eqt/ea8wMYNN3YO1LuzWpcy2exPRj2U0ZbWMvHXMUb4ea2qmhZGx1QlK4ULAuWKpXQ==&lt;/InverseQ&gt;&lt;D&gt;g1WAWI4pEK9TA7CA2Yyy/2FzzNiu0uQCuE2TZYRNiomo96KQXpxwqAzZLw+VDXfJMypwDMAVZe/SqzSJnFEtZxjdxaEo3VLcZ1mnbIL0vS7D6iFeYutF9kF231165qGd3k2tgymNMMpY7oYKjS11Y6JqWDU0WE5hjS2X35iG6mE=&lt;/D&gt;&lt;/RSAKeyValue&gt;&quot;);

        string data2Decrypt = &quot;pt0DMKQM34rIp/aw64NBRhj8HZlp31i3Nu/9SP6o2Iwl9plu8k1TDbe04cQRkIDx/h6hsAKM0j13HdauidnEEvtm0yp9WrOMGut2XJUTUxWt5/y4bosm06tYtfw5HnlNHMMBtY22caCppwfdOJbiwp9rwEtWV9tXHU7VMenlVuw=&quot;;

        byte[] encyrptedBytes = Convert.FromBase64String(data2Decrypt);
        //Error occured in this line
        byte[] plain = rsa1.Decrypt(encyrptedBytes, false); 
        string decryptedString = System.Text.Encoding.UTF8.GetString(plain);
        return decryptedString;
    }

UPDATED:

StackTrace:

   at
 System.Security.Cryptography.CryptographicException
 .ThrowCryptographicException(Int32 hr)
   at    System.Security.Cryptography.RSACryptoServiceProvider.DecryptKey(SafeKeyHandle
 pKeyContext, Byte[] pbEncryptedKey, Int32 cbEncryptedKey, Boolean fOAEP, ObjectHandleOnStack ohRetDecryptedKey)
   at System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] 
rgb, Boolean fOAEP)
  at CJSecurity.Program.Decrypt() in C:\CJSecurity\CJSecurity\Program.cs

答案1

得分: 2

私钥不正确。我假设密钥生成失败并出现了一些奇怪的错误。我使用openssl检查了您的私钥,并且它指出n不等于p * q。

$rsa1 = New-Object System.Security.Cryptography.RSACryptoServiceProvider
$rsa1.FromXmlString("&lt;RSAKeyValue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\openssl.exe' rsa -check -inform DER -in rsa1.der

以下是来自openssl命令的输出:

RSA key error: n does not equal p q
writing RSA key
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDatFVVUUlFsf/AA8O2fBnC76fFT4Bnq5fGH+xb4AwIGiCDu+d1
mydGIegu21hg+o7I6F+BoE5CiCsh+soviWKkribnZIHPD8V9wz0Owqz+thHze0Eu
IgB+B2dWMXbu1kgP4ckwVOIjfSYAuOvv61+n7aUgAJcDVEgpOTtL2kOZfwIDAQAB
AoGBAINVgFiOKRCvUwOwgNmMsv9hc8zYrtLkArhNk2WETYqJqPeikF6ccKgM2S8P
lQ13yTMqcAzAFWXv0qs0iZxRLWcY3cWhKN1S3GdZp2yC9L0uw+ohXmLrRfZBdt9d
euahnd5NrYMpjTDKWO6GCo0tdWOialg1NFhOYY0tl9+YhuphAkEA+lXMCEwIN/7+
eMpBrq87kQppxu3jJBTwztGTfXNaPUTx+A6uqRwug5oHBbSpYXKNDNCBzVm/0VxB
3bo4FJx+ZQJBAMmrDhmiWhPcZRfU9sUbinhvWcQoso4WGmAf5m7S/kiG7ZES4vwM
WlOINiJf1MYcYUlwKysSM1dvM5LYrKTycdUCQQC55GBAgQVRX59j3v1rVGCtUcg+
moQjVRGE4wfdDn2iJcJw66YzmePIgeZZDFEDNecb7vGUpeLU+udGWXQvig4pAkB1
8Qw2AbUcmHCnF22aqRi6hqTBC+whHNwgC4gAmHUOhZq6lYkaTz8kSy0dvV363u4m
vOsCLYrJP8exwfZ2ITRhAkBihgX08XoWILRieBV5oL96q395rzAxg03dg7Uu7Nal
zLZ7E9GPZTRltYy8dcxRvh5raqaFkbHVCUrhQsC5Yqld
-----END RSA PRIVATE KEY-----
英文:

The private key is not correct. I assume the key generation failed with some strange error. I checked your private key using openssl, and it says the n does not equal to p * q.

$rsa1 = New-Object System.Security.Cryptography.RSACryptoServiceProvider
$rsa1.FromXmlString(&quot;&lt;RSAKeyValue&gt;&lt;Modulus&gt;2rRVVVFJRbH/wAPDtnwZwu+nxU+AZ6uXxh/sW+AMCBogg7vndZsnRiHoLttYYPqOyOhfgaBOQogrIfrKL4lipK4m52SBzw/FfcM9DsKs/rYR83tBLiIAfgdnVjF27tZID+HJMFTiI30mALjr7+tfp+2lIACXA1RIKTk7S9pDmX8=&lt;/Modulus&gt;&lt;Exponent&gt;AQAB&lt;/Exponent&gt;&lt;P&gt;+lXMCEwIN/7+eMpBrq87kQppxu3jJBTwztGTfXNaPUTx+A6uqRwug5oHBbSpYXKNDNCBzVm/0VxB3bo4FJx+ZQ==&lt;/P&gt;&lt;Q&gt;yasOGaJaE9xlF9T2xRuKeG9ZxCiyjhYaYB/mbtL+SIbtkRLi/AxaU4g2Il/UxhxhSXArKxIzV28zktispPJx1Q==&lt;/Q&gt;&lt;DP&gt;ueRgQIEFUV+fY979a1RgrVHIPpqEI1URhOMH3Q59oiXCcOumM5njyIHmWQxRAzXnG+7xlKXi1PrnRll0L4oOKQ==&lt;/DP&gt;&lt;DQ&gt;dfEMNgG1HJhwpxdtmqkYuoakwQvsIRzcIAuIAJh1DoWaupWJGk8/JEstHb1d+t7uJrzrAi2KyT/HscH2diE0YQ==&lt;/DQ&gt;&lt;InverseQ&gt;YoYF9PF6FiC0YngVeaC/eqt/ea8wMYNN3YO1LuzWpcy2exPRj2U0ZbWMvHXMUb4ea2qmhZGx1QlK4ULAuWKpXQ==&lt;/InverseQ&gt;&lt;D&gt;g1WAWI4pEK9TA7CA2Yyy/2FzzNiu0uQCuE2TZYRNiomo96KQXpxwqAzZLw+VDXfJMypwDMAVZe/SqzSJnFEtZxjdxaEo3VLcZ1mnbIL0vS7D6iFeYutF9kF231165qGd3k2tgymNMMpY7oYKjS11Y6JqWDU0WE5hjS2X35iG6mE=&lt;/D&gt;&lt;/RSAKeyValue&gt;&quot;)
[System.IO.File]::WriteAllBytes(&#39;rsa1.der&#39;,  $rsa1.ExportPkcs8PrivateKey())
&amp; &#39;C:\Program Files\OpenSSL-Win64\bin\openssl.exe&#39; rsa -check -inform DER -in rsa1.der

Here is the output from the openssl command:

RSA key error: n does not equal p q
writing RSA key
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDatFVVUUlFsf/AA8O2fBnC76fFT4Bnq5fGH+xb4AwIGiCDu+d1
mydGIegu21hg+o7I6F+BoE5CiCsh+soviWKkribnZIHPD8V9wz0Owqz+thHze0Eu
IgB+B2dWMXbu1kgP4ckwVOIjfSYAuOvv61+n7aUgAJcDVEgpOTtL2kOZfwIDAQAB
AoGBAINVgFiOKRCvUwOwgNmMsv9hc8zYrtLkArhNk2WETYqJqPeikF6ccKgM2S8P
lQ13yTMqcAzAFWXv0qs0iZxRLWcY3cWhKN1S3GdZp2yC9L0uw+ohXmLrRfZBdt9d
euahnd5NrYMpjTDKWO6GCo0tdWOialg1NFhOYY0tl9+YhuphAkEA+lXMCEwIN/7+
eMpBrq87kQppxu3jJBTwztGTfXNaPUTx+A6uqRwug5oHBbSpYXKNDNCBzVm/0VxB
3bo4FJx+ZQJBAMmrDhmiWhPcZRfU9sUbinhvWcQoso4WGmAf5m7S/kiG7ZES4vwM
WlOINiJf1MYcYUlwKysSM1dvM5LYrKTycdUCQQC55GBAgQVRX59j3v1rVGCtUcg+
moQjVRGE4wfdDn2iJcJw66YzmePIgeZZDFEDNecb7vGUpeLU+udGWXQvig4pAkB1
8Qw2AbUcmHCnF22aqRi6hqTBC+whHNwgC4gAmHUOhZq6lYkaTz8kSy0dvV363u4m
vOsCLYrJP8exwfZ2ITRhAkBihgX08XoWILRieBV5oL96q395rzAxg03dg7Uu7Nal
zLZ7E9GPZTRltYy8dcxRvh5raqaFkbHVCUrhQsC5Yqld
-----END RSA PRIVATE KEY-----

huangapple
  • 本文由 发表于 2020年8月25日 18:37:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63577078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定