Utils.java 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package com.izouma.dingtalk.utils.aes;
  2. import java.util.Random;
  3. /**
  4. * 加解密工具类
  5. */
  6. public class Utils {
  7. /**
  8. * 获取随机字符串
  9. *
  10. * @return
  11. */
  12. public static String getRandomStr(int count) {
  13. String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  14. Random random = new Random();
  15. StringBuffer sb = new StringBuffer();
  16. for (int i = 0; i < count; i++) {
  17. int number = random.nextInt(base.length());
  18. sb.append(base.charAt(number));
  19. }
  20. return sb.toString();
  21. }
  22. /*
  23. * int转byte数组,高位在前
  24. */
  25. public static byte[] int2Bytes(int count) {
  26. byte[] byteArr = new byte[4];
  27. byteArr[3] = (byte) (count & 0xFF);
  28. byteArr[2] = (byte) (count >> 8 & 0xFF);
  29. byteArr[1] = (byte) (count >> 16 & 0xFF);
  30. byteArr[0] = (byte) (count >> 24 & 0xFF);
  31. return byteArr;
  32. }
  33. /**
  34. * 高位在前bytes数组转int
  35. *
  36. * @param byteArr
  37. * @return
  38. */
  39. public static int bytes2int(byte[] byteArr) {
  40. int count = 0;
  41. for (int i = 0; i < 4; i++) {
  42. count <<= 8;
  43. count |= byteArr[i] & 0xff;
  44. }
  45. return count;
  46. }
  47. }