Util.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. package com.security.cipher.sm;
  2. import java.math.BigInteger;
  3. public class Util
  4. {
  5. /**
  6. * 整形转换成网络传输的字节流(字节数组)型数据
  7. *
  8. * @param num 一个整型数据
  9. * @return 4个字节的自己数组
  10. */
  11. public static byte[] intToBytes(int num)
  12. {
  13. byte[] bytes = new byte[4];
  14. bytes[0] = (byte) (0xff & (num >> 0));
  15. bytes[1] = (byte) (0xff & (num >> 8));
  16. bytes[2] = (byte) (0xff & (num >> 16));
  17. bytes[3] = (byte) (0xff & (num >> 24));
  18. return bytes;
  19. }
  20. /**
  21. * 四个字节的字节数据转换成一个整形数据
  22. *
  23. * @param bytes 4个字节的字节数组
  24. * @return 一个整型数据
  25. */
  26. public static int byteToInt(byte[] bytes)
  27. {
  28. int num = 0;
  29. int temp;
  30. temp = (0x000000ff & (bytes[0])) << 0;
  31. num = num | temp;
  32. temp = (0x000000ff & (bytes[1])) << 8;
  33. num = num | temp;
  34. temp = (0x000000ff & (bytes[2])) << 16;
  35. num = num | temp;
  36. temp = (0x000000ff & (bytes[3])) << 24;
  37. num = num | temp;
  38. return num;
  39. }
  40. /**
  41. * 长整形转换成网络传输的字节流(字节数组)型数据
  42. *
  43. * @param num 一个长整型数据
  44. * @return 4个字节的自己数组
  45. */
  46. public static byte[] longToBytes(long num)
  47. {
  48. byte[] bytes = new byte[8];
  49. for (int i = 0; i < 8; i++)
  50. {
  51. bytes[i] = (byte) (0xff & (num >> (i * 8)));
  52. }
  53. return bytes;
  54. }
  55. /**
  56. * 大数字转换字节流(字节数组)型数据
  57. *
  58. * @param n
  59. * @return
  60. */
  61. public static byte[] byteConvert32Bytes(BigInteger n)
  62. {
  63. byte tmpd[] = (byte[])null;
  64. if(n == null)
  65. {
  66. return null;
  67. }
  68. if(n.toByteArray().length == 33)
  69. {
  70. tmpd = new byte[32];
  71. System.arraycopy(n.toByteArray(), 1, tmpd, 0, 32);
  72. }
  73. else if(n.toByteArray().length == 32)
  74. {
  75. tmpd = n.toByteArray();
  76. }
  77. else
  78. {
  79. tmpd = new byte[32];
  80. for(int i = 0; i < 32 - n.toByteArray().length; i++)
  81. {
  82. tmpd[i] = 0;
  83. }
  84. System.arraycopy(n.toByteArray(), 0, tmpd, 32 - n.toByteArray().length, n.toByteArray().length);
  85. }
  86. return tmpd;
  87. }
  88. /**
  89. * 换字节流(字节数组)型数据转大数字
  90. *
  91. * @param b
  92. * @return
  93. */
  94. public static BigInteger byteConvertInteger(byte[] b)
  95. {
  96. if (b[0] < 0)
  97. {
  98. byte[] temp = new byte[b.length + 1];
  99. temp[0] = 0;
  100. System.arraycopy(b, 0, temp, 1, b.length);
  101. return new BigInteger(temp);
  102. }
  103. return new BigInteger(b);
  104. }
  105. /**
  106. * 根据字节数组获得值(十六进制数字)
  107. *
  108. * @param bytes
  109. * @return
  110. */
  111. public static String getHexString(byte[] bytes)
  112. {
  113. return getHexString(bytes, true);
  114. }
  115. /**
  116. * 根据字节数组获得值(十六进制数字)
  117. *
  118. * @param bytes
  119. * @param upperCase
  120. * @return
  121. */
  122. public static String getHexString(byte[] bytes, boolean upperCase)
  123. {
  124. String ret = "";
  125. for (int i = 0; i < bytes.length; i++)
  126. {
  127. ret += Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1);
  128. }
  129. return upperCase ? ret.toUpperCase() : ret;
  130. }
  131. /**
  132. * 打印十六进制字符串
  133. *
  134. * @param bytes
  135. */
  136. public static void printHexString(byte[] bytes)
  137. {
  138. for (int i = 0; i < bytes.length; i++)
  139. {
  140. String hex = Integer.toHexString(bytes[i] & 0xFF);
  141. if (hex.length() == 1)
  142. {
  143. hex = '0' + hex;
  144. }
  145. System.out.print("0x" + hex.toUpperCase() + ",");
  146. }
  147. System.out.println("");
  148. }
  149. /**
  150. * Convert hex string to byte[]
  151. *
  152. * @param hexString
  153. * the hex string
  154. * @return byte[]
  155. */
  156. public static byte[] hexStringToBytes(String hexString)
  157. {
  158. if (hexString == null || hexString.equals(""))
  159. {
  160. return null;
  161. }
  162. hexString = hexString.toUpperCase();
  163. int length = hexString.length() / 2;
  164. char[] hexChars = hexString.toCharArray();
  165. byte[] d = new byte[length];
  166. for (int i = 0; i < length; i++)
  167. {
  168. int pos = i * 2;
  169. d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
  170. }
  171. return d;
  172. }
  173. /**
  174. * Convert char to byte
  175. *
  176. * @param c
  177. * char
  178. * @return byte
  179. */
  180. public static byte charToByte(char c)
  181. {
  182. return (byte) "0123456789ABCDEF".indexOf(c);
  183. }
  184. /**
  185. * 用于建立十六进制字符的输出的小写字符数组
  186. */
  187. private static final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5',
  188. '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
  189. /**
  190. * 用于建立十六进制字符的输出的大写字符数组
  191. */
  192. private static final char[] DIGITS_UPPER = {'0', '1', '2', '3', '4', '5',
  193. '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  194. /**
  195. * 将字节数组转换为十六进制字符数组
  196. *
  197. * @param data byte[]
  198. * @return 十六进制char[]
  199. */
  200. public static char[] encodeHex(byte[] data) {
  201. return encodeHex(data, true);
  202. }
  203. /**
  204. * 将字节数组转换为十六进制字符数组
  205. *
  206. * @param data byte[]
  207. * @param toLowerCase <code>true</code> 传换成小写格式 , <code>false</code> 传换成大写格式
  208. * @return 十六进制char[]
  209. */
  210. public static char[] encodeHex(byte[] data, boolean toLowerCase) {
  211. return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
  212. }
  213. /**
  214. * 将字节数组转换为十六进制字符数组
  215. *
  216. * @param data byte[]
  217. * @param toDigits 用于控制输出的char[]
  218. * @return 十六进制char[]
  219. */
  220. protected static char[] encodeHex(byte[] data, char[] toDigits) {
  221. int l = data.length;
  222. char[] out = new char[l << 1];
  223. // two characters form the hex value.
  224. for (int i = 0, j = 0; i < l; i++) {
  225. out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
  226. out[j++] = toDigits[0x0F & data[i]];
  227. }
  228. return out;
  229. }
  230. /**
  231. * 将字节数组转换为十六进制字符串
  232. *
  233. * @param data byte[]
  234. * @return 十六进制String
  235. */
  236. public static String encodeHexString(byte[] data) {
  237. return encodeHexString(data, true);
  238. }
  239. /**
  240. * 将字节数组转换为十六进制字符串
  241. *
  242. * @param data byte[]
  243. * @param toLowerCase <code>true</code> 传换成小写格式 , <code>false</code> 传换成大写格式
  244. * @return 十六进制String
  245. */
  246. public static String encodeHexString(byte[] data, boolean toLowerCase) {
  247. return encodeHexString(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
  248. }
  249. /**
  250. * 将字节数组转换为十六进制字符串
  251. *
  252. * @param data byte[]
  253. * @param toDigits 用于控制输出的char[]
  254. * @return 十六进制String
  255. */
  256. protected static String encodeHexString(byte[] data, char[] toDigits) {
  257. return new String(encodeHex(data, toDigits));
  258. }
  259. /**
  260. * 将十六进制字符数组转换为字节数组
  261. *
  262. * @param data 十六进制char[]
  263. * @return byte[]
  264. * @throws RuntimeException 如果源十六进制字符数组是一个奇怪的长度,将抛出运行时异常
  265. */
  266. public static byte[] decodeHex(char[] data) {
  267. int len = data.length;
  268. if ((len & 0x01) != 0) {
  269. throw new RuntimeException("Odd number of characters.");
  270. }
  271. byte[] out = new byte[len >> 1];
  272. // two characters form the hex value.
  273. for (int i = 0, j = 0; j < len; i++) {
  274. int f = toDigit(data[j], j) << 4;
  275. j++;
  276. f = f | toDigit(data[j], j);
  277. j++;
  278. out[i] = (byte) (f & 0xFF);
  279. }
  280. return out;
  281. }
  282. /**
  283. * 将十六进制字符转换成一个整数
  284. *
  285. * @param ch 十六进制char
  286. * @param index 十六进制字符在字符数组中的位置
  287. * @return 一个整数
  288. * @throws RuntimeException 当ch不是一个合法的十六进制字符时,抛出运行时异常
  289. */
  290. protected static int toDigit(char ch, int index) {
  291. int digit = Character.digit(ch, 16);
  292. if (digit == -1) {
  293. throw new RuntimeException("Illegal hexadecimal character " + ch
  294. + " at index " + index);
  295. }
  296. return digit;
  297. }
  298. /**
  299. * 数字字符串转ASCII码字符串
  300. *
  301. * @param String
  302. * 字符串
  303. * @return ASCII字符串
  304. */
  305. public static String StringToAsciiString(String content) {
  306. String result = "";
  307. int max = content.length();
  308. for (int i = 0; i < max; i++) {
  309. char c = content.charAt(i);
  310. String b = Integer.toHexString(c);
  311. result = result + b;
  312. }
  313. return result;
  314. }
  315. /**
  316. * 十六进制转字符串
  317. *
  318. * @param hexString
  319. * 十六进制字符串
  320. * @param encodeType
  321. * 编码类型4:Unicode,2:普通编码
  322. * @return 字符串
  323. */
  324. public static String hexStringToString(String hexString, int encodeType) {
  325. String result = "";
  326. int max = hexString.length() / encodeType;
  327. for (int i = 0; i < max; i++) {
  328. char c = (char) hexStringToAlgorism(hexString
  329. .substring(i * encodeType, (i + 1) * encodeType));
  330. result += c;
  331. }
  332. return result;
  333. }
  334. /**
  335. * 十六进制字符串装十进制
  336. *
  337. * @param hex
  338. * 十六进制字符串
  339. * @return 十进制数值
  340. */
  341. public static int hexStringToAlgorism(String hex) {
  342. hex = hex.toUpperCase();
  343. int max = hex.length();
  344. int result = 0;
  345. for (int i = max; i > 0; i--) {
  346. char c = hex.charAt(i - 1);
  347. int algorism = 0;
  348. if (c >= '0' && c <= '9') {
  349. algorism = c - '0';
  350. } else {
  351. algorism = c - 55;
  352. }
  353. result += Math.pow(16, max - i) * algorism;
  354. }
  355. return result;
  356. }
  357. /**
  358. * 十六转二进制
  359. *
  360. * @param hex
  361. * 十六进制字符串
  362. * @return 二进制字符串
  363. */
  364. public static String hexStringToBinary(String hex) {
  365. hex = hex.toUpperCase();
  366. String result = "";
  367. int max = hex.length();
  368. for (int i = 0; i < max; i++) {
  369. char c = hex.charAt(i);
  370. switch (c) {
  371. case '0':
  372. result += "0000";
  373. break;
  374. case '1':
  375. result += "0001";
  376. break;
  377. case '2':
  378. result += "0010";
  379. break;
  380. case '3':
  381. result += "0011";
  382. break;
  383. case '4':
  384. result += "0100";
  385. break;
  386. case '5':
  387. result += "0101";
  388. break;
  389. case '6':
  390. result += "0110";
  391. break;
  392. case '7':
  393. result += "0111";
  394. break;
  395. case '8':
  396. result += "1000";
  397. break;
  398. case '9':
  399. result += "1001";
  400. break;
  401. case 'A':
  402. result += "1010";
  403. break;
  404. case 'B':
  405. result += "1011";
  406. break;
  407. case 'C':
  408. result += "1100";
  409. break;
  410. case 'D':
  411. result += "1101";
  412. break;
  413. case 'E':
  414. result += "1110";
  415. break;
  416. case 'F':
  417. result += "1111";
  418. break;
  419. }
  420. }
  421. return result;
  422. }
  423. /**
  424. * ASCII码字符串转数字字符串
  425. *
  426. * @param String
  427. * ASCII字符串
  428. * @return 字符串
  429. */
  430. public static String AsciiStringToString(String content) {
  431. String result = "";
  432. int length = content.length() / 2;
  433. for (int i = 0; i < length; i++) {
  434. String c = content.substring(i * 2, i * 2 + 2);
  435. int a = hexStringToAlgorism(c);
  436. char b = (char) a;
  437. String d = String.valueOf(b);
  438. result += d;
  439. }
  440. return result;
  441. }
  442. /**
  443. * 将十进制转换为指定长度的十六进制字符串
  444. *
  445. * @param algorism
  446. * int 十进制数字
  447. * @param maxLength
  448. * int 转换后的十六进制字符串长度
  449. * @return String 转换后的十六进制字符串
  450. */
  451. public static String algorismToHexString(int algorism, int maxLength) {
  452. String result = "";
  453. result = Integer.toHexString(algorism);
  454. if (result.length() % 2 == 1) {
  455. result = "0" + result;
  456. }
  457. return patchHexString(result.toUpperCase(), maxLength);
  458. }
  459. /**
  460. * 字节数组转为普通字符串(ASCII对应的字符)
  461. *
  462. * @param bytearray
  463. * byte[]
  464. * @return String
  465. */
  466. public static String byteToString(byte[] bytearray) {
  467. String result = "";
  468. char temp;
  469. int length = bytearray.length;
  470. for (int i = 0; i < length; i++) {
  471. temp = (char) bytearray[i];
  472. result += temp;
  473. }
  474. return result;
  475. }
  476. /**
  477. * 二进制字符串转十进制
  478. *
  479. * @param binary
  480. * 二进制字符串
  481. * @return 十进制数值
  482. */
  483. public static int binaryToAlgorism(String binary) {
  484. int max = binary.length();
  485. int result = 0;
  486. for (int i = max; i > 0; i--) {
  487. char c = binary.charAt(i - 1);
  488. int algorism = c - '0';
  489. result += Math.pow(2, max - i) * algorism;
  490. }
  491. return result;
  492. }
  493. /**
  494. * 十进制转换为十六进制字符串
  495. *
  496. * @param algorism
  497. * int 十进制的数字
  498. * @return String 对应的十六进制字符串
  499. */
  500. public static String algorismToHEXString(int algorism) {
  501. String result = "";
  502. result = Integer.toHexString(algorism);
  503. if (result.length() % 2 == 1) {
  504. result = "0" + result;
  505. }
  506. result = result.toUpperCase();
  507. return result;
  508. }
  509. /**
  510. * HEX字符串前补0,主要用于长度位数不足。
  511. *
  512. * @param str
  513. * String 需要补充长度的十六进制字符串
  514. * @param maxLength
  515. * int 补充后十六进制字符串的长度
  516. * @return 补充结果
  517. */
  518. static public String patchHexString(String str, int maxLength) {
  519. String temp = "";
  520. for (int i = 0; i < maxLength - str.length(); i++) {
  521. temp = "0" + temp;
  522. }
  523. str = (temp + str).substring(0, maxLength);
  524. return str;
  525. }
  526. /**
  527. * 将一个字符串转换为int
  528. *
  529. * @param s
  530. * String 要转换的字符串
  531. * @param defaultInt
  532. * int 如果出现异常,默认返回的数字
  533. * @param radix
  534. * int 要转换的字符串是什么进制的,如16 8 10.
  535. * @return int 转换后的数字
  536. */
  537. public static int parseToInt(String s, int defaultInt, int radix) {
  538. int i = 0;
  539. try {
  540. i = Integer.parseInt(s, radix);
  541. } catch (NumberFormatException ex) {
  542. i = defaultInt;
  543. }
  544. return i;
  545. }
  546. /**
  547. * 将一个十进制形式的数字字符串转换为int
  548. *
  549. * @param s
  550. * String 要转换的字符串
  551. * @param defaultInt
  552. * int 如果出现异常,默认返回的数字
  553. * @return int 转换后的数字
  554. */
  555. public static int parseToInt(String s, int defaultInt) {
  556. int i = 0;
  557. try {
  558. i = Integer.parseInt(s);
  559. } catch (NumberFormatException ex) {
  560. i = defaultInt;
  561. }
  562. return i;
  563. }
  564. /**
  565. * 十六进制串转化为byte数组
  566. *
  567. * @return the array of byte
  568. */
  569. public static byte[] hexToByte(String hex)
  570. throws IllegalArgumentException {
  571. if (hex.length() % 2 != 0) {
  572. throw new IllegalArgumentException();
  573. }
  574. char[] arr = hex.toCharArray();
  575. byte[] b = new byte[hex.length() / 2];
  576. for (int i = 0, j = 0, l = hex.length(); i < l; i++, j++) {
  577. String swap = "" + arr[i++] + arr[i];
  578. int byteint = Integer.parseInt(swap, 16) & 0xFF;
  579. b[j] = new Integer(byteint).byteValue();
  580. }
  581. return b;
  582. }
  583. /**
  584. * 字节数组转换为十六进制字符串
  585. *
  586. * @param b
  587. * byte[] 需要转换的字节数组
  588. * @return String 十六进制字符串
  589. */
  590. public static String byteToHex(byte b[]) {
  591. if (b == null) {
  592. throw new IllegalArgumentException(
  593. "Argument b ( byte array ) is null! ");
  594. }
  595. String hs = "";
  596. String stmp = "";
  597. for (int n = 0; n < b.length; n++) {
  598. stmp = Integer.toHexString(b[n] & 0xff);
  599. if (stmp.length() == 1) {
  600. hs = hs + "0" + stmp;
  601. } else {
  602. hs = hs + stmp;
  603. }
  604. }
  605. return hs.toUpperCase();
  606. }
  607. public static byte[] subByte(byte[] input, int startIndex, int length) {
  608. byte[] bt = new byte[length];
  609. for (int i = 0; i < length; i++) {
  610. bt[i] = input[i + startIndex];
  611. }
  612. return bt;
  613. }
  614. }