BLE_WIFI_Scale_Server_Api 服务器与wifi秤交互只需要实现3个接口:设备注册、获取用户、上传记录
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Base64Util.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package com.inet.ailink.receiver.common.utils;
  2. import java.io.UnsupportedEncodingException;
  3. import java.util.Base64;
  4. //import com.google.common.primitives.Bytes;
  5. public class Base64Util
  6. {
  7. final static Base64.Decoder decoder = Base64.getDecoder();
  8. final static Base64.Encoder encoder = Base64.getEncoder();
  9. public Base64Util(){}
  10. /**
  11. * 解码
  12. * @param encoderStr
  13. * @return
  14. * @throws UnsupportedEncodingException
  15. */
  16. public static String decoderString(String base64Text) throws UnsupportedEncodingException{
  17. String result = "";
  18. if(base64Text != null && !base64Text.isEmpty() && !base64Text.equals("")){
  19. byte[] textByte = decoder.decode(base64Text);
  20. result = bytes_String16(textByte);
  21. }
  22. return result;
  23. }
  24. /**
  25. * byte[]转16进制字符串
  26. * @param b
  27. * @return
  28. */
  29. public static String bytes_String16(byte[] b) {
  30. StringBuilder sb = new StringBuilder();
  31. for(int i=0;i<b.length;i++) {
  32. sb.append(String.format("%02x", b[i]));
  33. }
  34. return sb.toString();
  35. }
  36. /**
  37. * 编码
  38. * @param encoderStr
  39. * @return
  40. * @throws UnsupportedEncodingException
  41. */
  42. public static String encoderString(String base64Text) throws UnsupportedEncodingException{
  43. String result = "";
  44. if(base64Text != null && !base64Text.isEmpty() && !base64Text.equals("")){
  45. byte[] textByte = base64Text.getBytes("UTF-8");
  46. result = encoder.encodeToString(textByte);
  47. }
  48. return result;
  49. }
  50. /**
  51. * 编码
  52. * @param encoderStr
  53. * @return
  54. * @throws UnsupportedEncodingException
  55. */
  56. public static String encoderString(byte[] textByte) throws UnsupportedEncodingException{
  57. return encoder.encodeToString(textByte);
  58. }
  59. public static int getLength(String s) {
  60. int length = 0;
  61. for (int i = 0; i < s.length(); i++) {
  62. int ascii = Character.codePointAt(s, i);
  63. if (ascii >= 0 && ascii <= 255) {
  64. length++;
  65. } else {
  66. length += 2;
  67. }
  68. }
  69. return length;
  70. }
  71. private static String intToHex(int n) {
  72. StringBuffer s = new StringBuffer();
  73. String a;
  74. char []b = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  75. while(n != 0){
  76. s = s.append(b[n%16]);
  77. n = n/16;
  78. }
  79. a = s.reverse().toString();
  80. return a;
  81. }
  82. public static byte[] hexStrToByteArray(String str)
  83. {
  84. if (str == null) {
  85. return null;
  86. }
  87. if (str.length() == 0) {
  88. return new byte[0];
  89. }
  90. byte[] byteArray = new byte[str.length() / 2];
  91. for (int i = 0; i < byteArray.length; i++){
  92. String subStr = str.substring(2 * i, 2 * i + 2);
  93. byteArray[i] = ((byte)Integer.parseInt(subStr, 16));
  94. }
  95. return byteArray;
  96. }
  97. public static void main(String args[]) throws UnsupportedEncodingException
  98. {
  99. //01 01 16 b7 28 32 18 4a 88 00 0e 00 00 00 00 42 4d 10 01 0a 00 13 05 07 e9
  100. byte[] old =new byte[]{(byte)0x01
  101. ,(byte)0x01 ,(byte)0x16 ,(byte)0xb7 ,(byte)0x28
  102. ,(byte)0x32 ,(byte)0x18 ,(byte)0x4a ,(byte)0x88
  103. ,(byte)0x00 ,(byte)0x0e ,(byte)0x00 ,(byte)0x00
  104. ,(byte)0x00 ,(byte)0x00 ,(byte)0x42 ,(byte)0x4d
  105. ,(byte)0x10 ,(byte)0x01 ,(byte)0x0a ,(byte)0x00
  106. ,(byte)0x13 ,(byte)0x05 ,(byte)0x07 ,(byte)0xe9};
  107. //原始数据
  108. System.out.println( "原始数据:");
  109. for(byte i : old)
  110. System.out.print(i + " ");
  111. System.out.println();
  112. //tea加密
  113. byte[] teaDscrypt =TeaUtils.encrypt(old,null);
  114. System.out.println( "tea加密:");
  115. for(byte i : teaDscrypt)
  116. System.out.print(i + " ");
  117. System.out.println();
  118. //base编码
  119. String base64Text = encoderString(teaDscrypt);
  120. System.out.println("base64编码后的数据长度:"+getLength(base64Text)+":"+base64Text);
  121. //base解码
  122. String base64TextEn= decoderString(base64Text);
  123. System.out.println("base64解码后的数据长度:"+getLength(base64TextEn)+":"+base64TextEn);
  124. /*//转称byte数组(每两位一个16进制byte)
  125. List<String> base64TeaByteOldList = new ArrayList<String>();
  126. for(int i=0;i<base64TextEn.length();i++){
  127. if(i%2 == 0){
  128. base64TeaByteOldList.add("0x"+base64TextEn.charAt(i)+base64TextEn.charAt(i+1));
  129. }
  130. }
  131. //解码后转换得到的list
  132. System.out.println("解码后转换得到的list:");
  133. for(String byte16:base64TeaByteOldList){
  134. System.out.print(byte16+" ");
  135. }
  136. System.out.println();*/
  137. //解码数据转换为byte数组
  138. byte[] base64TeaByteOld = hexStrToByteArray(base64TextEn);
  139. System.out.println( "解码后得到的tea加密的byte数组:");
  140. for(byte i : base64TeaByteOld)
  141. System.out.print(i + " ");
  142. System.out.println();
  143. byte[] base64TeaByte =TeaUtils.decrypt(base64TeaByteOld,null);
  144. System.out.println( "tea解密:");
  145. for(byte i : base64TeaByte)
  146. System.out.print(i + " ");
  147. System.out.println();
  148. }
  149. }