You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@santuario.apache.org by Carlos González <cg...@extendnow.com> on 2002/10/25 10:55:49 UTC

PROBLEM WHILE DECRYPTING

Hi All!

I have problems when decrypting a node....

method to encrypt

	public void encrypt(Element element, X509Certificate certificate)
throws Exception
		{
		KeyStore keystore = SecurityHelper.loadKeyStore(new
File(properties.getProperty("keystore.file")),
properties.getProperty("keystore.password"));

		X509Certificate owncertificate =
SecurityHelper.loadX509Certificate(keystore,
properties.getProperty("xmlep.encryption.certificate.alias"));
		
		X509Data x509data = new
X509Data(element.getOwnerDocument());
		x509data.add(xmlx509certificate);*/

		KeyInfo keyinfo = new KeyInfo(element.getOwnerDocument());
	
        javax.crypto.KeyGenerator keygen =
javax.crypto.KeyGenerator.getInstance("DESede");
        Key key = keygen.generateKey();

		EncryptionMethod em = new
EncryptionMethod(element.getOwnerDocument(),
EncryptionConstants.ALGO_ID_KEYTRANSPORT_RSA15);

		byte ciphertext[] = em.wrap(key,
owncertificate.getPublicKey());

		System.out.println("Ciphertext "+ciphertext);

	    KeyInfo wrapkeyinfo = new KeyInfo(element.getOwnerDocument());
		
		wrapkeyinfo.add(new
org.apache.xml.security.keys.content.KeyName(element.getOwnerDocument(),"Key
"));

		EncryptedKey cryptoKey = new
EncryptedKey(element.getOwnerDocument(), em, wrapkeyinfo, new
CipherData(element.getOwnerDocument(), ciphertext), null, null,
Long.toString(System.currentTimeMillis()), null,
EncryptionConstants.TYPE_CONTENT, null);
	
		EncryptedData cryptoData = new
EncryptedData(element.getOwnerDocument(),
EncryptionConstants.ALGO_ID_BLOCKCIPHER_TRIPLEDES, null, keyinfo, null,
element.getNodeName());
	


		cryptoData.encryptElementAndReplace(element, key);
		System.out.println("Appending
"+cryptoKey.getElement().getNodeName()+"with children
"+cryptoKey.getElement().getChildNodes().getLength());

		keyinfo.getElement().appendChild(cryptoKey.getElement());

	
XMLUtils.outputDOMc14nWithComments(element.getOwnerDocument(), new
FileOutputStream(new File("test2.xml")));
		}
	

Method to decrypt

public void decrypt(Node node) throws Exception
		{
		KeyStore keystore = SecurityHelper.loadKeyStore(new
File(properties.getProperty("keystore.file")),
properties.getProperty("keystore.password"));

		PrivateKey privatekey = (PrivateKey)
keystore.getKey(properties.getProperty("xmlep.encryption.privatekey.alias"),
properties.getProperty("xmlep.encryption.privatekey.password").toCharArray()
);
	

		Element ed = (Element)XPathAPI.selectSingleNode(node,
"//*[local-name()='EncryptedData']");

		Element ek = (Element)XPathAPI.selectSingleNode(ed,
"//*[local-name()='EncryptedKey']");

		EncryptedKey encryptedkey = new EncryptedKey(ek, "");

		EncryptionMethod wrapmethod =
encryptedkey.getEncryptionMethod();

		byte[] ciphertext =
encryptedkey.getCipherData().getCipherValue().getCipherText();

		System.out.println("Ciphertext "+ciphertext);
		System.out.println("PrivateKey "+privatekey);
		Key unwrapped = wrapmethod.unwrap(ciphertext, privatekey,
EncryptionConstants.ALGO_ID_KEYTRANSPORT_RSA15);

		System.out.println("Encryption method
"+encryptedkey.getEncryptionMethod());

		}


The XML produced is
....
	<xenc:EncryptedData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"
Id="params" Type="http://www.w3.org/2001/04/xmlenc#Element">
<xenc:EncryptionMethod
Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc"></xenc:Encryption
Method>
<ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<xenc:EncryptedKey Type="http://www.w3.org/2001/04/xmlenc#Content">
<xenc:EncryptionMethod
Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5"></xenc:EncryptionMethod
>
<ds:KeyInfo>
<ds:KeyName>Key</ds:KeyName>
</ds:KeyInfo>
<xenc:CipherData>
<xenc:CipherValue>lr/c2CAjWgTL5zC4KeqmTKHkV3dNIyaSdLGKl02vwhZHFobYen1/a/tFqe
3WB7d7s5YvEn03d5Wo
uJLg+7WXoOVnd1QjH0CcaNukjzbAX4HiBLEzF57VT8T7B54ODyI8RZPpVvBiEtQqTPrBWy8yMKOu
Shf2d/k2gcI5KDdY5Ow=</xenc:CipherValue>
</xenc:CipherData>
<xenc:CarriedKeyName>1035474521262</xenc:CarriedKeyName>
</xenc:EncryptedKey></ds:KeyInfo>
<xenc:CipherData>
<xenc:CipherValue>G07lNozwGdB33xr77LY0heiVvB0ggbnMXH7gzWKW73pu2hGmTnfm8f8SDI
31BOjeZ1R8+C3sWpQF
LTQdu/aMFaSCD4fbdJuiGil7YKKT1bIDi8sqwGg2Ke7DYJlkhsJBret/iGcoW6khhG41oXLlxloW
QLopE5DePgUQoYhRFoA=</xenc:CipherValue>
</xenc:CipherData>
</xenc:EncryptedData>

.....

And the exception

Exception in thread "main"
org.apache.xml.security.exceptions.XMLSecurityExcepti
on: {0}
        at
org.apache.xml.security.algorithms.encryption.EncryptionMethod.<init>
(EncryptionMethod.java:242)
        at
org.apache.xml.security.algorithms.encryption.EncryptionMethod.<init>
(EncryptionMethod.java:128)
        at
com.extendnow.gfp.transport.security.XMLEncryptionProcessor.encrypt(X
MLEncryptionProcessor.java:100)
        at
com.extendnow.gfp.transport.security.XMLEncryptionProcessor.main(XMLE
ncryptionProcessor.java:55)


Have you got any hints??

Thank you in advance...

Carlos Gonzalez-Cadenas
e-xtendnow