PageRenderTime 77ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/ch/ethz/ssh2/crypto/cipher/BlockCipherFactory.java

https://github.com/8nevil8/ganymed-ssh-2
Java | 130 lines | 104 code | 14 blank | 12 comment | 7 complexity | 5c893875661a4f18f1bf7687a4e62923 MD5 | raw file
  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. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Vector;
  9. /**
  10. * BlockCipherFactory.
  11. *
  12. * @author Christian Plattner
  13. * @version $Id$
  14. */
  15. public class BlockCipherFactory
  16. {
  17. private static final class CipherEntry
  18. {
  19. String type;
  20. int blocksize;
  21. int keysize;
  22. String cipherClass;
  23. public CipherEntry(String type, int blockSize, int keySize, String cipherClass)
  24. {
  25. this.type = type;
  26. this.blocksize = blockSize;
  27. this.keysize = keySize;
  28. this.cipherClass = cipherClass;
  29. }
  30. }
  31. private static final List<CipherEntry> ciphers = new Vector<CipherEntry>();
  32. static
  33. {
  34. /* Higher Priority First */
  35. ciphers.add(new CipherEntry("aes128-ctr", 16, 16, "ch.ethz.ssh2.crypto.cipher.AES"));
  36. ciphers.add(new CipherEntry("aes192-ctr", 16, 24, "ch.ethz.ssh2.crypto.cipher.AES"));
  37. ciphers.add(new CipherEntry("aes256-ctr", 16, 32, "ch.ethz.ssh2.crypto.cipher.AES"));
  38. ciphers.add(new CipherEntry("blowfish-ctr", 8, 16, "ch.ethz.ssh2.crypto.cipher.BlowFish"));
  39. ciphers.add(new CipherEntry("aes128-cbc", 16, 16, "ch.ethz.ssh2.crypto.cipher.AES"));
  40. ciphers.add(new CipherEntry("aes192-cbc", 16, 24, "ch.ethz.ssh2.crypto.cipher.AES"));
  41. ciphers.add(new CipherEntry("aes256-cbc", 16, 32, "ch.ethz.ssh2.crypto.cipher.AES"));
  42. ciphers.add(new CipherEntry("blowfish-cbc", 8, 16, "ch.ethz.ssh2.crypto.cipher.BlowFish"));
  43. ciphers.add(new CipherEntry("3des-ctr", 8, 24, "ch.ethz.ssh2.crypto.cipher.DESede"));
  44. ciphers.add(new CipherEntry("3des-cbc", 8, 24, "ch.ethz.ssh2.crypto.cipher.DESede"));
  45. }
  46. public static String[] getDefaultCipherList()
  47. {
  48. List<String> list = new ArrayList<String>(ciphers.size());
  49. for (CipherEntry ce : ciphers)
  50. {
  51. list.add(ce.type);
  52. }
  53. return list.toArray(new String[ciphers.size()]);
  54. }
  55. public static void checkCipherList(String[] cipherCandidates)
  56. {
  57. for (String cipherCandidate : cipherCandidates)
  58. {
  59. getEntry(cipherCandidate);
  60. }
  61. }
  62. // @SuppressWarnings("rawtypes")
  63. public static BlockCipher createCipher(String type, boolean encrypt, byte[] key, byte[] iv)
  64. {
  65. try
  66. {
  67. CipherEntry ce = getEntry(type);
  68. Class cc = Class.forName(ce.cipherClass);
  69. BlockCipher bc = (BlockCipher) cc.newInstance();
  70. if (type.endsWith("-cbc"))
  71. {
  72. bc.init(encrypt, key);
  73. return new CBCMode(bc, iv, encrypt);
  74. }
  75. else if (type.endsWith("-ctr"))
  76. {
  77. bc.init(true, key);
  78. return new CTRMode(bc, iv, encrypt);
  79. }
  80. throw new IllegalArgumentException("Cannot instantiate " + type);
  81. }
  82. catch (ClassNotFoundException e)
  83. {
  84. throw new IllegalArgumentException("Cannot instantiate " + type, e);
  85. }
  86. catch (InstantiationException e)
  87. {
  88. throw new IllegalArgumentException("Cannot instantiate " + type, e);
  89. }
  90. catch (IllegalAccessException e)
  91. {
  92. throw new IllegalArgumentException("Cannot instantiate " + type, e);
  93. }
  94. }
  95. private static CipherEntry getEntry(String type)
  96. {
  97. for (CipherEntry ce : ciphers)
  98. {
  99. if (ce.type.equals(type))
  100. {
  101. return ce;
  102. }
  103. }
  104. throw new IllegalArgumentException("Unkown algorithm " + type);
  105. }
  106. public static int getBlockSize(String type)
  107. {
  108. CipherEntry ce = getEntry(type);
  109. return ce.blocksize;
  110. }
  111. public static int getKeySize(String type)
  112. {
  113. CipherEntry ce = getEntry(type);
  114. return ce.keysize;
  115. }
  116. }