}
protected HBCIKey getElementKey(Element root,String owner,String type,String part)
throws Exception
{
HBCIKey ret=null;
NodeList keys=root.getElementsByTagName("key");
int len=keys.getLength();
for (int i=0;i<len;i++) {
Node n=keys.item(i);
if (n.getNodeType()==Node.ELEMENT_NODE) {
Element keynode=(Element)n;
if (keynode.getAttribute("owner").equals(owner) &&
keynode.getAttribute("type").equals(type) &&
keynode.getAttribute("part").equals(part)) {
Key key;
if (part.equals("public")) {
RSAPublicKeySpec spec=new RSAPublicKeySpec(new BigInteger(getElementValue(keynode,"modulus")),
new BigInteger(getElementValue(keynode,"exponent")));
key=KeyFactory.getInstance("RSA").generatePublic(spec);
} else {
String modulus=getElementValue(keynode,"modulus");
String privexponent=getElementValue(keynode,"exponent");
String pubexponent=getElementValue(keynode,"pubexponent");
String p=getElementValue(keynode,"p");
String q=getElementValue(keynode,"q");
String dP=getElementValue(keynode,"dP");
String dQ=getElementValue(keynode,"dQ");
String qInv=getElementValue(keynode,"qInv");
if (privexponent==null) {
// only CRT
HBCIUtils.log("private "+type+" key is CRT-only",HBCIUtils.LOG_DEBUG);
key=new RSAPrivateCrtKey2(new BigInteger(p),
new BigInteger(q),
new BigInteger(dP),
new BigInteger(dQ),
new BigInteger(qInv));
} else if (p==null) {
// only exponent
HBCIUtils.log("private "+type+" key is exponent-only",HBCIUtils.LOG_DEBUG);
RSAPrivateKeySpec spec=new RSAPrivateKeySpec(new BigInteger(modulus),
new BigInteger(privexponent));
key=KeyFactory.getInstance("RSA").generatePrivate(spec);
} else {
// complete data
HBCIUtils.log("private "+type+" key is fully specified",HBCIUtils.LOG_DEBUG);
RSAPrivateCrtKeySpec spec=new RSAPrivateCrtKeySpec(new BigInteger(modulus),
new BigInteger(pubexponent),
new BigInteger(privexponent),
new BigInteger(p),
new BigInteger(q),
new BigInteger(dP),
new BigInteger(dQ),
new BigInteger(qInv));
key=KeyFactory.getInstance("RSA").generatePrivate(spec);
}
}
ret=new HBCIKey(getElementValue(keynode,"country"),
getElementValue(keynode,"blz"),
getElementValue(keynode,"userid"),
getElementValue(keynode,"keynum"),
getElementValue(keynode,"keyversion"),
key);