PageRenderTime 97ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/lib-core/src/main/java/com/silverpeas/util/cryptage/SilverCryptFactorySymetric.java

https://github.com/NicolasEYSSERIC/Silverpeas-Core
Java | 138 lines | 93 code | 14 blank | 31 comment | 8 complexity | 6db40cf7aebbb84ee9b72fd9c8bd94f4 MD5 | raw file
Possible License(s): AGPL-3.0
  1. /**
  2. * Copyright (C) 2000 - 2012 Silverpeas
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License as
  6. * published by the Free Software Foundation, either version 3 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * As a special exception to the terms and conditions of version 3.0 of
  10. * the GPL, you may redistribute this Program in connection with Free/Libre
  11. * Open Source Software ("FLOSS") applications as described in Silverpeas's
  12. * FLOSS exception. You should have received a copy of the text describing
  13. * the FLOSS exception, and it is also available here:
  14. * "http://www.silverpeas.org/docs/core/legal/floss_exception.html"
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. package com.silverpeas.util.cryptage;
  25. import com.stratelia.silverpeas.silvertrace.SilverTrace;
  26. import com.stratelia.webactiv.util.exception.SilverpeasException;
  27. import java.util.Arrays;
  28. import java.security.NoSuchAlgorithmException;
  29. import javax.crypto.Cipher;
  30. import javax.crypto.NoSuchPaddingException;
  31. public class SilverCryptFactorySymetric {
  32. public static final String ALGORITHM = "Blowfish";
  33. /**
  34. * Singleton managing the keyring.
  35. **/
  36. private static SilverCryptFactorySymetric factory = null;
  37. private static Cipher cipher = null;
  38. private SilverCryptKeysSymetric silverCryptKeysSymetric = null;
  39. private SilverCryptFactorySymetric() {
  40. }
  41. static {
  42. try {
  43. cipher = Cipher.getInstance(ALGORITHM);
  44. } catch (NoSuchAlgorithmException e) {
  45. SilverTrace.error("util", "SilverCryptFactorySymetric.NoSuchAlgorithm",
  46. "root.MSG_GEN_PARAM_VALUE", "In init", e);
  47. } catch (NoSuchPaddingException e) {
  48. SilverTrace.error("util", "SilverCryptFactorySymetric.NoSuchPadding",
  49. "root.MSG_GEN_PARAM_VALUE", "In init", e);
  50. }
  51. }
  52. public static SilverCryptFactorySymetric getInstance() {
  53. synchronized (SilverCryptFactorySymetric.class) {
  54. if (factory == null) {
  55. factory = new SilverCryptFactorySymetric();
  56. }
  57. }
  58. return factory;
  59. }
  60. public synchronized byte[] goCrypting(String stringUnCrypted) throws CryptageException {
  61. return goCrypting(stringUnCrypted, null);
  62. }
  63. public synchronized byte[] goCrypting(String stringUnCrypted, SilverCryptKeysSymetric symetricKeys)
  64. throws CryptageException {
  65. SilverTrace.info("util", "SilverCryptFactorySymetric.goCrypting", "root.MSG_GEN_ENTER_METHOD",
  66. "stringUnCrypted = " + stringUnCrypted);
  67. byte[] cipherText = null;
  68. try {
  69. byte[] cipherBytes = stringUnCrypted.getBytes();
  70. SilverCryptKeysSymetric keys = symetricKeys;
  71. if (symetricKeys == null) {
  72. keys = this.getSymetricKeys();
  73. }
  74. cipher.init(Cipher.ENCRYPT_MODE, keys.getKey());
  75. SilverTrace.debug("util", "SilverCryptFactorySymetric.goCrypting",
  76. "root.MSG_GEN_PARAM_VALUE",
  77. "After init");
  78. cipherText = cipher.doFinal(cipherBytes);
  79. } catch (Exception e) {
  80. throw new CryptageException("SilverCryptFactory.goCrypting", SilverpeasException.ERROR,
  81. "util.CRYPT_FAILED", e);
  82. }
  83. SilverTrace.info("util", "SilverCryptFactorySymetric.goCrypting", "root.MSG_GEN_EXIT_METHOD",
  84. "cipherText = " + Arrays.toString(cipherText));
  85. return cipherText;
  86. }
  87. public synchronized String goUnCrypting(byte[] cipherBytes) throws CryptageException {
  88. return goUnCrypting(cipherBytes, null);
  89. }
  90. public synchronized String goUnCrypting(byte[] cipherBytes, SilverCryptKeysSymetric symetricKeys)
  91. throws CryptageException {
  92. SilverTrace
  93. .info("util", "SilverCryptFactorySymetric.goUnCrypting", "root.MSG_GEN_ENTER_METHOD");
  94. String uncrypted = "";
  95. try {
  96. SilverCryptKeysSymetric keys = symetricKeys;
  97. if (symetricKeys == null) {
  98. keys = this.getSymetricKeys();
  99. }
  100. cipher.init(Cipher.DECRYPT_MODE, keys.getKey());
  101. SilverTrace.debug("util", "SilverCryptFactorySymetric.goUnCrypting",
  102. "root.MSG_GEN_PARAM_VALUE", "After init");
  103. byte[] newPlainText = cipher.doFinal(cipherBytes);
  104. uncrypted = new String(newPlainText, "UTF8");
  105. } catch (Exception e) {
  106. throw new CryptageException("SilverCryptFactory.goUnCrypting", SilverpeasException.ERROR,
  107. "util.UNCRYPT_FAILED", e);
  108. }
  109. SilverTrace.info("util", "SilverCryptFactorySymetric.goUnCrypting",
  110. "root.MSG_GEN_EXIT_METHOD", "uncrypted = " + uncrypted);
  111. return uncrypted;
  112. }
  113. /**
  114. * Returns the keyring.
  115. * @return the keyring.
  116. * @throws CryptageException
  117. */
  118. public SilverCryptKeysSymetric getSymetricKeys() throws CryptageException {
  119. if (silverCryptKeysSymetric == null) {
  120. silverCryptKeysSymetric = new SilverCryptKeysSymetric();
  121. }
  122. return silverCryptKeysSymetric;
  123. }
  124. }