title: What is "A device attached to the system is not functioning" error in RSA encryption?
body: <p>Using RSA and want to encrypt with the public key in JAVA and using public and private keys to decrypt in c#.</p>
  <p>Encryption works fine in java but decryption in c# has this error:
  &quot;A device attached to the system is not functioning&quot;, What does it mean?</p>
  <p>Java encryption method:</p>
  <pre><code>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;;
  }
  </code></pre>
  <p>C# decryption method:</p>
  <pre><code>   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;
      }
  </code></pre>
  <p><strong>UPDATED:</strong></p>
  <p>StackTrace:</p>
  <pre><code>   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
  </code></pre>