title: RSA SignatureException: Signature length not correct
body: <p>I am having issues signing an rsa signature. I have a signature that has been encrypted with a private key. I have an issue when trying to validate it with the public key however. I get the following exception:</p>
  
  <pre><code>java.security.SignatureException: Signature length not correct: got 336 but was expecting 128
      at sun.security.rsa.RSASignature.engineVerify(RSASignature.java:189)
      at java.security.Signature$Delegate.engineVerify(Signature.java:1219)
      at java.security.Signature.verify(Signature.java:652)
      at XmlReader.main(XmlReader.java:65)
  </code></pre>
  
  <p>I have retrieved my signature and public key the following way:</p>
  
  <pre><code>    BigInteger modulus = new BigInteger(Base64.getDecoder().decode(publicKeyString));
      BigInteger exponent = new BigInteger(Base64.getDecoder().decode("AQAB"));
  
      RSAPublicKeySpec keySpec = new RSAPublicKeySpec(modulus, exponent);
      KeyFactory keyFactory = KeyFactory.getInstance("RSA");
      PublicKey pubKey = keyFactory.generatePublic(keySpec);
  
      byte[] sigToVerify = Base64.getDecoder().decode(signatureString);
      Signature sig = Signature.getInstance("MD5WithRSA");
      sig.initVerify(pubKey);
      boolean verifies = sig.verify(sigToVerify);
  </code></pre>
  
  <p>The application fails at the last line. Any thoughts as to where this exception is caused?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Added data for signature to be verified:</p>
  
  <pre><code>        String data = "...." //hidden since sensitive data
          byte[] dataBytes = Base64.getEncoder().encode(data.getBytes());
          dataBytes = Base64.getDecoder().decode(dataBytes);
  </code></pre>