Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.53 KB | None | 0 0
  1. package com.zjh.blwy.opendoorqcode;
  2.  
  3. import android.text.TextUtils;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Calendar;
  6. import java.util.Date;
  7.  
  8. public class XXTEACAI {
  9.     private int[] a;
  10.     private final String[] b;
  11.     private a c;
  12.  
  13.     public enum OpenType {
  14.         NO_MATCH,
  15.         HOUSE_NUM,
  16.         MAC
  17.     }
  18.  
  19.     public enum TimeLimitType {
  20.         NO,
  21.         YES
  22.     }
  23.  
  24.     public static class a {
  25.         private String a;
  26.         private String b;
  27.         private TimeLimitType c = TimeLimitType.YES;
  28.         private int d = 1;
  29.         private int e = 60;
  30.         private Integer f = null;
  31.         private Integer g = null;
  32.         private OpenType h = OpenType.NO_MATCH;
  33.         private Integer i = null;
  34.         private String[] j;
  35.         private Date k = null;
  36.         private Date l = null;
  37.  
  38.         public void a(String str) {
  39.             this.a = str;
  40.         }
  41.  
  42.         public void a(TimeLimitType timeLimitType) {
  43.             this.c = timeLimitType;
  44.         }
  45.  
  46.         public void a(byte b) {
  47.             this.d = b;
  48.         }
  49.  
  50.         public void a(int i) {
  51.             this.e = i;
  52.         }
  53.  
  54.         public void b(int i) {
  55.             this.f = Integer.valueOf(i);
  56.         }
  57.  
  58.         public void c(int i) {
  59.             this.g = Integer.valueOf(i);
  60.         }
  61.  
  62.         public void a(String... strArr) {
  63.             int length = strArr.length;
  64.             int i = 0;
  65.             while (i < length) {
  66.                 String str = strArr[i];
  67.                 if (str.length() != 6) {
  68.                     throw new RuntimeException("please input macaddress last six number!");
  69.                 }
  70.                 try {
  71.                     Integer.valueOf(str, 16);
  72.                     i++;
  73.                 } catch (Exception e) {
  74.                     throw new RuntimeException("please input invalid mac address!");
  75.                 }
  76.             }
  77.             this.h = OpenType.MAC;
  78.             this.j = strArr;
  79.         }
  80.  
  81.         public void a(int i, int i2) {
  82.             if (i2 <= 0) {
  83.                 throw new RuntimeException("expriyDate must be >0");
  84.             }
  85.             Calendar instance = Calendar.getInstance();
  86.             instance.add(13, -i);
  87.             this.k = instance.getTime();
  88.             instance.add(13, i2);
  89.             this.l = instance.getTime();
  90.         }
  91.  
  92.         public XXTEACAI a() {
  93.             if (TextUtils.isEmpty(this.a) || this.a.contains("|")) {
  94.                 throw new RuntimeException("please call setUnique and unique not contain '|'");
  95.             } else if (this.f == null || this.g == null) {
  96.                 throw new RuntimeException("plase call setUserSecertKey and setDeviceSecertKey");
  97.             } else {
  98.                 switch (this.h) {
  99.                     case HOUSE_NUM:
  100.                         if (this.i == null || this.j == null || this.j.length <= 0) {
  101.                             throw new RuntimeException("please call buildOpenType,number must not null,serials must not null");
  102.                         } else if (this.j.length > 255) {
  103.                             throw new RuntimeException("serials max length is 255");
  104.                         }
  105.                         break;
  106.                     case MAC:
  107.                         if (this.j == null || this.j.length <= 0) {
  108.                             this.j = new String[0];
  109.                             break;
  110.                         } else if (this.j.length > 255) {
  111.                             throw new RuntimeException("serials max length is 255");
  112.                         }
  113.                         break;
  114.                 }
  115.                 if (TextUtils.isEmpty(this.b)) {
  116.                     this.b = String.format("%08x", new Object[]{Integer.valueOf((int) ((System.currentTimeMillis() / 1000) & -1))});
  117.                 }
  118.                 return new XXTEACAI();
  119.             }
  120.         }
  121.     }
  122.  
  123.     private XXTEACAI(a aVar) {
  124.         this.b = new String[8];
  125.         this.c = aVar;
  126.         this.b[7] = "8";
  127.         b();
  128.         c();
  129.         d();
  130.         e();
  131.         f();
  132.         g();
  133.         h();
  134.     }
  135.  
  136.     private void b() {
  137.         this.a = new int[4];
  138.         int[] iArr = this.a;
  139.         int[] iArr2 = this.a;
  140.         int intValue = this.c.g.intValue();
  141.         iArr2[3] = intValue;
  142.         iArr[1] = intValue;
  143.         iArr = this.a;
  144.         iArr2 = this.a;
  145.         intValue = this.c.f.intValue();
  146.         iArr2[0] = intValue;
  147.         iArr[2] = intValue;
  148.     }
  149.  
  150.     private void c() {
  151.         this.b[0] = this.c.a + "|";
  152.     }
  153.  
  154.     private void d() {
  155.         this.b[1] = this.c.b;
  156.     }
  157.  
  158.     private void e() {
  159.         this.b[3] = String.format("%08x", new Object[]{this.c.f});
  160.     }
  161.  
  162.     private void f() {
  163.         int ordinal = this.c.c.ordinal();
  164.         int ordinal2 = this.c.h.ordinal();
  165.         this.b[4] = String.format("%1$02x%2$02x%3$02x%4$04x000000", new Object[]{Integer.valueOf(ordinal), Integer.valueOf(this.c.d), Integer.valueOf(ordinal2), Integer.valueOf(this.c.e)});
  166.     }
  167.  
  168.     private void g() {
  169.         if (this.c.k == null) {
  170.             Calendar instance = Calendar.getInstance();
  171.             this.c.k = instance.getTime();
  172.             instance.add(5, 3);
  173.             this.c.l = instance.getTime();
  174.         }
  175.         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyMMddHHmmss");
  176.         this.b[5] = simpleDateFormat.format(this.c.k) + simpleDateFormat.format(this.c.l);
  177.     }
  178.  
  179.     private void h() {
  180.         int i = 0;
  181.         StringBuilder stringBuilder;
  182.         String[] k;
  183.         int length;
  184.         switch (this.c.h) {
  185.             case HOUSE_NUM:
  186.                 stringBuilder = new StringBuilder();
  187.                 stringBuilder.append(String.format("%02x", new Object[]{Integer.valueOf(this.c.j.length)}));
  188.                 stringBuilder.append(String.format("%08x", new Object[]{this.c.i}));
  189.                 k = this.c.j;
  190.                 length = k.length;
  191.                 while (i < length) {
  192.                     stringBuilder.append(k[i]);
  193.                     i++;
  194.                 }
  195.                 this.b[6] = stringBuilder.toString();
  196.                 return;
  197.             case MAC:
  198.                 stringBuilder = new StringBuilder();
  199.                 stringBuilder.append(String.format("%02x", new Object[]{Integer.valueOf(this.c.j.length)}));
  200.                 k = this.c.j;
  201.                 length = k.length;
  202.                 while (i < length) {
  203.                     stringBuilder.append(k[i]);
  204.                     i++;
  205.                 }
  206.                 this.b[6] = stringBuilder.toString();
  207.                 return;
  208.             default:
  209.                 this.b[6] = "";
  210.                 return;
  211.         }
  212.     }
  213.  
  214.     private String i() {
  215.         String str = this.b[4] + this.b[5] + this.b[6];
  216.         int length = str.length();
  217.         int i = (byte) (length / 8);
  218.         int i2 = (byte) (length % 8);
  219.         if (i2 != 0) {
  220.             i = (byte) (i + 1);
  221.         } else {
  222.             i2 = 8;
  223.         }
  224.         String toHexString = Integer.toHexString(i & 255);
  225.         if (toHexString.length() < 2) {
  226.             toHexString = "0" + toHexString;
  227.         }
  228.         String toHexString2 = Integer.toHexString(((byte) ((length % 8) / 2)) & 255);
  229.         if (toHexString2.length() < 2) {
  230.             toHexString2 = "0" + toHexString2;
  231.         }
  232.         this.b[2] = toHexString + toHexString2;
  233.         int i3 = i <= 20 ? 20 : i;
  234.         int[] iArr = new int[i3];
  235.         for (length = 0; length < i - 1; length++) {
  236.             iArr[length] = (int) Long.valueOf(str.substring(length * 8, (length + 1) * 8), 16).longValue();
  237.         }
  238.         iArr[i - 1] = (int) Long.valueOf(str.substring((i - 1) * 8, ((i - 1) * 8) + i2), 16).longValue();
  239.         int[] iArr2 = new int[i3];
  240.         iArr = a.a(iArr, this.a, i);
  241.         toHexString2 = "";
  242.         for (i3 = 0; i3 < i; i3++) {
  243.             toHexString2 = toHexString2 + a(Integer.toHexString(iArr[i3]).toUpperCase(), 8, true);
  244.         }
  245.         System.out.println("加密前字符串" + str);
  246.         System.out.println("加密后的字符串" + toHexString2);
  247.         a(iArr, i, i2);
  248.         return this.b[0] + this.b[1] + this.b[2] + this.b[3] + toHexString2;
  249.     }
  250.  
  251.     public String a(int[] iArr, int i, int i2) {
  252.         int[] iArr2 = new int[20];
  253.         int[] b = a.b(iArr, this.a, i);
  254.         String str = "";
  255.         String str2 = "";
  256.         for (int i3 = 0; i3 < i - 1; i3++) {
  257.             str = str + a(Integer.toHexString(b[i3]).toUpperCase(), 8, true);
  258.         }
  259.         if (i2 == 1) {
  260.             str2 = str + a(Integer.toHexString(b[i - 1]).toUpperCase(), 2, true);
  261.         } else if (i2 == 2) {
  262.             str2 = str + a(Integer.toHexString(b[i - 1]).toUpperCase(), 4, true);
  263.         } else if (i2 == 3) {
  264.             str2 = str + a(Integer.toHexString(b[i - 1]).toUpperCase(), 6, true);
  265.         } else {
  266.             str2 = str + a(Integer.toHexString(b[i - 1]).toUpperCase(), 8, true);
  267.         }
  268.         System.out.println("解密后字符串" + str2);
  269.         return str2;
  270.     }
  271.  
  272.     public final String a() {
  273.         return i();
  274.     }
  275.  
  276.     private String a(String str, int i, boolean z) {
  277.         if (str == null) {
  278.             str = "";
  279.         }
  280.         int length = i - str.length();
  281.         StringBuffer stringBuffer = new StringBuffer();
  282.         for (int i2 = 0; i2 < length; i2++) {
  283.             stringBuffer.append("0");
  284.         }
  285.         if (z) {
  286.             stringBuffer.append(str);
  287.         } else {
  288.             stringBuffer.insert(0, str);
  289.         }
  290.         return stringBuffer.toString();
  291.     }
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement