PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/cdemagneval/Silverpeas-Core
Java | 135 lines | 91 code | 13 blank | 31 comment | 8 complexity | 0bab483177c08e206f585683504ab39e MD5 | raw file
  1. /**
  2. * Copyright (C) 2000 - 2011 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://repository.silverpeas.com/legal/licensing"
  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", "root.MSG_GEN_PARAM_VALUE",
  76. "After init");
  77. cipherText = cipher.doFinal(cipherBytes);
  78. } catch (Exception e) {
  79. throw new CryptageException("SilverCryptFactory.goCrypting", SilverpeasException.ERROR,
  80. "util.CRYPT_FAILED", e);
  81. }
  82. SilverTrace.info("util", "SilverCryptFactorySymetric.goCrypting", "root.MSG_GEN_EXIT_METHOD",
  83. "cipherText = " + Arrays.toString(cipherText));
  84. return cipherText;
  85. }
  86. public synchronized String goUnCrypting(byte[] cipherBytes) throws CryptageException {
  87. return goUnCrypting(cipherBytes, null);
  88. }
  89. public synchronized String goUnCrypting(byte[] cipherBytes, SilverCryptKeysSymetric symetricKeys)
  90. throws CryptageException {
  91. SilverTrace.info("util", "SilverCryptFactorySymetric.goUnCrypting", "root.MSG_GEN_ENTER_METHOD");
  92. String uncrypted = "";
  93. try {
  94. SilverCryptKeysSymetric keys = symetricKeys;
  95. if (symetricKeys == null) {
  96. keys = this.getSymetricKeys();
  97. }
  98. cipher.init(Cipher.DECRYPT_MODE, keys.getKey());
  99. SilverTrace.debug("util", "SilverCryptFactorySymetric.goUnCrypting",
  100. "root.MSG_GEN_PARAM_VALUE", "After init");
  101. byte[] newPlainText = cipher.doFinal(cipherBytes);
  102. uncrypted = new String(newPlainText, "UTF8");
  103. } catch (Exception e) {
  104. throw new CryptageException("SilverCryptFactory.goUnCrypting", SilverpeasException.ERROR,
  105. "util.UNCRYPT_FAILED", e);
  106. }
  107. SilverTrace.info("util", "SilverCryptFactorySymetric.goUnCrypting",
  108. "root.MSG_GEN_EXIT_METHOD", "uncrypted = " + uncrypted);
  109. return uncrypted;
  110. }
  111. /**
  112. * Returns the keyring.
  113. * @return the keyring.
  114. * @throws CryptageException
  115. */
  116. public SilverCryptKeysSymetric getSymetricKeys() throws CryptageException {
  117. if (silverCryptKeysSymetric == null) {
  118. silverCryptKeysSymetric = new SilverCryptKeysSymetric();
  119. }
  120. return silverCryptKeysSymetric;
  121. }
  122. }