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

/projects/columba-1.0/src/columba/core/org/columba/core/base/Blowfish.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 107 lines | 60 code | 20 blank | 27 comment | 0 complexity | fcf8ba0a752820791f486d8a464f3f62 MD5 | raw file
  1. // The contents of this file are subject to the Mozilla Public License Version
  2. // 1.1
  3. //(the "License"); you may not use this file except in compliance with the
  4. //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
  5. //
  6. //Software distributed under the License is distributed on an "AS IS" basis,
  7. //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  8. //for the specific language governing rights and
  9. //limitations under the License.
  10. //
  11. //The Original Code is "The Columba Project"
  12. //
  13. //The Initial Developers of the Original Code are Frederik Dietz and Timo
  14. // Stich.
  15. //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
  16. //
  17. //All Rights Reserved.
  18. package org.columba.core.base;
  19. import java.io.UnsupportedEncodingException;
  20. import java.nio.ByteBuffer;
  21. import java.security.InvalidKeyException;
  22. import java.security.Key;
  23. import java.security.NoSuchAlgorithmException;
  24. import java.util.logging.Logger;
  25. import javax.crypto.BadPaddingException;
  26. import javax.crypto.Cipher;
  27. import javax.crypto.IllegalBlockSizeException;
  28. import javax.crypto.NoSuchPaddingException;
  29. import javax.crypto.spec.SecretKeySpec;
  30. import org.columba.ristretto.coder.Base64;
  31. public class Blowfish {
  32. private static final Logger LOG = Logger
  33. .getLogger("org.columba.util.blowfish");
  34. private static final byte[] BYTES = { -127, 88, 27, -88, -13, -56, 19, -4,
  35. 45, 25, 38, 70, -17, 40, 36, -23 };
  36. private static final Key KEY = new SecretKeySpec(BYTES, "Blowfish");
  37. public static String encrypt(char[] source) {
  38. try {
  39. // Create the cipher
  40. Cipher blowCipher = Cipher.getInstance("Blowfish");
  41. // Initialize the cipher for encryption
  42. blowCipher.init(Cipher.ENCRYPT_MODE, KEY);
  43. // Our cleartext as bytes
  44. byte[] cleartext = new String(source).getBytes("UTF-8");
  45. // Encrypt the cleartext
  46. byte[] ciphertext = blowCipher.doFinal(cleartext);
  47. // Return a String representation of the cipher text
  48. return Base64.encode(ByteBuffer.wrap(ciphertext)).toString();
  49. } catch (InvalidKeyException e) {
  50. } catch (NoSuchAlgorithmException e) {
  51. LOG.severe(e.toString());
  52. } catch (NoSuchPaddingException e) {
  53. } catch (UnsupportedEncodingException e) {
  54. LOG.severe(e.toString());
  55. } catch (IllegalStateException e) {
  56. } catch (IllegalBlockSizeException e) {
  57. } catch (BadPaddingException e) {
  58. }
  59. return "";
  60. }
  61. public static char[] decrypt(String source) {
  62. try {
  63. // Create the cipher
  64. Cipher blowCipher = Cipher.getInstance("Blowfish");
  65. // Initialize the cipher for encryption
  66. blowCipher.init(Cipher.DECRYPT_MODE, KEY);
  67. // Encrypt the cleartext
  68. ByteBuffer ciphertext = Base64.decode(source);
  69. byte[] cipherArray = new byte[ciphertext.limit()];
  70. ciphertext.get(cipherArray);
  71. // Our cleartext as bytes
  72. byte[] cleartext = blowCipher.doFinal(cipherArray);
  73. // Return a String representation of the cipher text
  74. return new String(cleartext, "UTF-8").toCharArray();
  75. } catch (InvalidKeyException e) {
  76. } catch (NoSuchAlgorithmException e) {
  77. LOG.severe(e.toString());
  78. } catch (NoSuchPaddingException e) {
  79. } catch (UnsupportedEncodingException e) {
  80. LOG.severe(e.toString());
  81. } catch (IllegalStateException e) {
  82. } catch (IllegalBlockSizeException e) {
  83. } catch (BadPaddingException e) {
  84. }
  85. return new char[0];
  86. }
  87. }