123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- package com.inet.ailink.receiver.common.utils;
-
- import java.io.UnsupportedEncodingException;
-
- public class Base64TeaUitls {
-
-
- //加密
- public static String encrypt(String dataValue){
- byte[] old = BytesUtils.getBytes(Integer.parseInt(dataValue));
- String result = "";
- try {
- //tea加密
- byte[] teaDscrypt =TeaUtils.encrypt(old,null);
- //base编码
- result= Base64Util.encoderString(teaDscrypt);
- } catch (UnsupportedEncodingException e) {
- return result;
- }
- return result;
- }
-
- //解密
- public static byte[] decrypt(String base64Text){
- //base解码
- String base64TextEn;
- try {
- base64TextEn = Base64Util.decoderString(base64Text);
- //解码数据转换为byte数组
- byte[] base64TeaByteOld = Base64Util.hexStrToByteArray(base64TextEn);
- //tea解密byte数组
- byte[] base64TeaByte =TeaUtils.decrypt(base64TeaByteOld,null);
- //将负数转换为整数
- /*if(base64TeaByte != null){
- for(int i=0;i<base64TeaByte.length;i++){
- if(base64TeaByte[i]<0){
- base64TeaByte[i] = (byte) (256 + base64TeaByte[i]);
- }
- }
- }*/
- return base64TeaByte;
- } catch (UnsupportedEncodingException e) {
- return null;
- }
- }
-
- /**
- * 十进制转16进制
- * @param n
- * @return
- */
- private static String intToHex(int n) {
- StringBuilder sb = new StringBuilder(8);
- String a;
- char []b = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
- if(n==0){
- return "00";
- }
- while(n != 0){
- if(n<0){
- n= 256 +n;
- }
- sb = sb.append(b[n%16]);
- n = n/16;
- }
-
- a = sb.reverse().toString();
- if(a.length() == 1){
- a= "0"+a;
- }
- return a;
- }
-
- /**
- * 获取开始下标到结束下标的16进制的值
- * @param start
- * @param end
- * @param params
- * @return
- */
- public static String getStartToEndForHex(int start,int end,byte[] params){
- String result = "";
- for(int i=start;i<=end;i++){
- if(i==end){
- result = result+intToHex(params[i]);
- }else{
- result = result+intToHex(params[i])+":";
- }
- // System.out.println(params[i]+":"+intToHex(params[i]));
- }
- return result;
- }
-
- /**
- * 获取开始下标到结束下标的10进制的值
- * @param start
- * @param end
- * @param params
- * @return
- */
- public static String getStartToEndForInt(int start,int end,byte[] params){
- String result = "";
- for(int i=start;i<=end;i++){
- int temp = params[i];
- if(temp < 0){
- temp = 256 + temp;
- }
- if(i==end){
- result = result+temp;
- }else{
- result = result+temp+":";
- }
- }
- return result;
- }
-
- /*public static String getLowToHighEndForInt(int indexLow,int indexHigh,byte[] params){
- String result = "";
- int tempLow = params[indexLow];
- if(tempLow < 0){
- tempLow = 256 + tempLow;
- }
- int tempHigh = params[indexHigh];
- if(tempHigh < 0){
- tempHigh = 256 + tempHigh;
- }
- int tempResult = (tempLow << 8) | tempHigh;
- return result+tempResult;
- }*/
-
- /**
- * 获取低位到高位的移位+或运算的值
- * @param indexLow
- * @param indexHigh
- * @param params
- * @return
- */
- public static String getLowConactHighEndForInt(int indexLow,int indexHigh,byte[] params){
- String result = "";
- int tempResult = 0;
-
- for(int i = indexLow;i<=indexHigh;i++){
- int temp = (params[i] < 0 ? 256 + params[i] : params[i]);
- int tempmov = (indexHigh-i)+2;
- tempmov = (i == indexHigh ? 0 : (int) Math.pow(2, tempmov));
- tempResult = (temp << tempmov) | tempResult;
- }
-
- return result+tempResult;
- }
-
-
- public static String byteToHex(byte b){
- String hex = Integer.toHexString(b & 0xFF);
- if(hex.length() < 2){
- hex = "0" + hex;
- }
- return hex;
- }
-
- /**
- * 获取开始下标到结束下标的ASIII对应的值
- * @param start
- * @param end
- * @param params
- * @return
- */
- public static String getStartToEndForASIII(int start,int end,byte[] params){
- String result = "";
- for(int i=start;i<=end;i++){
- if(i==end){
- result = result+asciiToString(params[i]+"");
- }else{
- result = result+asciiToString(params[i]+"")+":";
- }
- }
- return result;
- }
-
- /**
- * ASCII转字符
- * @param value
- * @return
- */
- public static String asciiToString(String value)
- {
- value = value.replaceAll(":", "");
- char c= (char) Integer.parseInt(value);
- //System.out.println(value+":"+c);
- return c+"";
- }
-
- public static void main(String args[]) throws UnsupportedEncodingException
- {
- // String str = "H4CpBA/xguYaSidJuBnCEydjVVdwDBKAfgvuaUpSEuU=";
- // byte[] paramsByte = Base64TeaUitls.decrypt(str);
- //
- // for(byte i : paramsByte)
- // System.out.print(byteToHex(i) + " ");
- // System.out.println();
-
- //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
- //注册设备数据
- // byte[] old =new byte[]{
- // (byte)0x01,(byte)0x01,(byte)0x08 ,(byte)0x16 ,(byte)0xb7 ,(byte)0x28 //mac地址
- // ,(byte)0x00 ,(byte)0x00 //cid
- // ,(byte)0x00 ,(byte)0x01 //pid
- // ,(byte)0x00 ,(byte)0x02 //vid
- // ,(byte)0x42 ,(byte)0x4d //产品名称
- // ,(byte)0x01 //产品型号
- // ,(byte)0x01 //硬件版本
- // ,(byte)0x42 //软件版本
- // ,(byte)0x4d //用户版本
- // ,(byte)0x13 //年
- // ,(byte)0x0c //月
- // ,(byte)0x12 //日
- // ,(byte)0x06,(byte)0x06,(byte)0x06 ,(byte)0x06 ,(byte)0x06 ,(byte)0x06 //芯片id
- // ,(byte)0x09 //设备单位
- // ,(byte)0x03 //设备IMEI长度
- // ,(byte)0x01,(byte)0x02,(byte)0x20 }; //设备IMEI
-
- //上报称重数据(原始数据)
- //5781
- // byte[] old =new byte[]{
- // (byte)0x00,(byte)0x00 ,(byte)0x0f,(byte)0x71 //设备SN号
- // ,(byte)0x15 //年
- // ,(byte)0x03 //月
- // ,(byte)0x04 //日
- // ,(byte)0x0c //时
- // ,(byte)0x00 //分
- // ,(byte)0x00 //秒
- // ,(byte)0x00 //是否离线记录
- // ,(byte)0x00 //体重数据高
- // ,(byte)0x00 //体重数据中
- // ,(byte)0x3C //体重数据低
- // ,(byte)0x00 //体重单位
- // ,(byte)0x00 //体重精度
- // ,(byte)0x01 //阻抗高
- // ,(byte)0xff //阻抗低
- // ,(byte)0x5f //心率
- // ,(byte)0x01 //算法
- // ,(byte)0x00,(byte)0x00,(byte)0x03,(byte)0x97}; //用户id
-
- //最新OTA升级地址
- // byte[] old =new byte[]{
- // (byte)0x01,(byte)0x01,(byte)0x08 ,(byte)0x16 ,(byte)0xb7 ,(byte)0x28 //mac地址
- // ,(byte)0x00 ,(byte)0x00 //cid
- // ,(byte)0x00 ,(byte)0x01 //pid
- // ,(byte)0x00 ,(byte)0x02 //vid
- // ,(byte)0x42 ,(byte)0x4d //产品名称
- // ,(byte)0x01 //产品型号
- // ,(byte)0x01 //硬件版本
- // ,(byte)0x09 //软件版本
- // ,(byte)0x06,(byte)0x06,(byte)0x06 ,(byte)0x06 ,(byte)0x06 ,(byte)0x06 //芯片id
- // ,(byte)0x00,(byte)0x00 ,(byte)0x07,(byte)0xBF //设备id
- // ,(byte)0x00 //客户定制版本
- // ,(byte)0x00 ,(byte)0x00,(byte)0x00,(byte)0x00 //保留位
- // };
-
- // //通过cid获取设备访问地址
- byte[] old =new byte[]{
- (byte)0x00 ,(byte)0x00,(byte)0x00, //模块名称
- (byte)0x01,(byte)0x01,(byte)0x08 ,(byte)0x16 ,(byte)0xb7 ,(byte)0x28, //mac地址
- (byte)0x01,(byte)0x01,(byte)0x08 ,(byte)0x16 ,(byte)0xb7 ,(byte)0x28, //芯片id
- (byte)0x00 ,(byte)0x11, //cid
- (byte)0x00 ,(byte)0x01, //pid
- (byte)0x00 ,(byte)0x1B, //vid
- (byte)0x00,(byte)0x00 ,(byte)0x00 //保留位
- };
- //通过cid,pid,vid获取设备访问地址
- //57 4d 05 88 4a 18 32 28 a4 02 38 6f a5 dc 12 00 11 00 00 00 00 00 00 00
- // byte[] old =new byte[]{
- // (byte)0x57 ,(byte)0x4d,(byte)0x05, //模块名称
- // (byte)0x88,(byte)0x4a,(byte)0x08 ,(byte)0x32 ,(byte)0x28 ,(byte)0xa4, //mac地址
- // (byte)0x02,(byte)0x38,(byte)0x6f ,(byte)0xa5 ,(byte)0xdc ,(byte)0x12, //芯片id
- // (byte)0x00 ,(byte)0x0e, //cid
- // (byte)0x00 ,(byte)0x01, //pid
- // (byte)0x00 ,(byte)0x32, //vid
- // (byte)0x00,(byte)0x00 ,(byte)0x00 //保留位
- // };
- //刷牙数据上传
- // byte[] old =new byte[]{
- // (byte)0x00,(byte)0x00 ,(byte)0x07,(byte)0xBF, //设备SN号
- // (byte)0x01, //牙刷工作模式
- // (byte)0x00, // 刷牙时长高字节
- // (byte)0x03, // 刷牙时长低字节
- // (byte)0x00, //左边刷牙时长高字节
- // (byte)0x02, //左边刷牙时长低字节
- // (byte)0x00, //右边刷牙时长低字节
- // (byte)0x01, //右边刷牙时长低字节
- // (byte)0x09, //剩余电量
- // (byte)0x00,(byte)0x04, //默认刷牙时长
- // (byte)0x00,(byte)0x00,//保留位
- // (byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,//保留位
- // (byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00 //保留位
- // };
- //血糖数据
- // byte[] old =new byte[]{
- // (byte)0x00,(byte)0x00 ,(byte)0x07,(byte)0xBF, //设备SN号
- // (byte)0x00, //血糖数据高字节
- // (byte)0x00, //血糖数据中字节
- // (byte)0x12, //血糖数据低字节
- // (byte)0x01, //血糖单位
- // (byte)0x01, //小数点
- // (byte)0x00, //状态码
- // (byte)0x10, //剩余电量
- // (byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,//保留位
- // (byte)0x00 //保留位
- // };
- /*int a= 1846;
- System.out.println("原始数字:"+a);
- byte[] old = BytesUtils.getBytes(a);*/
-
- //通过设备id,获取设备单位
- // byte[] old =new byte[]{
- // (byte)0x00,(byte)0x00 ,(byte)0x0F,(byte)0x72, //设备id
- // (byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,//保留位
- // };
-
- //通过设备id,修改设备单位
- // byte[] old =new byte[]{
- // (byte)0x00,(byte)0x00 ,(byte)0x0B,(byte)0x31, //设备id
- // (byte)0x01 //设备单位
- // };
- //注册设备数据
- // byte[] old =new byte[]{
- // (byte)0x00, (byte)0x00 , (byte)0x00 , (byte)0x00 , (byte)0x00, (byte)0x00 //设备MAC
- // , (byte)0x00, (byte)0x2c//CID
- // , (byte)0x00 , (byte)0x00//PID
- // , (byte)0x00 , (byte)0x00//VID
- // , (byte)0x4c//模块名称
- // , (byte)0x4d
- // , (byte)0x09
- // , (byte)0x00
- // , (byte)0x0a
- // , (byte)0x00 , (byte)0x15 , (byte)0x04 , (byte)0x07 , (byte)0x00 , (byte)0x00
- // , (byte)0x00 , (byte)0x00 , (byte)0x00 , (byte)0x00 , (byte)0x00
- // , (byte)0x0f //设备IMEI长度
- // , (byte)0x08 , (byte)0x06 , (byte)0x09 , (byte)0x03 , (byte)0x02 , (byte)0x04 , (byte)0x00 , (byte)0x05 //设备IMEI 86932405
- // , (byte)0x00 , (byte)0x00 , (byte)0x00 , (byte)0x02 , (byte)0x07 , (byte)0x06 , (byte)0x09 //设备IMEI 0002769
- // , (byte)0x00, (byte)0x00 , (byte)0x00 , (byte)0x00 }; //备用字段
- //原始数据
- System.out.println( "原始数据:");
- for(byte i : old)
- System.out.print(byteToHex(i) + " ");
- System.out.println();
- //tea加密
- byte[] teaDscrypt =TeaUtils.encrypt(old,null);
- System.out.println( "tea加密:");
- for(byte i : teaDscrypt)
- System.out.print(byteToHex(i) + " ");
- System.out.println();
-
- //base编码
- String base64Text = Base64Util.encoderString(teaDscrypt);
- base64Text = "rvXiSvrWrR+/p1ZhAPlXr7x1gxbvrurA";
- System.out.println("base64编码后的数据长度:"+Base64Util.getLength(base64Text)+":"+base64Text);
- //base解码
- String base64TextEn= Base64Util.decoderString(base64Text);
- //String base64TextEn= Base64Util.decoderString("BCc0vnfr9kd5");
- // System.out.println("base64解码后的数据长度:"+Base64Util.getLength(base64TextEn)+":"+base64TextEn);
- /*//转称byte数组(每两位一个16进制byte)
- List<String> base64TeaByteOldList = new ArrayList<String>();
- for(int i=0;i<base64TextEn.length();i++){
- if(i%2 == 0){
- base64TeaByteOldList.add("0x"+base64TextEn.charAt(i)+base64TextEn.charAt(i+1));
- }
- }
- //解码后转换得到的list
- System.out.println("解码后转换得到的list:");
- for(String byte16:base64TeaByteOldList){
- System.out.print(byte16+" ");
- }
- System.out.println();*/
-
- //解码数据转换为byte数组
- byte[] base64TeaByteOld = Base64Util.hexStrToByteArray(base64TextEn);
- System.out.println( "解码后得到的tea加密的byte数组:");
- for(byte i : base64TeaByteOld)
- System.out.print(byteToHex(i) + " ");
- System.out.println();
-
- byte[] base64TeaByte =TeaUtils.decrypt(base64TeaByteOld,null);
-
- System.out.println( "tea解密:");
- for(byte i : base64TeaByte)
- System.out.print(byteToHex(i) + " ");
- System.out.println();
- //int imeiLength = base64TeaByte[28] & 0xFF;
- int imeiLength = Integer.parseInt(Base64TeaUitls.getStartToEndForInt(28, 28, base64TeaByte));
- System.out.println("imeiLength:"+imeiLength);
- System.out.println("IMEI:"+ Base64TeaUitls.getStartToEndForHex(28+1, 28+imeiLength, base64TeaByte));
- System.out.println("IMEI:"+ Base64TeaUitls.getStartToEndForInt(28+1, 28+imeiLength, base64TeaByte));
- System.out.println("IMEI:"+ Base64TeaUitls.getStartToEndForASIII(28+1, 28+imeiLength, base64TeaByte));
- }
- }
|