/HinemosManager/src_commons/com/clustercontrol/commons/util/CryptUtil.java

https://github.com/hinemos/hinemos · Java · 162 lines · 123 code · 21 blank · 18 comment · 12 complexity · 81b14d288f019848617d44c7eb577a34 MD5 · raw file

  1. /*
  2. * Copyright (c) 2018 NTT DATA INTELLILINK Corporation. All rights reserved.
  3. *
  4. * Hinemos (http://www.hinemos.info/)
  5. *
  6. * See the LICENSE file for licensing information.
  7. */
  8. package com.clustercontrol.commons.util;
  9. import java.io.BufferedReader;
  10. import java.io.File;
  11. import java.io.FileReader;
  12. import java.io.IOException;
  13. import java.security.InvalidKeyException;
  14. import java.security.NoSuchAlgorithmException;
  15. import javax.crypto.BadPaddingException;
  16. import javax.crypto.Cipher;
  17. import javax.crypto.IllegalBlockSizeException;
  18. import javax.crypto.NoSuchPaddingException;
  19. import javax.crypto.spec.SecretKeySpec;
  20. import org.apache.commons.codec.binary.Base64;
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. public class CryptUtil {
  24. private static final Log m_log = LogFactory.getLog( CryptUtil.class );
  25. // 使用する暗号化アルゴリズム
  26. private static String algorithm = "BLOWFISH";
  27. private static String cryptKey = "hinemos";
  28. static {
  29. String etcdir = System.getProperty("hinemos.manager.etc.dir");
  30. String keyFile = "db_crypt.key";
  31. String keyPath = etcdir + File.separator + keyFile;
  32. FileReader fileReader = null;
  33. BufferedReader bufferedReader = null;
  34. try {
  35. fileReader = new FileReader(keyPath);
  36. bufferedReader = new BufferedReader(fileReader);
  37. cryptKey = bufferedReader.readLine();
  38. } catch (Exception e){
  39. m_log.warn("file not readable. (" + keyFile + ") : " + e.getMessage(), e);
  40. } finally {
  41. try {
  42. if (bufferedReader != null) {
  43. bufferedReader.close();
  44. }
  45. } catch (IOException e) {
  46. }
  47. try {
  48. if (fileReader != null) {
  49. fileReader.close();
  50. }
  51. } catch (IOException e) {
  52. }
  53. }
  54. // m_log.info("key=[" + cryptKey + "]"); // TODO この行は、コメントアウトすること!(パスワードがログに出力されてしまうので。)
  55. }
  56. public static String encrypt(String word) {
  57. return encrypt(cryptKey, word);
  58. }
  59. private static String encrypt(String key, String word) {
  60. if (word == null) {
  61. return null;
  62. }
  63. // 暗号化
  64. SecretKeySpec sksSpec = new SecretKeySpec(key.getBytes(), algorithm);
  65. Cipher cipher = null;
  66. try {
  67. cipher = Cipher.getInstance(algorithm);
  68. } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
  69. m_log.warn("encrypt : " + (e.getClass().getName()) + "," + e.getMessage(), e);
  70. return null;
  71. }
  72. try {
  73. cipher.init(Cipher.ENCRYPT_MODE, sksSpec);
  74. } catch (InvalidKeyException e) {
  75. m_log.warn("encrypt : " + (e.getClass().getName()) + "," + e.getMessage(), e);
  76. return null;
  77. }
  78. byte[] encrypted = null;
  79. try {
  80. encrypted = cipher.doFinal(word.getBytes());
  81. } catch (IllegalBlockSizeException | BadPaddingException e) {
  82. m_log.warn("encrypt : " + (e.getClass().getName()) + "," + e.getMessage(), e);
  83. return null;
  84. }
  85. return Base64.encodeBase64String(encrypted);
  86. }
  87. public static String decrypt(String word) {
  88. return decrypt(cryptKey, word);
  89. }
  90. public static String decrypt(String key, String word) {
  91. if (word == null) {
  92. return null;
  93. }
  94. byte[] encrypted = Base64.decodeBase64(word);
  95. // 複合化
  96. SecretKeySpec sksSpec = new SecretKeySpec(key.getBytes(), algorithm);
  97. Cipher cipher = null;
  98. try {
  99. cipher = Cipher.getInstance(algorithm);
  100. } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
  101. m_log.warn("encrypt : " + (e.getClass().getName()) + "," + e.getMessage(), e);
  102. return null;
  103. }
  104. try {
  105. cipher.init(Cipher.DECRYPT_MODE, sksSpec);
  106. } catch (InvalidKeyException e) {
  107. m_log.warn("encrypt : " + (e.getClass().getName()) + "," + e.getMessage(), e);
  108. return null;
  109. }
  110. byte[] decrypted;
  111. try {
  112. decrypted = cipher.doFinal(encrypted);
  113. } catch (IllegalBlockSizeException | BadPaddingException e) {
  114. m_log.warn("encrypt : " + (e.getClass().getName()) + "," + e.getMessage(), e);
  115. return null;
  116. }
  117. return new String(decrypted);
  118. }
  119. /**
  120. * バージョンアップツールから利用する
  121. * @param args
  122. */
  123. public static void main(String args[]) {
  124. if (args.length != 3) {
  125. System.out.println("usage CryptUtil encrypt <key> <word>");
  126. System.out.println("usage CryptUtil decrypt <key> <word>");
  127. System.exit(1);
  128. }
  129. String mode = args[0];
  130. String key = args[1];
  131. String word = args[2];
  132. // System.out.println("mode=" + mode + ", key=" + key + ", word=" + word);
  133. if ("decrypt".equals(mode)) {
  134. // System.out.println("decrypt");
  135. System.out.println(decrypt(key, word));
  136. } else {
  137. // System.out.println("encrypt");
  138. System.out.println(encrypt(key, word));
  139. }
  140. }
  141. }