1 | package edu.stanford.rt.datatype; |
---|
2 | |
---|
3 | import java.security.PublicKey; |
---|
4 | import java.security.KeyFactory; |
---|
5 | import java.security.NoSuchAlgorithmException; |
---|
6 | |
---|
7 | import java.security.spec.DSAPublicKeySpec; |
---|
8 | import java.security.spec.RSAPublicKeySpec; |
---|
9 | import java.security.spec.InvalidKeySpecException; |
---|
10 | |
---|
11 | import java.security.interfaces.DSAPublicKey; |
---|
12 | import java.security.interfaces.RSAPublicKey; |
---|
13 | |
---|
14 | /** |
---|
15 | * @author Ninghui Li, Sandra Qiu <br> |
---|
16 | * |
---|
17 | * Note: This class is not useable. |
---|
18 | */ |
---|
19 | public class KeyValue implements DataValue |
---|
20 | { |
---|
21 | private KeyType type; |
---|
22 | |
---|
23 | private PublicKey value; |
---|
24 | |
---|
25 | public KeyValue(KeyType type, PublicKey value) |
---|
26 | { |
---|
27 | this.type = type; |
---|
28 | this.value = value; |
---|
29 | } |
---|
30 | |
---|
31 | public KeyType getType() |
---|
32 | { |
---|
33 | return type; |
---|
34 | } |
---|
35 | |
---|
36 | public PublicKey getValue() |
---|
37 | { |
---|
38 | return value; |
---|
39 | } |
---|
40 | |
---|
41 | public String toString() |
---|
42 | { |
---|
43 | KeyFactory kf = null; |
---|
44 | StringBuffer sb = new StringBuffer(); |
---|
45 | |
---|
46 | if(value instanceof DSAPublicKey) |
---|
47 | { |
---|
48 | try |
---|
49 | { |
---|
50 | kf = KeyFactory.getInstance("DSA"); |
---|
51 | DSAPublicKeySpec dsaPubKeySpec = |
---|
52 | (DSAPublicKeySpec)kf.getKeySpec(value, DSAPublicKeySpec.class); |
---|
53 | sb.append("DSA Public Key: \n") |
---|
54 | .append("\nG = ").append(dsaPubKeySpec.getG()) |
---|
55 | .append("\nP = ").append(dsaPubKeySpec.getP()) |
---|
56 | .append("\nQ = ").append(dsaPubKeySpec.getQ()) |
---|
57 | .append("\nY = ").append(dsaPubKeySpec.getY()); |
---|
58 | } |
---|
59 | catch(NoSuchAlgorithmException e1) |
---|
60 | { |
---|
61 | e1.printStackTrace(); |
---|
62 | } |
---|
63 | catch(InvalidKeySpecException e2) |
---|
64 | { |
---|
65 | e2.printStackTrace(); |
---|
66 | } |
---|
67 | } |
---|
68 | else if(value instanceof RSAPublicKey) |
---|
69 | { |
---|
70 | try |
---|
71 | { |
---|
72 | kf = KeyFactory.getInstance("RSA"); |
---|
73 | RSAPublicKeySpec rsaPubKeySpec = |
---|
74 | (RSAPublicKeySpec)kf.getKeySpec(value, RSAPublicKeySpec.class); |
---|
75 | sb.append("DSA Public Key: \n") |
---|
76 | .append("\nModulus = ").append(rsaPubKeySpec.getModulus()) |
---|
77 | .append("\nExponent = ").append(rsaPubKeySpec.getPublicExponent()); |
---|
78 | } |
---|
79 | catch(NoSuchAlgorithmException e1) |
---|
80 | { |
---|
81 | e1.printStackTrace(); |
---|
82 | } |
---|
83 | catch(InvalidKeySpecException e2) |
---|
84 | { |
---|
85 | e2.printStackTrace(); |
---|
86 | } |
---|
87 | } |
---|
88 | else |
---|
89 | { |
---|
90 | // TODO: any |
---|
91 | } |
---|
92 | |
---|
93 | return sb.toString(); |
---|
94 | } |
---|
95 | |
---|
96 | } |
---|