package edu.stanford.rt.datatype; import java.security.PublicKey; import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; import java.security.spec.DSAPublicKeySpec; import java.security.spec.RSAPublicKeySpec; import java.security.spec.InvalidKeySpecException; import java.security.interfaces.DSAPublicKey; import java.security.interfaces.RSAPublicKey; /** * @author Ninghui Li, Sandra Qiu
* * Note: This class is not useable. */ public class KeyValue implements DataValue { private KeyType type; private PublicKey value; public KeyValue(KeyType type, PublicKey value) { this.type = type; this.value = value; } public KeyType getType() { return type; } public PublicKey getValue() { return value; } public String toString() { KeyFactory kf = null; StringBuffer sb = new StringBuffer(); if(value instanceof DSAPublicKey) { try { kf = KeyFactory.getInstance("DSA"); DSAPublicKeySpec dsaPubKeySpec = (DSAPublicKeySpec)kf.getKeySpec(value, DSAPublicKeySpec.class); sb.append("DSA Public Key: \n") .append("\nG = ").append(dsaPubKeySpec.getG()) .append("\nP = ").append(dsaPubKeySpec.getP()) .append("\nQ = ").append(dsaPubKeySpec.getQ()) .append("\nY = ").append(dsaPubKeySpec.getY()); } catch(NoSuchAlgorithmException e1) { e1.printStackTrace(); } catch(InvalidKeySpecException e2) { e2.printStackTrace(); } } else if(value instanceof RSAPublicKey) { try { kf = KeyFactory.getInstance("RSA"); RSAPublicKeySpec rsaPubKeySpec = (RSAPublicKeySpec)kf.getKeySpec(value, RSAPublicKeySpec.class); sb.append("DSA Public Key: \n") .append("\nModulus = ").append(rsaPubKeySpec.getModulus()) .append("\nExponent = ").append(rsaPubKeySpec.getPublicExponent()); } catch(NoSuchAlgorithmException e1) { e1.printStackTrace(); } catch(InvalidKeySpecException e2) { e2.printStackTrace(); } } else { // TODO: any } return sb.toString(); } }