ToolsUtils.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. package com.zhongzheng.common.utils;
  2. import cn.hutool.core.convert.Convert;
  3. import cn.hutool.core.lang.Validator;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
  6. import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
  7. import com.aliyun.teaopenapi.models.Config;
  8. import com.google.zxing.common.BitMatrix;
  9. import com.zhongzheng.common.core.domain.AjaxResult;
  10. import com.zhongzheng.common.exception.CustomException;
  11. import io.micrometer.core.lang.NonNull;
  12. import net.sf.jsqlparser.expression.LongValue;
  13. import javax.crypto.Cipher;
  14. import javax.crypto.SecretKey;
  15. import javax.crypto.SecretKeyFactory;
  16. import javax.crypto.spec.DESKeySpec;
  17. import javax.crypto.spec.DESedeKeySpec;
  18. import javax.crypto.spec.IvParameterSpec;
  19. import javax.crypto.spec.SecretKeySpec;
  20. import java.awt.image.BufferedImage;
  21. import java.io.*;
  22. import java.nio.charset.StandardCharsets;
  23. import java.security.MessageDigest;
  24. import java.security.NoSuchAlgorithmException;
  25. import java.util.*;
  26. import static org.apache.xmlbeans.impl.util.Base64.encode;
  27. import java.util.regex.Matcher;
  28. import java.util.regex.Pattern;
  29. import java.util.zip.ZipEntry;
  30. import java.util.zip.ZipOutputStream;
  31. public class ToolsUtils {
  32. public static final int EMU_PER_PX = 9525;
  33. /**
  34. * 获取模块编码参数
  35. */
  36. public static String getSmsCode()
  37. {
  38. Random random = new Random();
  39. String result="";
  40. for (int i=0;i<6;i++)
  41. {
  42. result+=random.nextInt(10);
  43. }
  44. return result;
  45. }
  46. public static String join(@NonNull CharSequence delimiter, @NonNull Iterable tokens) {
  47. final Iterator<?> it = tokens.iterator();
  48. if (!it.hasNext()) {
  49. return "";
  50. }
  51. final StringBuilder sb = new StringBuilder();
  52. sb.append(it.next());
  53. while (it.hasNext()) {
  54. sb.append(delimiter);
  55. sb.append(it.next());
  56. }
  57. return sb.toString();
  58. }
  59. public static final int emuToPx(double emu) {
  60. return DoubleUtils.div(emu, EMU_PER_PX).intValue();
  61. }
  62. public static String getEncoding(String str) {
  63. String encode = "GB2312";
  64. try {
  65. if (str.equals(new String(str.getBytes(encode), encode))) {
  66. String s = encode;
  67. return s;
  68. }
  69. } catch (Exception exception) {
  70. }
  71. encode = "ISO-8859-1";
  72. try {
  73. if (str.equals(new String(str.getBytes(encode), encode))) {
  74. String s1 = encode;
  75. return s1;
  76. }
  77. } catch (Exception exception1) {
  78. }
  79. encode = "UTF-8";
  80. try {
  81. if (str.equals(new String(str.getBytes(encode), encode))) {
  82. String s2 = encode;
  83. return s2;
  84. }
  85. } catch (Exception exception2) {
  86. }
  87. encode = "GBK";
  88. try {
  89. if (str.equals(new String(str.getBytes(encode), encode))) {
  90. String s3 = encode;
  91. return s3;
  92. }
  93. } catch (Exception exception3) {
  94. }
  95. return "";
  96. }
  97. /**
  98. * 字符串转换UTF-8编码
  99. *
  100. * @param string 字符串
  101. * @return java.lang.String
  102. * @date 2022/4/14.
  103. */
  104. public static String toUtf8String(String string) {
  105. StringBuilder stringBuffer = new StringBuilder();
  106. for (int i = 0; i < string.length(); i++) {
  107. char c = string.charAt(i);
  108. if (c <= 255) {
  109. stringBuffer.append(c);
  110. } else {
  111. byte[] b;
  112. try {
  113. b = Character.toString(c).getBytes(StandardCharsets.UTF_8);
  114. } catch (Exception ex) {
  115. b = new byte[0];
  116. }
  117. for (int value : b) {
  118. int k = value;
  119. if (k < 0) k += 256;
  120. stringBuffer.append("%").append(Integer.toHexString(k).toUpperCase());
  121. }
  122. }
  123. }
  124. return stringBuffer.toString();
  125. }
  126. public static String StringToMd5(String psw) {
  127. {
  128. try {
  129. MessageDigest md5 = MessageDigest.getInstance("MD5");
  130. md5.update(psw.getBytes("UTF-8"));
  131. byte[] encryption = md5.digest();
  132. StringBuffer strBuf = new StringBuffer();
  133. for (int i = 0; i < encryption.length; i++) {
  134. if (Integer.toHexString(0xff & encryption[i]).length() == 1) {
  135. strBuf.append("0").append(Integer.toHexString(0xff & encryption[i]));
  136. } else {
  137. strBuf.append(Integer.toHexString(0xff & encryption[i]));
  138. }
  139. }
  140. return strBuf.toString();
  141. } catch (NoSuchAlgorithmException e) {
  142. return "";
  143. } catch (UnsupportedEncodingException e) {
  144. return "";
  145. }
  146. }
  147. }
  148. public static String getCharAndNumr(int length) {
  149. Random random = new Random();
  150. StringBuffer valSb = new StringBuffer();
  151. String charStr = "0123456789abcdefghijklmnopqrstuvwxyz";
  152. int charLength = charStr.length();
  153. for (int i = 0; i < length; i++) {
  154. int index = random.nextInt(charLength);
  155. valSb.append(charStr.charAt(index));
  156. }
  157. return valSb.toString();
  158. }
  159. public static <T> List<List<T>> splitListBycapacity(List<T> source, int capacity){
  160. List<List<T>> result=new ArrayList<List<T>>();
  161. if (source != null){
  162. int size = source.size();
  163. if (size > 0 ){
  164. for (int i = 0; i < size;) {
  165. List<T> value = null;
  166. int end = i+capacity;
  167. if (end > size){
  168. end = size;
  169. }
  170. value = source.subList(i,end);
  171. i = end;
  172. result.add(value);
  173. }
  174. }else {
  175. result = null;
  176. }
  177. }else {
  178. result = null;
  179. }
  180. return result;
  181. }
  182. /**
  183. * 校验签名
  184. *
  185. * @param token token
  186. * @param signature 签名
  187. * @param timestamp 时间戳
  188. * @param nonce 随机数
  189. * @return 布尔值
  190. */
  191. public static boolean checkGzhServerSignature(String token,String signature, String timestamp, String nonce) {
  192. String checktext = null;
  193. if (null != signature) {
  194. //对ToKen,timestamp,nonce 按字典排序
  195. String[] paramArr = new String[]{token, timestamp, nonce};
  196. Arrays.sort(paramArr);
  197. //将排序后的结果拼成一个字符串
  198. String content = paramArr[0].concat(paramArr[1]).concat(paramArr[2]);
  199. try {
  200. MessageDigest md = MessageDigest.getInstance("SHA-1");
  201. //对接后的字符串进行sha1加密
  202. byte[] digest = md.digest(content.toString().getBytes());
  203. checktext = byteToStr(digest);
  204. } catch (NoSuchAlgorithmException e) {
  205. e.printStackTrace();
  206. }
  207. }
  208. //将加密后的字符串与signature进行对比
  209. return checktext != null ? checktext.equals(signature.toUpperCase()) : false;
  210. }
  211. /**
  212. * 将字节数组转化我16进制字符串
  213. *
  214. * @param byteArrays 字符数组
  215. * @return 字符串
  216. */
  217. private static String byteToStr(byte[] byteArrays) {
  218. String str = "";
  219. for (int i = 0; i < byteArrays.length; i++) {
  220. str += byteToHexStr(byteArrays[i]);
  221. }
  222. return str;
  223. }
  224. /**
  225. * 将字节转化为十六进制字符串
  226. *
  227. * @param myByte 字节
  228. * @return 字符串
  229. */
  230. private static String byteToHexStr(byte myByte) {
  231. char[] Digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  232. char[] tampArr = new char[2];
  233. tampArr[0] = Digit[(myByte >>> 4) & 0X0F];
  234. tampArr[1] = Digit[myByte & 0X0F];
  235. String str = new String(tampArr);
  236. return str;
  237. }
  238. /**
  239. * 将文件转换成Byte数组
  240. */
  241. public static byte[] getBytesByFile(String pathStr) {
  242. File file = new File(pathStr);
  243. try {
  244. FileInputStream fis = new FileInputStream(file);
  245. ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
  246. byte[] b = new byte[1000];
  247. int n;
  248. while ((n = fis.read(b)) != -1) {
  249. bos.write(b, 0, n);
  250. }
  251. fis.close();
  252. byte[] data = bos.toByteArray();
  253. bos.close();
  254. return data;
  255. } catch (IOException e) {
  256. }
  257. return null;
  258. }
  259. public static String encodetoStr(byte[] src) {
  260. byte[] encoded = encode(src);
  261. return new String(encoded,0,0,encoded.length);
  262. }
  263. /**
  264. * 不够位数的在前面补0,保留num的长度位数字
  265. * @param code
  266. * @return
  267. */
  268. public static String autoGenericCode(String code, int num) {
  269. String result = "";
  270. // 保留num的位数
  271. // 0 代表前面补充0
  272. // num 代表长度为4
  273. // d 代表参数为正数型
  274. result = String.format("%0" + num + "d", Integer.parseInt(code));
  275. return result;
  276. }
  277. public static Boolean checkSignFromOldSys(String stamp,String sign) {
  278. String newSign = stamp+"pubilc2022";
  279. if(!sign.equals(ToolsUtils.EncoderByMd5(newSign))){
  280. return false;
  281. }
  282. if((Long.parseLong(stamp)+10L>(DateUtils.getNowTime().longValue()))&&(Long.parseLong(stamp)<(DateUtils.getNowTime().longValue()-10L))){
  283. return false;
  284. }
  285. return true;
  286. }
  287. public static Boolean checkOrderSignFromOldSys(String orderSn,String stamp,String sign) {
  288. String newSign = orderSn+stamp+"pubilc2022";
  289. if(!sign.equals(ToolsUtils.EncoderByMd5(newSign))){
  290. return false;
  291. }
  292. if((Long.parseLong(stamp)+10L>(DateUtils.getNowTime().longValue()))&&(Long.parseLong(stamp)<(DateUtils.getNowTime().longValue()-10L))){
  293. return false;
  294. }
  295. return true;
  296. }
  297. public static Boolean checkSignCwSnFromOldSys(String cwSn,String stamp,String sign) {
  298. String newSign = cwSn+stamp+"pubilc2022";
  299. if(!sign.equals(ToolsUtils.EncoderByMd5(newSign))){
  300. return false;
  301. }
  302. if((Long.parseLong(stamp)+10L>(DateUtils.getNowTime().longValue()))&&(Long.parseLong(stamp)<(DateUtils.getNowTime().longValue()-10L))){
  303. return false;
  304. }
  305. return true;
  306. }
  307. public static String EncoderByMd5WithUtf(String str) {
  308. String result = "";
  309. MessageDigest md5 = null;
  310. try {
  311. md5 = MessageDigest.getInstance("MD5");
  312. // 这句是关键
  313. md5.update(str.getBytes("UTF-8"));
  314. } catch (NoSuchAlgorithmException e) {
  315. // TODO Auto-generated catch block
  316. e.printStackTrace();
  317. } catch (UnsupportedEncodingException e) {
  318. // TODO Auto-generated catch block
  319. e.printStackTrace();
  320. }
  321. byte b[] = md5.digest();
  322. int i;
  323. StringBuffer buf = new StringBuffer("");
  324. for (int offset = 0; offset < b.length; offset++) {
  325. i = b[offset];
  326. if (i < 0)
  327. i += 256;
  328. if (i < 16)
  329. buf.append("0");
  330. buf.append(Integer.toHexString(i));
  331. }
  332. result = buf.toString();
  333. return result;
  334. }
  335. public static String EncoderByMd5(String str) {
  336. String result = "";
  337. MessageDigest md5 = null;
  338. try {
  339. md5 = MessageDigest.getInstance("MD5");
  340. // 这句是关键
  341. md5.update(str.getBytes("gbk"));
  342. } catch (NoSuchAlgorithmException e) {
  343. // TODO Auto-generated catch block
  344. e.printStackTrace();
  345. } catch (UnsupportedEncodingException e) {
  346. // TODO Auto-generated catch block
  347. e.printStackTrace();
  348. }
  349. byte b[] = md5.digest();
  350. int i;
  351. StringBuffer buf = new StringBuffer("");
  352. for (int offset = 0; offset < b.length; offset++) {
  353. i = b[offset];
  354. if (i < 0)
  355. i += 256;
  356. if (i < 16)
  357. buf.append("0");
  358. buf.append(Integer.toHexString(i));
  359. }
  360. result = buf.toString();
  361. return result;
  362. }
  363. public static boolean verifEasyPwd(String passWord) {
  364. if(Validator.isEmpty(passWord)){
  365. return false;
  366. }
  367. if(passWord.length()<8||passWord.length()>16){
  368. throw new CustomException("密码长度限制8到16位");
  369. }
  370. return true;
  371. }
  372. public static boolean verifPwd(String passWord) {
  373. if(Validator.isEmpty(passWord)){
  374. return false;
  375. }
  376. /*if(passWord.length()<8||passWord.length()>18){
  377. return false;
  378. }*/
  379. String regExp = "^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?\\d)(?=.*?[!#@*&.])[a-zA-Z\\d!#@*&.]{8,16}$";
  380. Pattern p = Pattern.compile(regExp);
  381. Matcher m = p.matcher(passWord);
  382. if (m.matches()){
  383. return true;
  384. } else {
  385. throw new CustomException("密码应由8-16位数字、大小写字母、符号组成");
  386. }
  387. }
  388. public static String getTenantId() {
  389. String TenantId = ServletUtils.getRequest().getHeader("TenantId");
  390. if(!StrUtil.isNotBlank(TenantId)||TenantId==null){
  391. TenantId = ServletUtils.getResponse().getHeader("TenantId");
  392. }
  393. if(Validator.isNotEmpty(TenantId)){
  394. if(TenantId.equals("undefined")){
  395. throw new CustomException("企业ID错误");
  396. }
  397. }
  398. return TenantId;
  399. }
  400. private static int getRandom(int count) {
  401. return (int) Math.round(Math.random() * (count));
  402. }
  403. public static String getRandomString(int length){
  404. String string = "abcdefghijklmnopqrstuvwxyz";
  405. StringBuffer sb = new StringBuffer();
  406. int len = string.length();
  407. for (int i = 0; i < length; i++) {
  408. sb.append(string.charAt(getRandom(len-1)));
  409. }
  410. return sb.toString();
  411. }
  412. public static BufferedImage toBufferedImage(BitMatrix matrix) {
  413. int black = 0xFF000000;
  414. int white = 0x00FFFFFF;
  415. int width = matrix.getWidth();
  416. int height = matrix.getHeight();
  417. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  418. for (int x = 0; x < width; x++) {
  419. for (int y = 0; y < height; y++) {
  420. image.setRGB(x, y, matrix.get(x, y) ? black : white);
  421. }
  422. }
  423. int imgHeight = image.getHeight();//取得图片的长和宽
  424. int imgWidth = image.getWidth();
  425. int c = image.getRGB(3, 3);
  426. int alpha = 10;
  427. //防止越位
  428. if (alpha < 0) {
  429. alpha = 0;
  430. } else if (alpha > 10) {
  431. alpha = 10;
  432. }
  433. BufferedImage tmpImg = new BufferedImage(imgWidth, imgHeight,BufferedImage.TYPE_4BYTE_ABGR);//新建一个类型支持透明的BufferedImage
  434. for(int i = 0; i < imgWidth; ++i)//把原图片的内容复制到新的图片,同时把背景设为透明
  435. {
  436. for(int j = 0; j < imgHeight; ++j){
  437. //把背景设为透明
  438. if(image.getRGB(i, j) == c){
  439. tmpImg .setRGB(i, j, c & 0x00ffffff);
  440. }
  441. //设置透明度
  442. else{
  443. int rgb = tmpImg .getRGB(i, j);
  444. rgb = ((alpha * 255 / 10) << 24) | (rgb & 0x00ffffff);
  445. tmpImg .setRGB(i, j, rgb);
  446. }
  447. }
  448. }
  449. return tmpImg ;
  450. }
  451. public static String solve(String num) {
  452. if (num == null) {
  453. return null;
  454. }
  455. // 判断是否有小数
  456. int index = num.indexOf(".");
  457. if (index >= 0) {
  458. String integer = num.substring(0, index);
  459. String decimal = num.substring(index);
  460. // 分隔后的整数+小数拼接起来
  461. return addSeparator(integer) + decimal;
  462. } else {
  463. return addSeparator(num);
  464. }
  465. }
  466. // 添加分隔符
  467. public static String addSeparator(String num) {
  468. int length = num.length();
  469. ArrayList list = new ArrayList();
  470. while (length > 3) {
  471. list.add(num.substring(length - 3, length));
  472. length = length - 3;
  473. }
  474. // 将前面小于三位的数字添加到ArrayList中
  475. list.add(num.substring(0, length));
  476. StringBuffer buffer = new StringBuffer();
  477. // 倒序拼接
  478. for (int i = list.size() - 1; i > 0; i--) {
  479. buffer.append(list.get(i) + ",");
  480. }
  481. buffer.append(list.get(0));
  482. return buffer.toString();
  483. }
  484. public static String dataSign(String sourceMsg, String pKey) throws NoSuchAlgorithmException {
  485. String tempPKey = MD5PubHasher(pKey.getBytes(StandardCharsets.UTF_8));
  486. tempPKey = sourceMsg + tempPKey;
  487. tempPKey = MD5PubHasher(tempPKey.getBytes(StandardCharsets.UTF_8));
  488. return tempPKey;
  489. }
  490. public static String MD5PubHasher(byte[] hashText) {
  491. try {
  492. MessageDigest md5 = MessageDigest.getInstance("MD5");
  493. byte[] b = md5.digest(hashText);
  494. StringBuilder ret = new StringBuilder();
  495. for (int i = 0; i < b.length; i++) {
  496. ret.append(String.format("%02x", b[i]));
  497. }
  498. return ret.toString();
  499. }catch (NoSuchAlgorithmException e){
  500. return null;
  501. }
  502. }
  503. public static String encryptDes(String source, byte[] key, byte[] iv) throws Exception {
  504. SecretKey secretKey = new SecretKeySpec(key, "DES");
  505. IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
  506. Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
  507. cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
  508. byte[] inputByteArray = source.getBytes(StandardCharsets.UTF_8);
  509. byte[] encryptedByteArray = cipher.doFinal(inputByteArray);
  510. return new String(java.util.Base64.getEncoder().encode(encryptedByteArray));
  511. }
  512. public static String decryptDes(String source, byte[] key, byte[] iv) throws Exception {
  513. SecretKey secretKey = new SecretKeySpec(key, "DES");
  514. IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
  515. Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
  516. cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
  517. byte[] inputByteArray = java.util.Base64.getDecoder().decode(source.getBytes(StandardCharsets.UTF_8));
  518. byte[] decryptedByteArray = cipher.doFinal(inputByteArray);
  519. return new String(decryptedByteArray, StandardCharsets.UTF_8);
  520. }
  521. /*public static String encryptDesNew(String source, String pass) throws Exception {
  522. byte[] rgbKey = pass.getBytes("UTF-8");
  523. byte[] rgbIV = pass.getBytes("UTF-8");
  524. DESedeKeySpec desKeySpec = new DESedeKeySpec(rgbKey);
  525. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  526. SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
  527. Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
  528. byte[] inputByteArray = source.getBytes("UTF-8");
  529. IvParameterSpec ivParameterSpec = new IvParameterSpec(rgbIV);
  530. cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
  531. byte[] encrypted = cipher.doFinal(inputByteArray);
  532. return Base64.getEncoder().encodeToString(encrypted);}
  533. }*/
  534. /**
  535. Des解密
  536. @param source 源字符串
  537. @param pass 密钥,长度必须8位
  538. @return 解密后的字符串 */
  539. /*public static String decryptDesNew(String source, String pass) throws Exception {
  540. byte[] rgbKey = pass.getBytes("UTF-8");
  541. byte[] rgbIV = pass.getBytes("UTF-8");
  542. DESedeKeySpec desKeySpec = new DESedeKeySpec(rgbKey);
  543. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  544. SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
  545. Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding")) {
  546. byte[] inputByteArray = Base64.getDecoder().decode(source);
  547. IvParameterSpec ivParameterSpec = new IvParameterSpec(rgbIV);
  548. cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
  549. byte[] decrypted = cipher.doFinal(inputByteArray);
  550. return new String(decrypted, "UTF-8");
  551. }
  552. }*/
  553. public static String encryptDesNew(String source, String pass) throws Exception {
  554. byte[] rgbKey = pass.getBytes(StandardCharsets.UTF_8);
  555. byte[] rgbIV = pass.getBytes(StandardCharsets.UTF_8);
  556. DESKeySpec desKeySpec = new DESKeySpec(rgbKey);
  557. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  558. SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
  559. Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
  560. byte[] inputByteArray = source.getBytes(StandardCharsets.UTF_8);
  561. IvParameterSpec ivParameterSpec = new IvParameterSpec(rgbIV);
  562. cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
  563. byte[] encryptedByteArray = cipher.doFinal(inputByteArray);
  564. return Base64.getEncoder().encodeToString(encryptedByteArray);
  565. }
  566. public static String decryptDesNew(String source, String pass) throws Exception {
  567. byte[] rgbKey = pass.getBytes(StandardCharsets.UTF_8);
  568. byte[] rgbIV = pass.getBytes(StandardCharsets.UTF_8);
  569. DESKeySpec desKeySpec = new DESKeySpec(rgbKey);
  570. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  571. SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
  572. Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
  573. byte[] inputByteArray = Base64.getDecoder().decode(source);
  574. IvParameterSpec ivParameterSpec = new IvParameterSpec(rgbIV);
  575. cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
  576. byte[] decryptedByteArray = cipher.doFinal(inputByteArray);
  577. return new String(decryptedByteArray, StandardCharsets.UTF_8);
  578. }
  579. private static String base64Encode(byte[] bytes) throws IOException {
  580. ByteArrayOutputStream output = new ByteArrayOutputStream();
  581. Base64.getEncoder().wrap(output).write(bytes);
  582. return output.toString(StandardCharsets.UTF_8.name());
  583. }
  584. /*private static byte[] base64Decode(String str) throws IOException {
  585. ByteArrayOutputStream output = new ByteArrayOutputStream();
  586. Base64.getDecoder().wrap(output).write(str.getBytes(StandardCharsets.UTF_8));
  587. return output.toByteArray();
  588. }*/
  589. private static byte[] base64Decode(String str) throws IOException {
  590. byte[] decodedString = Base64.getDecoder().decode(new String(str).getBytes("UTF-8"));
  591. return decodedString;
  592. }
  593. }