/plugins/org.jkiss.dbeaver.ext.import_config/src/org/jkiss/dbeaver/ext/import_config/wizards/navicat/NavicatEncrypt.java

https://github.com/dbeaver/dbeaver · Java · 212 lines · 165 code · 33 blank · 14 comment · 13 complexity · c62fd8fc2bd71af77c4549890912ffeb MD5 · raw file

  1. /**
  2. * Copyright (c) 2011-2017 GitHub Inc.
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  4. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  5. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  6. *
  7. * @author DoubleLabyrinth
  8. * @author juliardi (Port to Java)
  9. * see https://github.com/DoubleLabyrinth/how-does-navicat-encrypt-password
  10. */
  11. package org.jkiss.dbeaver.ext.import_config.wizards.navicat;
  12. import org.jkiss.utils.CommonUtils;
  13. import javax.crypto.spec.SecretKeySpec;
  14. import java.security.MessageDigest;
  15. import java.util.Arrays;
  16. public class NavicatEncrypt {
  17. private static final String NAVICAT_CODE = "3DC5CA39";
  18. private byte[] _iv;
  19. private SecretKeySpec keySpec;
  20. private javax.crypto.Cipher _chiperCrypt;
  21. private javax.crypto.Cipher _chiperDecrypt;
  22. public NavicatEncrypt()
  23. {
  24. initKey();
  25. initChiperEncrypt();
  26. initChiperDecrypt();
  27. initIV();
  28. }
  29. private void initKey()
  30. {
  31. try {
  32. MessageDigest md = MessageDigest.getInstance("SHA1");
  33. md.update(NAVICAT_CODE.getBytes("US-ASCII"), 0, NAVICAT_CODE.length());
  34. byte[] key = md.digest();
  35. keySpec = new SecretKeySpec(key, "Blowfish");
  36. } catch (Exception e) {
  37. System.out.println(e.getMessage());
  38. e.printStackTrace();
  39. }
  40. }
  41. private void initChiperEncrypt()
  42. {
  43. try {
  44. // Must use NoPadding
  45. _chiperCrypt = javax.crypto.Cipher.getInstance("Blowfish/ECB/NoPadding");
  46. _chiperCrypt.init(javax.crypto.Cipher.ENCRYPT_MODE, keySpec);
  47. } catch (Exception e) {
  48. System.out.println(e.getMessage());
  49. e.printStackTrace();
  50. }
  51. }
  52. private void initChiperDecrypt()
  53. {
  54. try {
  55. // Must use NoPadding
  56. _chiperDecrypt = javax.crypto.Cipher.getInstance("Blowfish/ECB/NoPadding");
  57. _chiperDecrypt.init(javax.crypto.Cipher.DECRYPT_MODE, keySpec);
  58. } catch (Exception e) {
  59. System.out.println(e.getMessage());
  60. e.printStackTrace();
  61. }
  62. }
  63. private void initIV()
  64. {
  65. try {
  66. byte[] initVec = CommonUtils.parseHexString("FFFFFFFFFFFFFFFF");
  67. _iv = _chiperCrypt.doFinal(initVec);
  68. } catch (Exception e) {
  69. System.out.println(e.getMessage());
  70. e.printStackTrace();
  71. }
  72. }
  73. private void xorBytes(byte[] a, byte[] b)
  74. {
  75. for (int i = 0; i < (a.length); i++) {
  76. int aVal = a[i] & 0xff; // convert byte to integer
  77. int bVal = b[i] & 0xff;
  78. a[i] = (byte) (aVal ^ bVal); // xor aVal and bVal and typecast to
  79. // byte
  80. }
  81. }
  82. private void xorBytes(byte[] a, byte[] b, int l)
  83. {
  84. for (int i = 0; i < l; i++) {
  85. int aVal = a[i] & 0xff; // convert byte to integer
  86. int bVal = b[i] & 0xff;
  87. a[i] = (byte) (aVal ^ bVal); // xor aVal and bVal and typecast to
  88. // byte
  89. }
  90. }
  91. public String encrypt(String inputString)
  92. {
  93. try {
  94. byte[] inData = inputString.getBytes("US-ASCII");
  95. byte[] outData = encrypt(inData);
  96. return CommonUtils.toHexString(outData);
  97. } catch (Exception e) {
  98. System.out.println(e.getMessage());
  99. e.printStackTrace();
  100. return "";
  101. }
  102. }
  103. private byte[] encrypt(byte[] inData)
  104. {
  105. try {
  106. byte[] CV = Arrays.copyOf(_iv, _iv.length);
  107. byte[] ret = new byte[inData.length];
  108. int left = inData.length % 8;
  109. int rounded = Math.floorDiv(inData.length, 8);
  110. for (int i = 0; i < rounded; i++) {
  111. byte[] tmp = Arrays.copyOfRange(inData, i * 8, (i * 8) + 8);
  112. xorBytes(tmp, CV);
  113. tmp = _chiperCrypt.doFinal(tmp);
  114. xorBytes(CV, tmp);
  115. for (int j = 0; j < tmp.length; j++) {
  116. ret[i * 8 + j] = tmp[j];
  117. }
  118. }
  119. if (left != 0) {
  120. CV = _chiperCrypt.doFinal(CV);
  121. byte[] tmp = Arrays.copyOfRange(inData, rounded * 8, (rounded * 8) + left);
  122. xorBytes(tmp, CV, left);
  123. for (int j = 0; j < tmp.length; j++) {
  124. ret[rounded * 8 + j] = tmp[j];
  125. }
  126. }
  127. return ret;
  128. } catch (Exception e) {
  129. System.out.println(e.getMessage());
  130. e.printStackTrace();
  131. return null;
  132. }
  133. }
  134. public String decrypt(String hexString)
  135. {
  136. try {
  137. byte[] inData = CommonUtils.parseHexString(hexString);
  138. byte[] outData = decrypt(inData);
  139. return new String(outData);
  140. } catch (Exception e) {
  141. System.out.println(e.getMessage());
  142. e.printStackTrace();
  143. return "";
  144. }
  145. }
  146. private byte[] decrypt(byte[] inData)
  147. {
  148. try {
  149. byte[] CV = Arrays.copyOf(_iv, _iv.length);
  150. byte[] ret = new byte[inData.length];
  151. int left = inData.length % 8;
  152. int rounded = Math.floorDiv(inData.length, 8);
  153. for (int i = 0; i < rounded; i++) {
  154. byte[] tmp = Arrays.copyOfRange(inData, i * 8, (i * 8) + 8);
  155. tmp = _chiperDecrypt.doFinal(tmp);
  156. xorBytes(tmp, CV);
  157. for (int j = 0; j < tmp.length; j++) {
  158. ret[(i * 8) + j] = tmp[j];
  159. }
  160. for (int j = 0; j < CV.length; j++) {
  161. CV[j] = (byte) (CV[j] ^ inData[i * 8 + j]);
  162. }
  163. }
  164. if (left != 0) {
  165. CV = _chiperDecrypt.doFinal(CV);
  166. byte[] tmp = Arrays.copyOfRange(inData, rounded * 8, (rounded * 8) + left);
  167. xorBytes(tmp, CV, left);
  168. for (int j = 0; j < tmp.length; j++) {
  169. ret[rounded * 8 + j] = tmp[j];
  170. }
  171. }
  172. return ret;
  173. } catch (Exception e) {
  174. System.out.println(e.getMessage());
  175. e.printStackTrace();
  176. return null;
  177. }
  178. }
  179. }