/src/main/java/com/trilead/ssh2/crypto/cipher/BlockCipherFactory.java

https://github.com/connectbot/sshlib · Java · 103 lines · 81 code · 15 blank · 7 comment · 4 complexity · a91d1482e2e9f64565a8b176310a0dd2 MD5 · raw file

  1. package com.trilead.ssh2.crypto.cipher;
  2. import java.lang.reflect.Constructor;
  3. import java.util.ArrayList;
  4. /**
  5. * BlockCipherFactory.
  6. *
  7. * @author Christian Plattner, plattner@trilead.com
  8. * @version $Id: BlockCipherFactory.java,v 1.2 2008/04/01 12:38:09 cplattne Exp $
  9. */
  10. public class BlockCipherFactory
  11. {
  12. private static class CipherEntry
  13. {
  14. final String type;
  15. final int blocksize;
  16. final int keysize;
  17. final String cipherClass;
  18. CipherEntry(String type, int blockSize, int keySize, String cipherClass)
  19. {
  20. this.type = type;
  21. this.blocksize = blockSize;
  22. this.keysize = keySize;
  23. this.cipherClass = cipherClass;
  24. }
  25. }
  26. private static final ArrayList<CipherEntry> ciphers = new ArrayList<>();
  27. static
  28. {
  29. /* Higher Priority First */
  30. ciphers.add(new CipherEntry("aes256-ctr", 16, 32, "com.trilead.ssh2.crypto.cipher.AES$CTR"));
  31. ciphers.add(new CipherEntry("aes128-ctr", 16, 16, "com.trilead.ssh2.crypto.cipher.AES$CTR"));
  32. ciphers.add(new CipherEntry("blowfish-ctr", 8, 16, "com.trilead.ssh2.crypto.cipher.BlowFish$CTR"));
  33. ciphers.add(new CipherEntry("aes256-cbc", 16, 32, "com.trilead.ssh2.crypto.cipher.AES$CBC"));
  34. ciphers.add(new CipherEntry("aes128-cbc", 16, 16, "com.trilead.ssh2.crypto.cipher.AES$CBC"));
  35. ciphers.add(new CipherEntry("blowfish-cbc", 8, 16, "com.trilead.ssh2.crypto.cipher.BlowFish$CBC"));
  36. ciphers.add(new CipherEntry("3des-ctr", 8, 24, "com.trilead.ssh2.crypto.cipher.DESede$CTR"));
  37. ciphers.add(new CipherEntry("3des-cbc", 8, 24, "com.trilead.ssh2.crypto.cipher.DESede$CBC"));
  38. }
  39. public static String[] getDefaultCipherList()
  40. {
  41. String list[] = new String[ciphers.size()];
  42. for (int i = 0; i < ciphers.size(); i++)
  43. {
  44. CipherEntry ce = ciphers.get(i);
  45. list[i] = ce.type;
  46. }
  47. return list;
  48. }
  49. public static void checkCipherList(String[] cipherCandidates)
  50. {
  51. for (String cipherCandidate : cipherCandidates)
  52. getEntry(cipherCandidate);
  53. }
  54. public static BlockCipher createCipher(String type, boolean encrypt, byte[] key, byte[] iv)
  55. {
  56. try
  57. {
  58. CipherEntry ce = getEntry(type);
  59. Class cc = Class.forName(ce.cipherClass);
  60. Constructor<BlockCipher> constructor = cc.getConstructor();
  61. BlockCipher bc = constructor.newInstance();
  62. bc.init(encrypt, key, iv);
  63. return bc;
  64. }
  65. catch (Exception e)
  66. {
  67. throw new IllegalArgumentException("Cannot instantiate " + type, e);
  68. }
  69. }
  70. private static CipherEntry getEntry(String type)
  71. {
  72. for (CipherEntry ce : ciphers) {
  73. if (ce.type.equals(type))
  74. return ce;
  75. }
  76. throw new IllegalArgumentException("Unknown algorithm " + type);
  77. }
  78. public static int getBlockSize(String type)
  79. {
  80. CipherEntry ce = getEntry(type);
  81. return ce.blocksize;
  82. }
  83. public static int getKeySize(String type)
  84. {
  85. CipherEntry ce = getEntry(type);
  86. return ce.keysize;
  87. }
  88. }