comparison app/src/main/java/ch/ethz/ssh2/crypto/cipher/BlockCipherFactory.java @ 438:d29cce60f393

migrate from Eclipse to Android Studio
author Carl Byington <carl@five-ten-sg.com>
date Thu, 03 Dec 2015 11:23:55 -0800
parents src/ch/ethz/ssh2/crypto/cipher/BlockCipherFactory.java@8c1451f51a5e
children
comparison
equal deleted inserted replaced
437:208b31032318 438:d29cce60f393
1 /*
2 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
3 * Please refer to the LICENSE.txt for licensing details.
4 */
5 package ch.ethz.ssh2.crypto.cipher;
6
7 import java.util.ArrayList;
8 import java.util.List;
9 import java.util.Vector;
10
11 /**
12 * BlockCipherFactory.
13 *
14 * @author Christian Plattner
15 * @version $Id: BlockCipherFactory.java 86 2014-04-07 14:15:18Z dkocher@sudo.ch $
16 */
17 public class BlockCipherFactory {
18 private static final class CipherEntry {
19 String type;
20 int blocksize;
21 int keysize;
22 String cipherClass;
23
24 public CipherEntry(String type, int blockSize, int keySize, String cipherClass) {
25 this.type = type;
26 this.blocksize = blockSize;
27 this.keysize = keySize;
28 this.cipherClass = cipherClass;
29 }
30 }
31
32 private static final List<CipherEntry> ciphers = new ArrayList<CipherEntry>();
33
34 static {
35 // Higher priority (stronger) first
36 ciphers.add(new CipherEntry("aes256-ctr", 16, 32, "ch.ethz.ssh2.crypto.cipher.AES"));
37 ciphers.add(new CipherEntry("aes192-ctr", 16, 24, "ch.ethz.ssh2.crypto.cipher.AES"));
38 ciphers.add(new CipherEntry("aes128-ctr", 16, 16, "ch.ethz.ssh2.crypto.cipher.AES"));
39 ciphers.add(new CipherEntry("blowfish-ctr", 8, 16, "ch.ethz.ssh2.crypto.cipher.BlowFish"));
40 ciphers.add(new CipherEntry("aes256-cbc", 16, 32, "ch.ethz.ssh2.crypto.cipher.AES"));
41 ciphers.add(new CipherEntry("aes192-cbc", 16, 24, "ch.ethz.ssh2.crypto.cipher.AES"));
42 ciphers.add(new CipherEntry("aes128-cbc", 16, 16, "ch.ethz.ssh2.crypto.cipher.AES"));
43 ciphers.add(new CipherEntry("blowfish-cbc", 8, 16, "ch.ethz.ssh2.crypto.cipher.BlowFish"));
44 ciphers.add(new CipherEntry("3des-ctr", 8, 24, "ch.ethz.ssh2.crypto.cipher.DESede"));
45 ciphers.add(new CipherEntry("3des-cbc", 8, 24, "ch.ethz.ssh2.crypto.cipher.DESede"));
46 }
47
48 public static String[] getDefaultCipherList() {
49 List<String> list = new ArrayList<String>(ciphers.size());
50
51 for (CipherEntry ce : ciphers) {
52 list.add(ce.type);
53 }
54
55 return list.toArray(new String[ciphers.size()]);
56 }
57
58 public static void checkCipherList(String[] cipherCandidates) {
59 for (String cipherCandidate : cipherCandidates) {
60 getEntry(cipherCandidate);
61 }
62 }
63
64 // @SuppressWarnings("rawtypes")
65 public static BlockCipher createCipher(String type, boolean encrypt, byte[] key, byte[] iv) {
66 try {
67 CipherEntry ce = getEntry(type);
68 Class<?> cc = Class.forName(ce.cipherClass);
69 BlockCipher bc = (BlockCipher) cc.newInstance();
70
71 if (type.endsWith("-cbc")) {
72 bc.init(encrypt, key);
73 return new CBCMode(bc, iv, encrypt);
74 }
75 else if (type.endsWith("-ctr")) {
76 bc.init(true, key);
77 return new CTRMode(bc, iv, encrypt);
78 }
79
80 throw new IllegalArgumentException("Cannot instantiate " + type);
81 }
82 catch (ClassNotFoundException e) {
83 throw new IllegalArgumentException("Cannot instantiate " + type, e);
84 }
85 catch (InstantiationException e) {
86 throw new IllegalArgumentException("Cannot instantiate " + type, e);
87 }
88 catch (IllegalAccessException e) {
89 throw new IllegalArgumentException("Cannot instantiate " + type, e);
90 }
91 }
92
93 private static CipherEntry getEntry(String type) {
94 for (CipherEntry ce : ciphers) {
95 if (ce.type.equals(type)) {
96 return ce;
97 }
98 }
99
100 throw new IllegalArgumentException("Unkown algorithm " + type);
101 }
102
103 public static int getBlockSize(String type) {
104 CipherEntry ce = getEntry(type);
105 return ce.blocksize;
106 }
107
108 public static int getKeySize(String type) {
109 CipherEntry ce = getEntry(type);
110 return ce.keysize;
111 }
112 }