PageRenderTime 67ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/main/java/org/mule/security/SecretKeyEncryptionStrategy.java

https://gitlab.com/thugside/mule
Java | 124 lines | 89 code | 17 blank | 18 comment | 6 complexity | 1b62f536e2e38692bf13982660b71e8d MD5 | raw file
  1. /*
  2. * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
  3. * The software in this package is published under the terms of the CPAL v1.0
  4. * license, a copy of which has been included with this distribution in the
  5. * LICENSE.txt file.
  6. */
  7. package org.mule.security;
  8. import org.mule.api.lifecycle.InitialisationException;
  9. import org.mule.config.i18n.CoreMessages;
  10. import org.mule.util.StringMessageUtils;
  11. import java.security.GeneralSecurityException;
  12. import java.security.spec.AlgorithmParameterSpec;
  13. import java.security.spec.KeySpec;
  14. import javax.crypto.Cipher;
  15. import javax.crypto.KeyGenerator;
  16. import javax.crypto.SecretKey;
  17. import javax.crypto.spec.SecretKeySpec;
  18. /**
  19. * SecretKey based encryption using JCE. Users must specify a key as an array of
  20. * bytes. This can be set directly on the strategy or a keyFactory can be specified.
  21. * A keyFactory is an implementation of {@link SecretKeyFactory} and must return a
  22. * byte array. The default algorthm used by this strategy is Blowfish, but users can
  23. * specify any valid algorithm supported by JCE.
  24. *
  25. * @see SecretKeyFactory
  26. * @deprecated This class is deprecated and will be removed in Mule 4.0. Use
  27. * {@link PasswordBasedEncryptionStrategy} instead, which follows the correct
  28. * way of transforming a string password into a cryptographic key
  29. */
  30. @Deprecated
  31. public class SecretKeyEncryptionStrategy extends AbstractJCEEncryptionStrategy
  32. {
  33. public static final String DEFAULT_ALGORITHM = "Blowfish";
  34. private byte[] key;
  35. private SecretKeyFactory keyFactory;
  36. public SecretKeyEncryptionStrategy()
  37. {
  38. algorithm = DEFAULT_ALGORITHM;
  39. }
  40. public void initialise() throws InitialisationException
  41. {
  42. if (key == null)
  43. {
  44. if (keyFactory == null)
  45. {
  46. throw new InitialisationException(CoreMessages.objectIsNull("Key / KeyFactory"), this);
  47. }
  48. else
  49. {
  50. try
  51. {
  52. key = keyFactory.getKey();
  53. }
  54. catch (Exception e)
  55. {
  56. throw new InitialisationException(e, this);
  57. }
  58. }
  59. }
  60. super.initialise();
  61. }
  62. @Override
  63. protected void createAndInitCiphers() throws GeneralSecurityException
  64. {
  65. encryptCipher = Cipher.getInstance(getAlgorithm());
  66. decryptCipher = Cipher.getInstance(getAlgorithm());
  67. AlgorithmParameterSpec paramSpec = createAlgorithmParameterSpec();
  68. if (paramSpec != null)
  69. {
  70. encryptCipher.init(Cipher.ENCRYPT_MODE, (SecretKeySpec) keySpec, paramSpec);
  71. decryptCipher.init(Cipher.DECRYPT_MODE, (SecretKeySpec) keySpec, paramSpec);
  72. }
  73. else
  74. {
  75. encryptCipher.init(Cipher.ENCRYPT_MODE, (SecretKeySpec) keySpec);
  76. decryptCipher.init(Cipher.DECRYPT_MODE, (SecretKeySpec) keySpec);
  77. }
  78. }
  79. protected KeySpec createKeySpec()
  80. {
  81. return new SecretKeySpec(key, algorithm);
  82. }
  83. protected AlgorithmParameterSpec createAlgorithmParameterSpec()
  84. {
  85. return null;
  86. }
  87. public void setKey(byte[] rawKey)
  88. {
  89. this.key = rawKey;
  90. }
  91. public void setKey(String rawKey)
  92. {
  93. this.key = StringMessageUtils.getBytes(rawKey);
  94. }
  95. public SecretKeyFactory getKeyFactory()
  96. {
  97. return keyFactory;
  98. }
  99. public void setKeyFactory(SecretKeyFactory keyFactory)
  100. {
  101. this.keyFactory = keyFactory;
  102. }
  103. protected SecretKey getSecretKey() throws GeneralSecurityException
  104. {
  105. return KeyGenerator.getInstance(algorithm).generateKey();
  106. }
  107. }