PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/aqmon/lib/j2ssh/src/com/sshtools/j2ssh/transport/cipher/BlowfishCbc.java

http://ece01sd.googlecode.com/
Java | 128 lines | 49 code | 15 blank | 64 comment | 1 complexity | 5465fb993006e30eff6217b098965b59 MD5 | raw file
Possible License(s): IPL-1.0, GPL-3.0, GPL-2.0, JSON
  1. /*
  2. * SSHTools - Java SSH2 API
  3. *
  4. * Copyright (C) 2002-2003 Lee David Painter and Contributors.
  5. *
  6. * Contributions made by:
  7. *
  8. * Brett Smith
  9. * Richard Pernavas
  10. * Erwin Bolwidt
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version 2
  15. * of the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  25. */
  26. package com.sshtools.j2ssh.transport.cipher;
  27. import com.sshtools.j2ssh.transport.AlgorithmOperationException;
  28. import org.apache.commons.logging.Log;
  29. import org.apache.commons.logging.LogFactory;
  30. import java.security.InvalidAlgorithmParameterException;
  31. import java.security.InvalidKeyException;
  32. import java.security.NoSuchAlgorithmException;
  33. import javax.crypto.Cipher;
  34. import javax.crypto.NoSuchPaddingException;
  35. import javax.crypto.spec.IvParameterSpec;
  36. import javax.crypto.spec.SecretKeySpec;
  37. /**
  38. *
  39. *
  40. * @author $author$
  41. * @version $Revision: 1.21 $
  42. */
  43. public class BlowfishCbc extends SshCipher {
  44. private static Log log = LogFactory.getLog(BlowfishCbc.class);
  45. /** */
  46. protected static String algorithmName = "blowfish-cbc";
  47. Cipher cipher;
  48. /**
  49. * Creates a new BlowfishCbc object.
  50. */
  51. public BlowfishCbc() {
  52. }
  53. /**
  54. *
  55. *
  56. * @return
  57. */
  58. public int getBlockSize() {
  59. return cipher.getBlockSize();
  60. }
  61. /**
  62. *
  63. *
  64. * @param mode
  65. * @param iv
  66. * @param keydata
  67. *
  68. * @throws AlgorithmOperationException
  69. */
  70. public void init(int mode, byte[] iv, byte[] keydata)
  71. throws AlgorithmOperationException {
  72. try {
  73. cipher = Cipher.getInstance("Blowfish/CBC/NoPadding");
  74. // Create a 16 byte key
  75. byte[] actualKey = new byte[16];
  76. System.arraycopy(keydata, 0, actualKey, 0, actualKey.length);
  77. SecretKeySpec keyspec = new SecretKeySpec(actualKey, "Blowfish");
  78. // Create the cipher according to its algorithm
  79. cipher.init(((mode == ENCRYPT_MODE) ? Cipher.ENCRYPT_MODE
  80. : Cipher.DECRYPT_MODE),
  81. keyspec, new IvParameterSpec(iv, 0, cipher.getBlockSize()));
  82. } catch (NoSuchPaddingException nspe) {
  83. log.error("Blowfish initialization failed", nspe);
  84. throw new AlgorithmOperationException("No Padding not supported");
  85. } catch (NoSuchAlgorithmException nsae) {
  86. log.error("Blowfish initialization failed", nsae);
  87. throw new AlgorithmOperationException("Algorithm not supported");
  88. } catch (InvalidKeyException ike) {
  89. log.error("Blowfish initialization failed", ike);
  90. throw new AlgorithmOperationException("Invalid encryption key");
  91. /*} catch (InvalidKeySpecException ispe) {
  92. throw new AlgorithmOperationException("Invalid encryption key specification");*/
  93. } catch (InvalidAlgorithmParameterException ape) {
  94. log.error("Blowfish initialization failed", ape);
  95. throw new AlgorithmOperationException("Invalid algorithm parameter");
  96. }
  97. }
  98. /**
  99. *
  100. *
  101. * @param data
  102. * @param offset
  103. * @param len
  104. *
  105. * @return
  106. *
  107. * @throws AlgorithmOperationException
  108. */
  109. public byte[] transform(byte[] data, int offset, int len)
  110. throws AlgorithmOperationException {
  111. return cipher.update(data, offset, len);
  112. }
  113. }