KeyStoreHelper { public byte[] signDataWithPrivateKey(byte[] data) throws UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException, InvalidKeyException, SignatureException { if( pvtKeyStore == null ) { throw new RuntimeException( "Key store with private key not configured. Please configure it properly before using signed serialization." ); } PrivateKey pvtkey = (PrivateKey) pvtKeyStore.getKey( pvtKeyAlias, pvtKeyPassword ); Signature sig = Signature.getInstance( "MD5withRSA" ); sig.initSign( pvtkey ); sig.update( data ); return sig.sign(); } KeyStoreHelper(); byte[] signDataWithPrivateKey(byte[] data); boolean checkDataWithPublicKey(final String publicKeyAlias,
                                          final byte[] data,
                                          final byte[] signature); String getPasswordKey(String pwdKeyAlias, char[] pwdKeyPassword); boolean isSigned(); URL getPvtKeyStoreURL(); char[] getPvtKeyStorePwd(); String getPvtKeyAlias(); char[] getPvtKeyPassword(); URL getPubKeyStoreURL(); char[] getPubKeyStorePwd(); KeyStore getPvtKeyStore(); KeyStore getPubKeyStore();  }
@Test public void testSignDataWithPrivateKey() throws UnsupportedEncodingException, UnrecoverableKeyException, InvalidKeyException, KeyStoreException, NoSuchAlgorithmException, SignatureException { final URL serverKeyStoreURL = getClass().getResource(KEYSTORE_SERVER_RESOURCE_NAME); System.setProperty( KeyStoreConstants.PROP_SIGN, Boolean.TRUE.toString() ); System.setProperty( KeyStoreConstants.PROP_PVT_KS_URL, serverKeyStoreURL.toExternalForm() ); System.setProperty(KeyStoreConstants.PROP_PVT_KS_PWD, KEYSTORE_SERVER_PASSWORD); System.setProperty( KeyStoreConstants.PROP_PVT_ALIAS, KEY_ALIAS ); System.setProperty( KeyStoreConstants.PROP_PVT_PWD, KEY_PASSWORD ); final KeyStoreHelper serverHelper = new KeyStoreHelper(); final byte[] data = "Hello World".getBytes("UTF8" ); final byte[] signature = serverHelper.signDataWithPrivateKey(data ); final URL clientKeyStoreURL = getClass().getResource(KEYSTORE_CLIENT_RESOURCE_NAME ); System.setProperty( KeyStoreConstants.PROP_SIGN, Boolean.TRUE.toString() ); System.setProperty( KeyStoreConstants.PROP_PUB_KS_URL, clientKeyStoreURL.toExternalForm() ); System.setProperty( KeyStoreConstants.PROP_PUB_KS_PWD, KEYSTORE_CLIENT_PASSWORD ); final KeyStoreHelper clientHelper = new KeyStoreHelper( ); assertTrue( clientHelper.checkDataWithPublicKey( KEY_ALIAS, data, signature ) ); assertFalse( clientHelper.checkDataWithPublicKey( KEY_ALIAS, "fake".getBytes( "UTF8" ), signature ) ); }