title: RSA key signing and verifying in Java(Android)
body: <p>I am having a slight problem with understanding the concept of RSA signing and verifying.
  The problem is that I can create key pair (public and private key) which is perfectly fine.</p>
  
  <pre><code>        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
          random = SecureRandom.getInstance("SHA1PRNG");
          keyGen.initialize(2048, random);
          KeyPair pair = keyGen.generateKeyPair();
          myPrivateKey = pair.getPrivate();
          myPublicKey = pair.getPublic();
  </code></pre>
  
  <p>Signing and verifying are as follows:</p>
  
  <pre><code>        //Singing with private key
          Signature s = Signature.getInstance("SHA1withRSA");
          s.initSign(javaPrivateKey);
  
          //Verifying with public key
          Signature s = Signature.getInstance("SHA1withRSA");
          s.initVerify(javaPublicKey);
  </code></pre>
  
  <p>When I print myPrivateKey and myPublicKey, I see modulus(n) and public exponent(e) are the same for public and private key.</p>
  
  <p>I have converted public and private keys to base64 and hex and I get the different values which is perfectly fine. However, I cannot sign the message with base64 or hex. I can sign it only with what I get from this: </p>
  
  <pre><code>         myPrivateKey = pair.getPrivate();  
  </code></pre>
  
  <p>I am aware of that verifying needs to be done with the public key which is visible to everyone. When the message is verified by the receiver, does the receiver use modulus and exponent only? What part of the public key needs to be shared by the sender? Modulus and exponent or Base64 or hex value of the key?</p>