title: Extract RSA Parameters(Modulus and Exponent) from encoded String (RSA Public key)
body: <p>I am trying to upgrade my RSA key pair size from 1024 to 2048. I am going to give the sample code here.</p>
  
  <p><strong>For 1024:</strong>
  The first step in my code is to build a signature using Java code</p>
  
  <pre><code>   try {
          // Generate Public &amp; Private Keys
          KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
          kpg.initialize(1024);
          KeyPair keyPair = kpg.genKeyPair();
  
          RSAPublicKey key = (RSAPublicKey) keyPair.getPublic();
          System.out.println("Modulus");
          System.out.println(key.getModulus());
  
  
          String publicKey = encode(key.getModulus().toByteArray()) + encode(key.getPublicExponent().toByteArray());
  
  
          // Build the Signature
          Signature sig = Signature.getInstance("SHA256withRSA");
          sig.initSign(keyPair.getPrivate());
          seedData = seedData + "ABC";
  
          sig.update(seedData.getBytes("UTF-8"));
          String signature = encode(sig.sign());
  
          signatureString.append(signature);
          // Embed the public key in the signature
          int publicKeyLocation = signatureString.charAt(signatureString.length() - 2) % 40;
          // Add some random bytes at the end
          int randomBytesToAdd = signatureString.charAt(signatureString.length() - 10) % 9;
          System.out.println("Random bytes to add");
          System.out.println(randomBytesToAdd);
          System.out.println("Adding Random bytes that are:");
          String s = randomString(randomBytesToAdd);
          signatureString.insert(publicKeyLocation, s);
      } catch (Exception e) {
  
      }
  
      return signatureString.toString();
  }
  </code></pre>
  
  <p><strong>Now this encoded string in passed to .NET application</strong>
  Where in the .NET application i used to get public key and signature</p>
  
  <pre><code> protected RSAParameters GetPublicKey(string encodedString)
      {
          int bytesToSkip = encodedString.Substring((encodedString.Length - 10), 1).ToCharArray()[0] % 9;
          int publicKeyStarts = bytesToSkip + encodedString.Substring((encodedString.Length - 2), 1).ToCharArray()[0] % 40;
          string keyString = encodedString.Substring(publicKeyStarts, 176);
          string modulusString = keyString.Substring(0, 172);
          string exponentString = keyString.Substring(172);
          RSAParameters publicKey = new RSAParameters();
          publicKey.Modulus = new byte[128];
          Array.Copy(System.Convert.FromBase64String(modulusString), 1, publicKey.Modulus, 0, 128);
          publicKey.Exponent = System.Convert.FromBase64String(exponentString);
          return publicKey;
      }
  
      protected byte[] GetSignature(string encodedString)
      {
          int bytesToSkip = encodedString.Substring((encodedString.Length - 10), 1).ToCharArray()[0] % 9;
          int publicKeyStarts = bytesToSkip + encodedString.Substring((encodedString.Length - 2), 1).ToCharArray()[0] % 40;
          String digSig = encodedString.Substring(0, publicKeyStarts - bytesToSkip) + encodedString.Substring(publicKeyStarts + 176);
          return System.Convert.FromBase64String(digSig);
      }
  </code></pre>
  
  <p><strong>If I change the key pair size from 1024 to 2048 what are the changes i need to make in my .NET code</strong></p>
  
  <p><strong>The answer I am looking for is: from the generated RSA public key how can I get length of Modulus and Exponent</strong> </p>