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 20 public final class SecretKeyManager 21 { 22 23 private SecretKey defaultStorageKey = null; 24 25 private Hashtable<String , SecretKey> keys = new Hashtable<String , SecretKey>(); 27 private SecretKey defaultUserKeyForEncryption = null; 28 29 30 private static SecretKeyManager instance = null; 32 private SecretKeyManager() 33 { 34 try 35 { 36 defaultStorageKey = SecretKeyUtilities.generateSecretKeyFromPassphrase("rz9r(i#*-.3)tyu89i".getBytes(), 8); 38 this.addKey(defaultStorageKey); 39 } 40 catch(Exception e) 41 { 42 } 43 } 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 (kid.signature), 60 key); 61 62 Object o = keys.get(new String (kid.signature)); 63 } 64 65 66 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 101 public SecretKey getKey(SecretKeyID id) 102 { 103 byte[] sign = id.getKeySignature(); 104 Object key = keys.get(new String (id.signature)); 105 if(key==null) 106 { 107 return null; 108 } 109 return (SecretKey) key; 110 } 111 112 113 114 117 public Vector<Object > getVectorRepresentation() 118 { 119 Vector<Object > rep = new Vector<Object >(); 120 rep.addElement(1); 122 if(defaultUserKeyForEncryption!=null) 123 { 124 rep.addElement(defaultUserKeyForEncryption.getEncoded()); } 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); 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 > v) 144 { 145 int version = (Integer ) v.elementAt(0); 146 if(version==1) 147 { 148 149 byte[] nk2 = (byte[]) v.elementAt(1); 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 ("unchecked") 161 Vector<byte[]> kv = (Vector<byte[]>) v.elementAt(2); 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 ("Bad version "+version); 172 } 173 } 174 175 } | Popular Tags |