continuation to this link we need to do below configuration setup:
Generate jks file:
1) keytool -genkeypair -alias tomcat -keyalg RSA -keysize 1024 -keypass changeit -keystore tomcat.jks
2) To view public key :
keytool -list -rfc -keystore C:\Users\rkumarga\tomcat.jks -alias tomcat -storepass changeit
3) in tomcat.properties file, use these values:
org.apache.ws.security.crypto.provider=org.apache.ws.security.components.crypto.Merlin
org.apache.ws.security.crypto.merlin.keystore.type=jks
org.apache.ws.security.crypto.merlin.keystore.password=changeit
org.apache.ws.security.crypto.merlin.keystore.alias=tomcat
org.apache.ws.security.crypto.merlin.keystore.file=C:/Users/kuku/tomcat.jks
Open postman tool and use the below url and click on Send, we get response in encrypted format:
http://localhost:8080/MyWS/rest/myRestServices/myCodes?_type=xml
This response from server has been encrypted using public key. to see the decrypted data of the response, follow the below steps:
Use the below code to get the private key from tomcat.jks file:
Generate jks file:
1) keytool -genkeypair -alias tomcat -keyalg RSA -keysize 1024 -keypass changeit -keystore tomcat.jks
2) To view public key :
keytool -list -rfc -keystore C:\Users\rkumarga\tomcat.jks -alias tomcat -storepass changeit
3) in tomcat.properties file, use these values:
org.apache.ws.security.crypto.provider=org.apache.ws.security.components.crypto.Merlin
org.apache.ws.security.crypto.merlin.keystore.type=jks
org.apache.ws.security.crypto.merlin.keystore.password=changeit
org.apache.ws.security.crypto.merlin.keystore.alias=tomcat
org.apache.ws.security.crypto.merlin.keystore.file=C:/Users/kuku/tomcat.jks
Open postman tool and use the below url and click on Send, we get response in encrypted format:
http://localhost:8080/MyWS/rest/myRestServices/myCodes?_type=xml
This response from server has been encrypted using public key. to see the decrypted data of the response, follow the below steps:
Use the below code to get the private key from tomcat.jks file:
import org.apache.commons.codec.binary.Base64; import java.io.FileInputStream; import java.security.Key; import java.security.KeyStore; public class DumpPrivateKey { static public void main(String[] args) throws Exception { final String keystoreName = "C:/Users/kuku/tomcat.jks"; final String keystorePassword = "changeit"; final String alias = "tomcat"; KeyStore ks = KeyStore.getInstance("jks"); ks.load(new FileInputStream(keystoreName), keystorePassword.toCharArray()); Key key = ks.getKey(alias, keystorePassword.toCharArray()); String b64 = new String(Base64.encodeBase64(key.getEncoded(), true)); System.out.println("-----BEGIN PRIVATE KEY-----"); System.out.println(b64); System.out.println("-----END PRIVATE KEY-----"); } }
output of the above code:
-----BEGIN PRIVATE KEY----- MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKVkFxAPT+WrykynfxMJVnAKM3i2 IGGQQhTFiyjgI6QnVKjUsb6rOUtACuVacSVKxwDLHfLV6JxOQX5ulZz9sNcFqOJ/Hif+U2Bc0swT dVnYFA+tyjQL+4gIu+WqdGtxWWJ5O2pyW1lvv5KtJsV2itaYtq2GSl5qLhUMMd1xyOAZAgMBAAEC gYA97WNBTSpyaceeuhy3qQhnfTpWmYM4ZMaJdoorrPlLbrqhWLqRFRWn2nv58spo1z36SxOUqImn dRnK2GdG2CrNTiCI5JyomGn2UGwV1I/uSsj9p2aF81O71dEmDrCDafgbqrjKrRPE5MBGPVChbTo2 mQ/ivYVprhkIbvAiQtNsAQJBANFZV1MbGSmQjvqDbaztWauOvk/oAeRv7BXTOLGpheiR/HdvpS4v diipB3boa2UFfAl3fuRxnkMIKLbB1sFZzYECQQDKPxdxrxtBWy+AOdVtWJj1f1s7TxCC+luInRWB g2cJZ5S26wWVT05W9uGzVumOiSnTuvQyPVO2qKqkMjOp4A6ZAkEAo7Ej4suRhTqPUbpqi4ibUNWw YXKb6C7LftVFw0U2e3+K1SjrE43w6iIgwA1DzB3Zut1Azwc4JFnA3AX9EfpSAQJANIB+YIe5hpTa Vuosio9GXeyqzVGwEqBVZLiKna1z8RAPnhCSyXjl+FNssfas+NCIqCtPOz+KXM+aRLe3i7DbeQJB AM6GMbwSfi6qXpaHhLBd+om7t0KHf4elSc5PNA7fNhPoIrgmi3WhKTcAP5MXM8KxtA96rgDZKTaX PGHB9FhPK10= -----END PRIVATE KEY-----
copy past the encrypted response and this Private key in the below url and click on Decrypt XML button, we can see the decrypted XML: