KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > crypto > SecretKeyManager


1 package SnowMailClient.crypto;
2
3 import snow.utils.storage.*;
4 import snow.crypto.*;
5
6 import java.security.*;
7 import java.security.spec.*;
8 import javax.crypto.spec.*;
9 import javax.crypto.*;
10 import java.io.*;
11 import java.util.zip.*;
12 import java.util.*;
13
14 /** contain all the keys entered during this session and the key to be used for
15   all cipherings
16   must be stored only if the user has set a secret passphrase.
17   Otherwise, it would be unsecure to save it with the default pass.
18
19 */

20 public final class SecretKeyManager
21 {
22   
23   private SecretKey defaultStorageKey = null;
24
25   // key = id, value = key
26
private Hashtable<String JavaDoc, SecretKey> keys = new Hashtable<String JavaDoc, SecretKey>();
27   private SecretKey defaultUserKeyForEncryption = null;
28
29
30   // singleton class
31
private static SecretKeyManager instance = null;
32   private SecretKeyManager()
33   {
34     try
35     {
36       // absolutely no serious security
37
defaultStorageKey = SecretKeyUtilities.generateSecretKeyFromPassphrase("rz9r(i#*-.3)tyu89i".getBytes(), 8);
38       this.addKey(defaultStorageKey);
39     }
40     catch(Exception JavaDoc e)
41     {
42     }
43   } // Constructor
44

45   public static SecretKeyManager getInstance()
46   {
47      if(instance==null)
48      {
49        instance = new SecretKeyManager();
50      }
51      return instance;
52   }
53
54   public void addKey(SecretKey key)
55   {
56      SecretKeyID kid = SecretKeyUtilities.computeSignature( key );
57
58      keys.put(
59         new String JavaDoc(kid.signature),
60         key);
61
62      Object JavaDoc o = keys.get(new String JavaDoc(kid.signature));
63   }
64
65
66   /** is used to make files not readable from system automatic tests...
67       (antivirus, search engines, ...)
68       give a good obfuscation security (as long as nobody use this sourcecode)
69   */

70   public SecretKey getDefaultKeyVersion1_()
71   {
72      return defaultStorageKey;
73   }
74
75   public SecretKey getUserKeyForEncryption()
76   {
77     return defaultUserKeyForEncryption;
78   }
79
80
81   public boolean isUserKeySet() { return this.defaultUserKeyForEncryption!=null; }
82
83   public void setUserKeyForEncryption(SecretKey sk)
84   {
85     this.defaultUserKeyForEncryption = sk;
86     if(sk!=null)
87     {
88       this.addKey(sk);
89     }
90   }
91
92   public SecretKey getActualKey()
93   {
94     if(defaultUserKeyForEncryption!=null) return defaultUserKeyForEncryption;
95     return this.defaultStorageKey;
96   }
97
98
99   /** look in the stored keys
100   */

101   public SecretKey getKey(SecretKeyID id)
102   {
103      byte[] sign = id.getKeySignature();
104      Object JavaDoc key = keys.get(new String JavaDoc(id.signature));
105      if(key==null)
106      {
107        return null;
108      }
109      return (SecretKey) key;
110   }
111
112
113
114   // Storage
115
//
116

117   public Vector<Object JavaDoc> getVectorRepresentation()
118   {
119      Vector<Object JavaDoc> rep = new Vector<Object JavaDoc>();
120      rep.addElement(1); //0
121

122      if(defaultUserKeyForEncryption!=null)
123      {
124        rep.addElement(defaultUserKeyForEncryption.getEncoded()); // 1
125
}
126      else
127      {
128        rep.addElement(new byte[0]);
129      }
130
131      Enumeration keyEnum = keys.elements();
132      Vector<byte[]> keyIDs = new Vector<byte[]>();
133      rep.addElement(keyIDs); // 2
134
while(keyEnum.hasMoreElements())
135      {
136        SecretKey key = (SecretKey) keyEnum.nextElement();
137        keyIDs.addElement(key.getEncoded());
138      }
139      return rep;
140   }
141
142
143   public void createFromVectorRepresentation(Vector<Object JavaDoc> v)
144   {
145      int version = (Integer JavaDoc) v.elementAt(0);
146      if(version==1)
147      {
148
149        byte[] nk2 = (byte[]) v.elementAt(1); // 1
150
if(nk2.length>0)
151        {
152           this.defaultUserKeyForEncryption = new SecretKeySpec(nk2, "Blowfish");
153           this.addKey(this.defaultUserKeyForEncryption);
154        }
155        else
156        {
157           this.defaultUserKeyForEncryption = null;
158        }
159
160        @SuppressWarnings JavaDoc("unchecked")
161        Vector<byte[]> kv = (Vector<byte[]>) v.elementAt(2); // 2
162
for(int i=0; i<kv.size(); i++)
163        {
164          byte[] key = kv.elementAt(i);
165          SecretKeySpec sks = new SecretKeySpec(key, "Blowfish");
166          this.addKey(sks);
167        }
168      }
169      else
170      {
171         throw new RuntimeException JavaDoc("Bad version "+version);
172      }
173   }
174
175 } // KeyManager
Popular Tags