You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@santuario.apache.org by Hye-Jung Kim <Hy...@orionhealth.com> on 2004/02/10 22:55:38 UTC
Decrypt using KeyInfo
Hello,
I am trying to decrypt the encrypted xml data using KeyInfo.
I extracted CipherValue of KeyInfo which is kek(key encryption key) to
pass on to my XmlCipher however, I am getting the exception
java.security.NoSuchAlgorithmException: Algorithm
http://www.w3.org/2001/04/xmlenc#kw-tripledes not available. I've tried
different provider but still failed to retrieve kek value from KeyInfo.
I am not sure what I am doing wrong to retrieve kek value in order to
decrypt xml data from KeyInfo ?
Thanks in advance.
hyejung
========== my java code =====================
Key key = null;
Element e = (Element)
encryptedDoc.getElementsByTagNameNS(EncryptionConstants.EncryptionSpecNS
,
EncryptionConstants._TAG_ENCRYPTEDDATA).item(0);
cipher = XMLCipher.getInstance();
cipher.init(XMLCipher.DECRYPT_MODE, null);
EncryptedData encryptedData = cipher.loadEncryptedData(encryptedDoc,
e);
if(encryptedData == null) {
throw new Exception("EncryptedData is null");
}else if (encryptedData.getKeyInfo() == null) {
throw new Exception("KeyInfo of the EncryptedData is
null");
}
EncryptedKey ek = encryptedData.getKeyInfo().itemEncryptedKey(0);
if (ek != null) {
CipherValue encryptionKeyCipherValue =
ek.getCipherData().getCipherValue();
SecretKeySpec ks = new
SecretKeySpec(encryptionKeyCipherValue.getValue().getBytes(),ek.getEncry
ptionMethod().getAlgorithm());
Key kek = keyFactory.generateSecret(ks);
XMLCipher keyCipher = XMLCipher.getInstance();
keyCipher.init(XMLCipher.UNWRAP_MODE, kek);
key = keyCipher.decryptKey(ek,
encryptedData.getEncryptionMethod().getAlgorithm());
}
//initialize the XMLCipher
XMLCipher cipher3 = XMLCipher.getInstance();
cipher3.init(XMLCipher.DECRYPT_MODE, key);
Document decryptedDoc = cipher.doFinal(encryptedDoc, e);
XMLUtils.outputDOM(decryptedDoc, output);
====== my encrypted xml data ===============
<xenc:EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
<xenc:EncryptionMethod
Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc"
xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" />
<file:///W:/Rhapsody/XMLSecurityFilter/resources/processed/encryptedWith
KeyInfo.xml#> - <ds:KeyInfo
xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<file:///W:/Rhapsody/XMLSecurityFilter/resources/processed/encryptedWith
KeyInfo.xml#> - <xenc:EncryptedKey
xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
<xenc:EncryptionMethod
Algorithm="http://www.w3.org/2001/04/xmlenc#kw-tripledes"
xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" />
<file:///W:/Rhapsody/XMLSecurityFilter/resources/processed/encryptedWith
KeyInfo.xml#> - <xenc:CipherData
xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
<xenc:CipherValue
xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">LYhDusdc65NgbAxiX1uxp7elP
wp4s9qQb6dtXu1uFE6p/Wm3+/fiNQ==</xenc:CipherValue>
</xenc:CipherData>
</xenc:EncryptedKey>
</ds:KeyInfo>
<file:///W:/Rhapsody/XMLSecurityFilter/resources/processed/encryptedWith
KeyInfo.xml#> - <xenc:CipherData
xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
<xenc:CipherValue
xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">TxuXWSIU/xR8OEhVQt1GxIC/9
ItnLWhC1/58eC8p3wPHRoKrO3vYn7LMlCLheprq</xenc:CipherValue>
</xenc:CipherData>
</xenc:EncryptedData>
Re: Decrypt using KeyInfo
Posted by Berin Lautenbach <be...@wingsofhermes.org>.
Hyejung,
Oops. I missed this - apologies!
Did you get this working? My first thought would be that you are trying
to create a key using
SecretKeySpec ks = new
SecretKeySpec(encryptionKeyCipherValue.getValue().getBytes(),
ek.getEncryptionMethod().getAlgorithm());
Which will fail, as the bytes you are using are encrypted bytes from the
doc, and the algorithm is an xml-encryption algorithm, not a JCE algorithm.
If you are still having problems, come back to the list and we will see
what we can do.
Cheers,
Berin
Hye-Jung Kim wrote:
> Hello,
> I am trying to decrypt the encrypted xml data using KeyInfo.
> I extracted CipherValue of KeyInfo which is kek(key encryption key) to
> pass on to my XmlCipher however, I am getting the exception
> java.security.NoSuchAlgorithmException: Algorithm
> http://www.w3.org/2001/04/xmlenc#kw-tripledes not available. I've tried
> different provider but still failed to retrieve kek value from KeyInfo.
> I am not sure what I am doing wrong to retrieve kek value in order to
> decrypt xml data from KeyInfo ?
>
> Thanks in advance.
>
> hyejung
>
> ========== my java code =====================
>
> Key key = null;
> Element e = (Element)
> encryptedDoc.getElementsByTagNameNS(EncryptionConstants.EncryptionSpecNS,
> EncryptionConstants._TAG_ENCRYPTEDDATA).item(0);
> cipher = XMLCipher.getInstance();
> cipher.init(XMLCipher.DECRYPT_MODE, null);
>
> EncryptedData encryptedData = cipher.loadEncryptedData(encryptedDoc, e);
>
> if(encryptedData == null) {
> throw new Exception("EncryptedData is null");
> }else if (encryptedData.getKeyInfo() == null) {
> throw new Exception("KeyInfo of the EncryptedData is null");
> }
> EncryptedKey ek = encryptedData.getKeyInfo().itemEncryptedKey(0);
>
> *if (ek != null) {
> CipherValue encryptionKeyCipherValue =
> ek.getCipherData().getCipherValue();
> SecretKeySpec ks = new
> SecretKeySpec(encryptionKeyCipherValue.getValue().getBytes(),ek.getEncryptionMethod().getAlgorithm());
>
> Key kek = keyFactory.generateSecret(ks);
> XMLCipher keyCipher = XMLCipher.getInstance();
> keyCipher.init(XMLCipher.UNWRAP_MODE, kek);
> key = keyCipher.decryptKey(ek,
> encryptedData.getEncryptionMethod().getAlgorithm());
> }
>
> * //initialize the XMLCipher
> XMLCipher cipher3 = XMLCipher.getInstance();
> cipher3.init(XMLCipher.DECRYPT_MODE, key);
> Document decryptedDoc = cipher.doFinal(encryptedDoc, e);
> XMLUtils.outputDOM(decryptedDoc, output);
>
> ====== my encrypted xml data ===============
> <xenc:EncryptedData Type="*http://www.w3.org/2001/04/xmlenc#Element*"
> xmlns:xenc="*http://www.w3.org/2001/04/xmlenc#*">
> * * <xenc:EncryptionMethod
> Algorithm="*http://www.w3.org/2001/04/xmlenc#tripledes-cbc*"
> xmlns:xenc="*http://www.w3.org/2001/04/xmlenc#*" />
> *-*
> <file:///W:/Rhapsody/XMLSecurityFilter/resources/processed/encryptedWithKeyInfo.xml#>
> <ds:KeyInfo xmlns:ds="*http://www.w3.org/2000/09/xmldsig#*">
> *-*
> <file:///W:/Rhapsody/XMLSecurityFilter/resources/processed/encryptedWithKeyInfo.xml#>
> <xenc:EncryptedKey xmlns:xenc="*http://www.w3.org/2001/04/xmlenc#*">
> * * <xenc:EncryptionMethod
> Algorithm="*http://www.w3.org/2001/04/xmlenc#kw-tripledes*"
> xmlns:xenc="*http://www.w3.org/2001/04/xmlenc#*" />
> *-*
> <file:///W:/Rhapsody/XMLSecurityFilter/resources/processed/encryptedWithKeyInfo.xml#>
> <xenc:CipherData xmlns:xenc="*http://www.w3.org/2001/04/xmlenc#*">
> * * <xenc:CipherValue
> xmlns:xenc="*http://www.w3.org/2001/04/xmlenc#*">*LYhDusdc65NgbAxiX1uxp7elPwp4s9qQb6dtXu1uFE6p/Wm3+/fiNQ==*</xenc:CipherValue>
>
> * * </xenc:CipherData>
> * * </xenc:EncryptedKey>
> * * </ds:KeyInfo>
> *-*
> <file:///W:/Rhapsody/XMLSecurityFilter/resources/processed/encryptedWithKeyInfo.xml#>
> <xenc:CipherData xmlns:xenc="*http://www.w3.org/2001/04/xmlenc#*">
> * * <xenc:CipherValue
> xmlns:xenc="*http://www.w3.org/2001/04/xmlenc#*">*TxuXWSIU/xR8OEhVQt1GxIC/9ItnLWhC1/58eC8p3wPHRoKrO3vYn7LMlCLheprq*</xenc:CipherValue>
>
> * * </xenc:CipherData>
> * * </xenc:EncryptedData>
Re: Decrypt using KeyInfo
Posted by Vishal Mahajan <Vi...@Sun.COM>.
Hi Hyejung,
Are you initializing the SecretKeyFactory using an algoritm URI? That
could be incorrect. I think you need to supply the corresponding JCE id
instead.
Regards,
Vishal
Hye-Jung Kim wrote:
> Hello,
> I am trying to decrypt the encrypted xml data using KeyInfo.
> I extracted CipherValue of KeyInfo which is kek(key encryption key)
> to pass on to my XmlCipher however, I am getting the exception
> java.security.NoSuchAlgorithmException: Algorithm
> http://www.w3.org/2001/04/xmlenc#kw-tripledes not available. I've
> tried different provider but still failed to retrieve kek value from
> KeyInfo. I am not sure what I am doing wrong to retrieve kek value in
> order to decrypt xml data from KeyInfo ?
>
> Thanks in advance.
>
> hyejung
>
> ========== my java code =====================
>
> Key key = null;
> Element e = (Element)
> encryptedDoc.getElementsByTagNameNS(EncryptionConstants.EncryptionSpecNS,
> EncryptionConstants._TAG_ENCRYPTEDDATA).item(0);
> cipher = XMLCipher.getInstance();
> cipher.init(XMLCipher.DECRYPT_MODE, null);
>
> EncryptedData encryptedData =
> cipher.loadEncryptedData(encryptedDoc, e);
>
> if(encryptedData == null) {
> throw new Exception("EncryptedData is null");
> }else if (encryptedData.getKeyInfo() == null) {
> throw new Exception("KeyInfo of the EncryptedData is null");
> }
> EncryptedKey ek = encryptedData.getKeyInfo().itemEncryptedKey(0);
>
> *if (ek != null) {
> CipherValue encryptionKeyCipherValue =
> ek.getCipherData().getCipherValue();
> SecretKeySpec ks = new
> SecretKeySpec(encryptionKeyCipherValue.getValue().getBytes(),ek.getEncryptionMethod().getAlgorithm());
>
> Key kek = keyFactory.generateSecret(ks);
> XMLCipher keyCipher = XMLCipher.getInstance();
> keyCipher.init(XMLCipher.UNWRAP_MODE, kek);
> key = keyCipher.decryptKey(ek,
> encryptedData.getEncryptionMethod().getAlgorithm());
> }
>
> * //initialize the XMLCipher
> XMLCipher cipher3 = XMLCipher.getInstance();
> cipher3.init(XMLCipher.DECRYPT_MODE, key);
> Document decryptedDoc = cipher.doFinal(encryptedDoc, e);
> XMLUtils.outputDOM(decryptedDoc, output);
>
> ====== my encrypted xml data ===============
> <xenc:EncryptedData Type="*http://www.w3.org/2001/04/xmlenc#Element*"
> xmlns:xenc="*http://www.w3.org/2001/04/xmlenc#*">
> * * <xenc:EncryptionMethod
> Algorithm="*http://www.w3.org/2001/04/xmlenc#tripledes-cbc*"
> xmlns:xenc="*http://www.w3.org/2001/04/xmlenc#*" />
> *-*
> <file:///W:/Rhapsody/XMLSecurityFilter/resources/processed/encryptedWithKeyInfo.xml#>
> <ds:KeyInfo xmlns:ds="*http://www.w3.org/2000/09/xmldsig#*">
> *-*
> <file:///W:/Rhapsody/XMLSecurityFilter/resources/processed/encryptedWithKeyInfo.xml#>
> <xenc:EncryptedKey xmlns:xenc="*http://www.w3.org/2001/04/xmlenc#*">
> * * <xenc:EncryptionMethod
> Algorithm="*http://www.w3.org/2001/04/xmlenc#kw-tripledes*"
> xmlns:xenc="*http://www.w3.org/2001/04/xmlenc#*" />
> *-*
> <file:///W:/Rhapsody/XMLSecurityFilter/resources/processed/encryptedWithKeyInfo.xml#>
> <xenc:CipherData xmlns:xenc="*http://www.w3.org/2001/04/xmlenc#*">
> * * <xenc:CipherValue xmlns:xenc="*http://www.w3.org/2001/04/xmlenc#*">*LYhDusdc65NgbAxiX1uxp7elPwp4s9qQb6dtXu1uFE6p/Wm3+/fiNQ==*</xenc:CipherValue>
> * * </xenc:CipherData>
> * * </xenc:EncryptedKey>
> * * </ds:KeyInfo>
> *-*
> <file:///W:/Rhapsody/XMLSecurityFilter/resources/processed/encryptedWithKeyInfo.xml#>
> <xenc:CipherData xmlns:xenc="*http://www.w3.org/2001/04/xmlenc#*">
> * * <xenc:CipherValue xmlns:xenc="*http://www.w3.org/2001/04/xmlenc#*">*TxuXWSIU/xR8OEhVQt1GxIC/9ItnLWhC1/58eC8p3wPHRoKrO3vYn7LMlCLheprq*</xenc:CipherValue>
> * * </xenc:CipherData>
> * * </xenc:EncryptedData>