PageRenderTime 3743ms CodeModel.GetById 40ms RepoModel.GetById 7ms app.codeStats 0ms

/eclipse_projects/PubkeyGenerator/src/com/trilead/ssh2/crypto/cipher/BlockCipherFactory.java

https://gitlab.com/imxieke/remote-desktop-clients
Java | 115 lines | 92 code | 16 blank | 7 comment | 7 complexity | c52e884231da2909bc420566dc344c49 MD5 | raw file
  1. package com.trilead.ssh2.crypto.cipher;
  2. import java.util.Vector;
  3. /**
  4. * BlockCipherFactory.
  5. *
  6. * @author Christian Plattner, plattner@trilead.com
  7. * @version $Id: BlockCipherFactory.java,v 1.2 2008/04/01 12:38:09 cplattne Exp $
  8. */
  9. public class BlockCipherFactory
  10. {
  11. static class CipherEntry
  12. {
  13. String type;
  14. int blocksize;
  15. int keysize;
  16. String cipherClass;
  17. public CipherEntry(String type, int blockSize, int keySize, String cipherClass)
  18. {
  19. this.type = type;
  20. this.blocksize = blockSize;
  21. this.keysize = keySize;
  22. this.cipherClass = cipherClass;
  23. }
  24. }
  25. static Vector<CipherEntry> ciphers = new Vector<CipherEntry>();
  26. static
  27. {
  28. /* Higher Priority First */
  29. ciphers.addElement(new CipherEntry("aes256-ctr", 16, 32, "com.trilead.ssh2.crypto.cipher.AES"));
  30. ciphers.addElement(new CipherEntry("aes192-ctr", 16, 24, "com.trilead.ssh2.crypto.cipher.AES"));
  31. ciphers.addElement(new CipherEntry("aes128-ctr", 16, 16, "com.trilead.ssh2.crypto.cipher.AES"));
  32. ciphers.addElement(new CipherEntry("blowfish-ctr", 8, 16, "com.trilead.ssh2.crypto.cipher.BlowFish"));
  33. ciphers.addElement(new CipherEntry("aes256-cbc", 16, 32, "com.trilead.ssh2.crypto.cipher.AES"));
  34. ciphers.addElement(new CipherEntry("aes192-cbc", 16, 24, "com.trilead.ssh2.crypto.cipher.AES"));
  35. ciphers.addElement(new CipherEntry("aes128-cbc", 16, 16, "com.trilead.ssh2.crypto.cipher.AES"));
  36. ciphers.addElement(new CipherEntry("blowfish-cbc", 8, 16, "com.trilead.ssh2.crypto.cipher.BlowFish"));
  37. ciphers.addElement(new CipherEntry("3des-ctr", 8, 24, "com.trilead.ssh2.crypto.cipher.DESede"));
  38. ciphers.addElement(new CipherEntry("3des-cbc", 8, 24, "com.trilead.ssh2.crypto.cipher.DESede"));
  39. }
  40. public static String[] getDefaultCipherList()
  41. {
  42. String list[] = new String[ciphers.size()];
  43. for (int i = 0; i < ciphers.size(); i++)
  44. {
  45. CipherEntry ce = ciphers.elementAt(i);
  46. list[i] = new String(ce.type);
  47. }
  48. return list;
  49. }
  50. public static void checkCipherList(String[] cipherCandidates)
  51. {
  52. for (int i = 0; i < cipherCandidates.length; i++)
  53. getEntry(cipherCandidates[i]);
  54. }
  55. public static BlockCipher createCipher(String type, boolean encrypt, byte[] key, byte[] iv)
  56. {
  57. try
  58. {
  59. CipherEntry ce = getEntry(type);
  60. Class cc = Class.forName(ce.cipherClass);
  61. BlockCipher bc = (BlockCipher) cc.newInstance();
  62. if (type.endsWith("-cbc"))
  63. {
  64. bc.init(encrypt, key);
  65. return new CBCMode(bc, iv, encrypt);
  66. }
  67. else if (type.endsWith("-ctr"))
  68. {
  69. bc.init(true, key);
  70. return new CTRMode(bc, iv, encrypt);
  71. }
  72. throw new IllegalArgumentException("Cannot instantiate " + type);
  73. }
  74. catch (Exception e)
  75. {
  76. throw new IllegalArgumentException("Cannot instantiate " + type);
  77. }
  78. }
  79. private static CipherEntry getEntry(String type)
  80. {
  81. for (int i = 0; i < ciphers.size(); i++)
  82. {
  83. CipherEntry ce = ciphers.elementAt(i);
  84. if (ce.type.equals(type))
  85. return ce;
  86. }
  87. throw new IllegalArgumentException("Unkown algorithm " + type);
  88. }
  89. public static int getBlockSize(String type)
  90. {
  91. CipherEntry ce = getEntry(type);
  92. return ce.blocksize;
  93. }
  94. public static int getKeySize(String type)
  95. {
  96. CipherEntry ce = getEntry(type);
  97. return ce.keysize;
  98. }
  99. }