title: RSA .NET encryption Java decryption
body: <p>I am trying to encrypt strings in .NET by using a RSA algorithm and decrypt the result in Java. At the moment, I have been able to do the opposite (Encrypt in Java, Decrypt in .NET). 
  Here I have my code that actually works (JAVA encryption):</p>
  
  <pre><code>byte[] modulusBytes = Base64.decode("2rRVVVFJRbH/wAPDtnwZwu+nxU+AZ6uXxh/sW+AMCBogg7vndZsnRiHoLttYYPqOyOhfgaBOQogrIfrKL4lipK4m52SBzw/FfcM9DsKs/rYR83tBLiIAfgdnVjF27tZID+HJMFTiI30mALjr7+tfp+2lIACXA1RIKTk7S9pDmX8=");
  byte[] exponentBytes = Base64.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.encode(cipherData);
  return encryptedString;
  </code></pre>
  
  <p>And (.NET decryption)</p>
  
  <pre><code>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);
  rsa1.FromXmlString("&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;");
  
  string data2Decrypt = "BaB21vY+RD/jiY3AAsb269fIWTEH38s0xLUfJ7CoVUgaQ6vYzB0tiJ1Ag9HNEdCcuZdGchhqnms8jpsqsHC1iKrz6QCLsgUU7VNWDfQqZYR6Rl/GwR0biK2STnOL+g06f/JUdixHOHOgROify1m8qppYo5plpOVMqYFzEMREMkM=";
  
  byte[] encyrptedBytes = Convert.FromBase64String(data2Decrypt);
  
  byte[] plain = rsa1.Decrypt(encyrptedBytes, false);
  string decryptedString = System.Text.Encoding.UTF8.GetString(plain);
  Console.WriteLine("SALIDA: " + decryptedString);
  </code></pre>
  
  <p>Now I want to do the opposite... But I get some errors like (the size of the key should be 128 bytes... etc) How should I do it?</p>
  
  <p>Here I add the current <strong>non working</strong> code:</p>
  
  <p><strong>.NET</strong></p>
  
  <pre><code>public string Encrypt(string text)
  {
      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);
      rsa1.FromXmlString("&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;92jJJyzFBSx6gL4Y1YpALmc5CNjoE/wETjqb3ci2v0+3rZWvJKmKy1ZEdlXpyuvXVksJ6cMdUpNAkMknUk9pTQ==&lt;/P&gt;&lt;Q&gt;4kxkABZOXyDLryYGCGY0b8N0FIdu5BTCFDYEdcatxl/f7ZGDS1NgHJpUWxkVXFfHy2Y/GuDOIbpcwlsO739H+w==&lt;/Q&gt;&lt;DP&gt;5bNFvrdUHF+VRN45VFjNCcgQLeSkY5mBrdfASoNFGA29LM5iE5nNIMfxPCS7sQiRnq6Af6YFHVtVgJchiMvtqQ==&lt;/DP&gt;&lt;DQ&gt;j+ng1qVY5epnXlWiFIla45C7K6sNfIMvAcdwgq39KWEjeWPGyYqWXtpOtzh2eylf6Bx4GVHKBW0NPJTIJMsfLQ==&lt;/DQ&gt;&lt;InverseQ&gt;8uu0dfPVDqB2qFM1Vdi8hl+2uZtN7gjT2co1cEWy29HVYBZD0k9KKCf2PbkeuSfpgFpE70wW5Hrp8V7l/SwSOw==&lt;/InverseQ&gt;&lt;D&gt;MM/c18zroJ2Iqi9s5/asvUBF3pjO3NSEbFjFpP/NT6WdKimvECWPz2xT6NlV0Vc6tQaAAmtn7Bt+HPhfVdrA4/ysYVe3/6TWkPjW+bvAhMWu/ZqISx11/jPYSGD9g3ZXgUiqcQM8UbOjlswoq4fpheEXTB0xdVutDLpO3qgHN6k=&lt;/D&gt;&lt;/RSAKeyValue&gt;");
  
      System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
      byte[] textBytes = encoding.GetBytes(text);
      byte[] encryptedOutput = rsa1.Encrypt(textBytes, false);
      string outputB64 = Convert.ToBase64String(encryptedOutput);
      Console.WriteLine(outputB64);
      return outputB64;
  }
  </code></pre>
  
  <p><strong>Java</strong></p>
  
  <pre><code>public static String Decrypt(String encodedString) throws IllegalBlockSizeException, UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException
  {
      byte[] modulusBytes = Base64.decode("2rRVVVFJRbH/wAPDtnwZwu+nxU+AZ6uXxh/sW+AMCBogg7vndZsnRiHoLttYYPqOyOhfgaBOQogrIfrKL4lipK4m52SBzw/FfcM9DsKs/rYR83tBLiIAfgdnVjF27tZID+HJMFTiI30mALjr7+tfp+2lIACXA1RIKTk7S9pDmX8=");
      byte[] exponentBytes = Base64.decode("AQAB");
      BigInteger modulus = new BigInteger(1, modulusBytes );
      BigInteger exponent = new BigInteger(1, exponentBytes);
  
      RSAPrivateKeySpec rsaPrivKey = new RSAPrivateKeySpec(modulus, exponent);
      KeyFactory fact = KeyFactory.getInstance("RSA");
      PrivateKey privKey = fact.generatePrivate(rsaPrivKey);
  
      Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
      cipher.init(Cipher.DECRYPT_MODE, privKey);
  
      byte[] base64String = Base64.decode(encodedString);
      byte[] plainBytes = new String(base64String).getBytes("UTF-8");
      byte[] cipherData = cipher.doFinal(plainBytes);
  
      System.out.println(cipherData);
      return cipherData.toString();
  }
  </code></pre>