@Test public void testBasicAgainstJce() throws Exception { int keySize = 2048; KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(keySize); KeyPair keyPair = keyGen.generateKeyPair(); RSAPublicKey pub = (RSAPublicKey) keyPair.getPublic(); RSAPrivateCrtKey priv = (RSAPrivateCrtKey) keyPair.getPrivate(); String message = "Hello"; RsaSsaPkcs1SignJce signer = new RsaSsaPkcs1SignJce(priv, HashType.SHA256); byte[] signature = signer.sign(message.getBytes("UTF-8")); Signature verifier = Signature.getInstance("SHA256withRSA"); verifier.initVerify(pub); verifier.update(message.getBytes("UTF-8")); assertTrue(verifier.verify(signature)); }
@Override public byte[] sign(final byte[] data) throws GeneralSecurityException { Signature signer = EngineFactory.SIGNATURE.getInstance(signatureAlgorithm); signer.initSign(privateKey); signer.update(data); byte[] signature = signer.sign(); Signature verifier = EngineFactory.SIGNATURE.getInstance(signatureAlgorithm); verifier.initVerify(publicKey); verifier.update(data); if (!verifier.verify(signature)) { throw new java.lang.RuntimeException("Security bug: RSA signature computation error"); } return signature; }
RsaSsaPkcs1SignJce implements PublicKeySign { @Override public byte[] sign(final byte[] data) throws GeneralSecurityException { Signature signer = EngineFactory.SIGNATURE.getInstance(signatureAlgorithm); signer.initSign(privateKey); signer.update(data); byte[] signature = signer.sign(); Signature verifier = EngineFactory.SIGNATURE.getInstance(signatureAlgorithm); verifier.initVerify(publicKey); verifier.update(data); if (!verifier.verify(signature)) { throw new java.lang.RuntimeException("Security bug: RSA signature computation error"); } return signature; } }
RsaSsaPkcs1SignJce implements PublicKeySign { @Override public byte[] sign(final byte[] data) throws GeneralSecurityException { Signature signer = EngineFactory.SIGNATURE.getInstance(signatureAlgorithm); signer.initSign(privateKey); signer.update(data); byte[] signature = signer.sign(); Signature verifier = EngineFactory.SIGNATURE.getInstance(signatureAlgorithm); verifier.initVerify(publicKey); verifier.update(data); if (!verifier.verify(signature)) { throw new java.lang.RuntimeException("Security bug: RSA signature computation error"); } return signature; } RsaSsaPkcs1SignJce(final RSAPrivateCrtKey priv, HashType hash); }
RsaSsaPkcs1SignJce implements PublicKeySign { @Override public byte[] sign(final byte[] data) throws GeneralSecurityException { Signature signer = EngineFactory.SIGNATURE.getInstance(signatureAlgorithm); signer.initSign(privateKey); signer.update(data); byte[] signature = signer.sign(); Signature verifier = EngineFactory.SIGNATURE.getInstance(signatureAlgorithm); verifier.initVerify(publicKey); verifier.update(data); if (!verifier.verify(signature)) { throw new java.lang.RuntimeException("Security bug: RSA signature computation error"); } return signature; } RsaSsaPkcs1SignJce(final RSAPrivateCrtKey priv, HashType hash); @Override byte[] sign(final byte[] data); }
RsaSsaPkcs1SignJce implements PublicKeySign { @Override public byte[] sign(final byte[] data) throws GeneralSecurityException { Signature signer = EngineFactory.SIGNATURE.getInstance(signatureAlgorithm); signer.initSign(privateKey); signer.update(data); byte[] signature = signer.sign(); Signature verifier = EngineFactory.SIGNATURE.getInstance(signatureAlgorithm); verifier.initVerify(publicKey); verifier.update(data); if (!verifier.verify(signature)) { throw new java.lang.RuntimeException("Security bug: RSA signature computation error"); } return signature; } RsaSsaPkcs1SignJce(final RSAPrivateCrtKey priv, HashType hash); @Override byte[] sign(final byte[] data);  }