/core-library/src/main/java/org/silverpeas/core/security/encryption/cipher/BlowfishCipher.java

https://github.com/ebonnet/Silverpeas-Core · Java · 128 lines · 76 code · 14 blank · 38 comment · 6 complexity · e8ebce4fb157ccf3777162a46ae0bcb9 MD5 · raw file

  1. /*
  2. * Copyright (C) 2000 - 2016 Silverpeas
  3. *
  4. * This program is free software: you can redistribute it and/or modify it under the terms of the
  5. * GNU Affero General Public License as published by the Free Software Foundation, either version 3
  6. * of the License, or (at your option) any later version.
  7. *
  8. * As a special exception to the terms and conditions of version 3.0 of the GPL, you may
  9. * redistribute this Program in connection with Free/Libre Open Source Software ("FLOSS")
  10. * applications as described in Silverpeas's FLOSS exception. You should have received a copy of the
  11. * text describing the FLOSS exception, and it is also available here:
  12. * "http://www.silverpeas.org/docs/core/legal/floss_exception.html"
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  15. * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License along with this program.
  19. * If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package org.silverpeas.core.security.encryption.cipher;
  22. import org.silverpeas.core.silvertrace.SilverTrace;
  23. import org.silverpeas.core.util.Charsets;
  24. import javax.crypto.KeyGenerator;
  25. import javax.crypto.NoSuchPaddingException;
  26. import javax.crypto.SecretKey;
  27. import java.security.NoSuchAlgorithmException;
  28. /**
  29. * Blowfish is a keyed, symmetric block cipher, designed in 1993 by Bruce Schneier and included in a
  30. * large number of cipher suites and encryption products. Blowfish provides a good encryption rate
  31. * in software and no effective cryptanalysis of it has been found to date. However, the Advanced
  32. * Encryption Standard now receives more attention.
  33. *
  34. * Blowfish was one of the first secure block ciphers not subject to any patents and therefore
  35. * freely available for anyone to use. This benefit has contributed to its popularity in
  36. * cryptographic software.
  37. *
  38. * This implementation wraps the Blowfish cipher provided in the Java Cryptography API and it
  39. * performs the redundant operations in the encryption and in the decryption.
  40. */
  41. public class BlowfishCipher implements Cipher {
  42. private final javax.crypto.Cipher cipher;
  43. private BlowfishKey blowfishKey = null;
  44. protected BlowfishCipher() throws NoSuchPaddingException, NoSuchAlgorithmException {
  45. blowfishKey = new BlowfishKey();
  46. cipher = javax.crypto.Cipher.getInstance(CryptographicAlgorithmName.Blowfish.name());
  47. }
  48. /**
  49. * Gets the name of the algorithm of the cipher.
  50. *
  51. * @return the algorithm name.
  52. */
  53. @Override
  54. public CryptographicAlgorithmName getAlgorithmName() {
  55. return CryptographicAlgorithmName.Blowfish;
  56. }
  57. @Override
  58. public byte[] encrypt(String data, CipherKey keyCode) throws CryptoException {
  59. byte[] cipherText;
  60. try {
  61. byte[] cipherBytes = data.getBytes();
  62. BlowfishKey key;
  63. if (keyCode == null) {
  64. key = this.getSymmetricKey();
  65. } else {
  66. key = new BlowfishKey(keyCode.getRawKey());
  67. }
  68. synchronized (cipher) {
  69. cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, key);
  70. cipherText = cipher.doFinal(cipherBytes);
  71. }
  72. } catch (Exception e) {
  73. throw new CryptoException(CryptoException.ENCRYPTION_FAILURE, e);
  74. }
  75. return cipherText;
  76. }
  77. @Override
  78. public String decrypt(byte[] cipher, CipherKey keyCode) throws CryptoException {
  79. SilverTrace
  80. .info("util", "BlowfishCipher.decrypt", "root.MSG_GEN_ENTER_METHOD");
  81. String uncrypted;
  82. byte[] newPlainText;
  83. try {
  84. BlowfishKey key;
  85. if (keyCode == null) {
  86. key = this.getSymmetricKey();
  87. } else {
  88. key = new BlowfishKey(keyCode.getRawKey());
  89. }
  90. synchronized (cipher) {
  91. this.cipher.init(javax.crypto.Cipher.DECRYPT_MODE, key);
  92. newPlainText = this.cipher.doFinal(cipher);
  93. }
  94. uncrypted = new String(newPlainText, Charsets.UTF_8);
  95. } catch (Exception e) {
  96. throw new CryptoException(CryptoException.DECRYPTION_FAILURE, e);
  97. }
  98. return uncrypted;
  99. }
  100. private BlowfishKey getSymmetricKey() {
  101. return blowfishKey;
  102. }
  103. @Override
  104. public CipherKey generateCipherKey() throws CryptoException {
  105. try {
  106. KeyGenerator keyGenerator = KeyGenerator.getInstance(CryptographicAlgorithmName.Blowfish.
  107. name());
  108. SecretKey key = keyGenerator.generateKey();
  109. return CipherKey.aKeyFromBinary(key.getEncoded());
  110. } catch (NoSuchAlgorithmException ex) {
  111. throw new CryptoException(CryptoException.KEY_GENERATION_FAILURE, ex);
  112. }
  113. }
  114. }