KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > ssh2 > crypto > cipher > BlockCipherFactory


1
2 package ch.ethz.ssh2.crypto.cipher;
3
4 import java.util.Vector JavaDoc;
5
6 /**
7  * BlockCipherFactory.
8  *
9  * @author Christian Plattner, plattner@inf.ethz.ch
10  * @version $Id: BlockCipherFactory.java,v 1.4 2005/12/05 17:13:27 cplattne Exp $
11  */

12 public class BlockCipherFactory
13 {
14     static class CipherEntry
15     {
16         String JavaDoc type;
17         int blocksize;
18         int keysize;
19         String JavaDoc cipherClass;
20
21         public CipherEntry(String JavaDoc type, int blockSize, int keySize, String JavaDoc cipherClass)
22         {
23             this.type = type;
24             this.blocksize = blockSize;
25             this.keysize = keySize;
26             this.cipherClass = cipherClass;
27         }
28     }
29
30     static Vector JavaDoc ciphers = new Vector JavaDoc();
31
32     static
33     {
34         /* Higher Priority First */
35
36         ciphers.addElement(new CipherEntry("aes256-ctr", 16, 32, "ch.ethz.ssh2.crypto.cipher.AES"));
37         ciphers.addElement(new CipherEntry("aes192-ctr", 16, 24, "ch.ethz.ssh2.crypto.cipher.AES"));
38         ciphers.addElement(new CipherEntry("aes128-ctr", 16, 16, "ch.ethz.ssh2.crypto.cipher.AES"));
39         ciphers.addElement(new CipherEntry("blowfish-ctr", 8, 16, "ch.ethz.ssh2.crypto.cipher.BlowFish"));
40
41         ciphers.addElement(new CipherEntry("aes256-cbc", 16, 32, "ch.ethz.ssh2.crypto.cipher.AES"));
42         ciphers.addElement(new CipherEntry("aes192-cbc", 16, 24, "ch.ethz.ssh2.crypto.cipher.AES"));
43         ciphers.addElement(new CipherEntry("aes128-cbc", 16, 16, "ch.ethz.ssh2.crypto.cipher.AES"));
44         ciphers.addElement(new CipherEntry("blowfish-cbc", 8, 16, "ch.ethz.ssh2.crypto.cipher.BlowFish"));
45         
46         ciphers.addElement(new CipherEntry("3des-ctr", 8, 24, "ch.ethz.ssh2.crypto.cipher.DESede"));
47         ciphers.addElement(new CipherEntry("3des-cbc", 8, 24, "ch.ethz.ssh2.crypto.cipher.DESede"));
48     }
49
50     public static String JavaDoc[] getDefaultCipherList()
51     {
52         String JavaDoc list[] = new String JavaDoc[ciphers.size()];
53         for (int i = 0; i < ciphers.size(); i++)
54         {
55             CipherEntry ce = (CipherEntry) ciphers.elementAt(i);
56             list[i] = new String JavaDoc(ce.type);
57         }
58         return list;
59     }
60
61     public static void checkCipherList(String JavaDoc[] cipherCandidates)
62     {
63         for (int i = 0; i < cipherCandidates.length; i++)
64             getEntry(cipherCandidates[i]);
65     }
66
67     public static BlockCipher createCipher(String JavaDoc type, boolean encrypt, byte[] key, byte[] iv)
68     {
69         try
70         {
71             CipherEntry ce = getEntry(type);
72             Class JavaDoc cc = Class.forName(ce.cipherClass);
73             BlockCipher bc = (BlockCipher) cc.newInstance();
74
75             if (type.endsWith("-cbc"))
76             {
77                 bc.init(encrypt, key);
78                 return new CBCMode(bc, iv, encrypt);
79             }
80             else if (type.endsWith("-ctr"))
81             {
82                 bc.init(true, key);
83                 return new CTRMode(bc, iv, encrypt);
84             }
85             throw new IllegalArgumentException JavaDoc("Cannot instantiate " + type);
86         }
87         catch (Exception JavaDoc e)
88         {
89             throw new IllegalArgumentException JavaDoc("Cannot instantiate " + type);
90         }
91     }
92
93     private static CipherEntry getEntry(String JavaDoc type)
94     {
95         for (int i = 0; i < ciphers.size(); i++)
96         {
97             CipherEntry ce = (CipherEntry) ciphers.elementAt(i);
98             if (ce.type.equals(type))
99                 return ce;
100         }
101         throw new IllegalArgumentException JavaDoc("Unkown algorithm " + type);
102     }
103
104     public static int getBlockSize(String JavaDoc type)
105     {
106         CipherEntry ce = getEntry(type);
107         return ce.blocksize;
108     }
109
110     public static int getKeySize(String JavaDoc type)
111     {
112         CipherEntry ce = getEntry(type);
113         return ce.keysize;
114     }
115 }
116
Popular Tags