I am following simple algorithm i.e. first decrypt to get symmetric key(that was used to encrypt the data) using my private RSA key, and then use this symmetric key to decrypt data.
The symmetric key is encrypted with my public rsa key.
But I am not able to get neither the Decrypted Symmetric key or Decrypted Data...........
Can anyone suggest what I am doing wrong. See my code below
String certPath = "C:/somepath/temp.keystore"; // Keystore path
KeyStore ks = KeyStore.getInstance("JKS"); // Gets the Java Keystore
ks.load(new FileInputStream(new File(certPath)),password.toCharArray()); // loads the certificate
RSAPrivateKey privKey = (RSAPrivateKey) ks.getKey("SamlTest",password.toCharArray());
PublicKey pubKey = ks.getCertificate("SamlTest").getPublicKey();
/* we will use this part to get cipher data i.e. decryption */
// step 1. initialize cipher
XMLCipher xmlCipher = XMLCipher.getInstance();// i know client used AES/CBC/ISO10126Padding as cipher.algoritm, do we specify in getInstance() ????
// step 2. Init in UNWRAP_MODE with privKey as key because we need to get Symmetric key first
xmlCipher.init(XMLCipher.UNWRAP_MODE, privKey);
//cipher.algoritm RSA/ECB/PKCS1Padding was used for Encrypting key 'AES' with key 'RSA' with algorithm URI 'http://www.w3.org/2001/04/xmlenc#rsa-1_5'
// step 3. doFinal on dom,encryptedKeyElement
xmlCipher.doFinal(doc, encryptedDataElementKey);// where Element encryptedDataElementKey = (Element) doc.getElementsByTagNameNS(namespaceURI,localName_Key).item(0);
//step 4. now doc will have decrypted key, so Init in DECRYPT_MODE using new decrypted key
xmlCipher.init(XMLCipher.DECRYPT_MODE , null);
// we need to setKEK, so use Symmetric key here
xmlCipher.setKEK(symKey);
// do the actual decryption
xmlCipher.doFinal(doc, encryptedDataElement);//where dataElement is Element encryptedDataElement = (Element) doc.getElementsByTagNameNS(namespaceURI,localName_Data).item(0);
Any comments are welcome