1 2 package ch.ethz.ssh2.crypto.cipher; 3 4 import java.util.Vector ; 5 6 12 public class BlockCipherFactory 13 { 14 static class CipherEntry 15 { 16 String type; 17 int blocksize; 18 int keysize; 19 String cipherClass; 20 21 public CipherEntry(String type, int blockSize, int keySize, String cipherClass) 22 { 23 this.type = type; 24 this.blocksize = blockSize; 25 this.keysize = keySize; 26 this.cipherClass = cipherClass; 27 } 28 } 29 30 static Vector ciphers = new Vector (); 31 32 static 33 { 34 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 [] getDefaultCipherList() 51 { 52 String list[] = new String [ciphers.size()]; 53 for (int i = 0; i < ciphers.size(); i++) 54 { 55 CipherEntry ce = (CipherEntry) ciphers.elementAt(i); 56 list[i] = new String (ce.type); 57 } 58 return list; 59 } 60 61 public static void checkCipherList(String [] cipherCandidates) 62 { 63 for (int i = 0; i < cipherCandidates.length; i++) 64 getEntry(cipherCandidates[i]); 65 } 66 67 public static BlockCipher createCipher(String type, boolean encrypt, byte[] key, byte[] iv) 68 { 69 try 70 { 71 CipherEntry ce = getEntry(type); 72 Class 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 ("Cannot instantiate " + type); 86 } 87 catch (Exception e) 88 { 89 throw new IllegalArgumentException ("Cannot instantiate " + type); 90 } 91 } 92 93 private static CipherEntry getEntry(String 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 ("Unkown algorithm " + type); 102 } 103 104 public static int getBlockSize(String type) 105 { 106 CipherEntry ce = getEntry(type); 107 return ce.blocksize; 108 } 109 110 public static int getKeySize(String type) 111 { 112 CipherEntry ce = getEntry(type); 113 return ce.keysize; 114 } 115 } 116 | Popular Tags |