jhoel99

sss

Feb 25th, 2021
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.84 KB | None | 0 0
  1. package com.codeph.v2rayenc;
  2. // Portions copyright 2002, Google, Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. //     http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15.  
  16. import com.codeph.v2rayenc.*;
  17. import java.io.UnsupportedEncodingException;
  18. import java.io.ByteArrayOutputStream;
  19.  
  20.  
  21. public final class crypt {
  22.  
  23.  
  24.     private static final int DELTA = -1703701580;
  25.  
  26.     static final class TeaBase64
  27.     {
  28.  
  29.  
  30.         private  TeaBase64() {}
  31.  
  32.  
  33.         public static final String encode(byte[] data) {
  34.             final char[] base64EncodeChars = new char[] {
  35.                     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  36.                     'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  37.                     'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  38.                     'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  39.                     'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  40.                     'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  41.                     'w', 'x', 'y', 'z', '0', '1', '2', '3',
  42.                     '4', '5', '6', '7', '8', '9', '+', '/' };
  43.             StringBuilder sb = new StringBuilder();
  44.             int r = data.length % 3;
  45.             int len = data.length - r;
  46.             int i = 0;
  47.             int c;
  48.             while (i < len) {
  49.                 c = (0x000000ff & data[i++]) << 16 |
  50.                         (0x000000ff & data[i++]) << 8  |
  51.                         (0x000000ff & data[i++]);
  52.                 sb.append(base64EncodeChars[c >> 18]);
  53.                 sb.append(base64EncodeChars[c >> 12 & 0x3f]);
  54.                 sb.append(base64EncodeChars[c >> 6  & 0x3f]);
  55.                 sb.append(base64EncodeChars[c & 0x3f]);
  56.             }
  57.             if (r == 1) {
  58.                 c = 0x000000ff & data[i++];
  59.                 sb.append(base64EncodeChars[c >> 2]);
  60.                 sb.append(base64EncodeChars[(c & 0x03) << 4]);
  61.                 sb.append("==");
  62.             }
  63.             else if (r == 2) {
  64.                 c = (0x000000ff & data[i++]) << 8 |
  65.                         (0x000000ff & data[i++]);
  66.                 sb.append(base64EncodeChars[c >> 10]);
  67.                 sb.append(base64EncodeChars[c >> 4 & 0x3f]);
  68.                 sb.append(base64EncodeChars[(c & 0x0f) << 2]);
  69.                 sb.append("=");
  70.             }
  71.             return sb.toString();
  72.         }
  73.  
  74.         public static final byte[] decode(String str) {
  75.             final byte[] base64DecodeChars = new byte[] {
  76.                     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  77.                     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  78.                     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
  79.                     52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
  80.                     -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
  81.                     15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
  82.                     -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  83.                     41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 };
  84.             byte[] data = str.getBytes();
  85.             int len = data.length;
  86.             ByteArrayOutputStream buf = new ByteArrayOutputStream(len);
  87.             int i = 0;
  88.             int b1, b2, b3, b4;
  89.             while (i < len) {
  90.                 do {
  91.                     b1 = base64DecodeChars[data[i++]];
  92.                 } while (i < len && b1 == -1);
  93.                 if (b1 == -1) {
  94.                     break;
  95.                 }
  96.                 do {
  97.                     b2 = base64DecodeChars[data[i++]];
  98.                 } while (i < len && b2 == -1);
  99.                 if (b2 == -1) {
  100.                     break;
  101.                 }
  102.                 buf.write(((b1 << 2) | ((b2 & 0x30) >>> 4)));
  103.                 do {
  104.                     b3 = data[i++];
  105.                     if (b3 == 61) {
  106.                         return buf.toByteArray();
  107.                     }
  108.                     b3 = base64DecodeChars[b3];
  109.                 } while (i < len && b3 == -1);
  110.                 if (b3 == -1) {
  111.                     break;
  112.                 }
  113.                 buf.write((((b2 & 0x0f) << 4) | ((b3 & 0x3c) >>> 2)));
  114.                 do {
  115.                     b4 = data[i++];
  116.                     if (b4 == 61) {
  117.                         return buf.toByteArray();
  118.                     }
  119.                     b4 = base64DecodeChars[b4];
  120.                 } while (i < len && b4 == -1);
  121.                 if (b4 == -1) {
  122.                     break;
  123.                 }
  124.                 buf.write((((b3 & 0x03) << 6) | b4));
  125.             }
  126.             return buf.toByteArray();
  127.         }
  128.     }
  129.  
  130.  
  131.  
  132.  
  133.  
  134.     private static int MX(int sum, int y, int z, int p, int e, int[] k) {
  135.         return (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
  136.     }
  137.  
  138.  
  139.     private crypt() {}
  140.  
  141.  
  142.     public static final byte[] encrypt(byte[] data, byte[] key) {
  143.         if (data.length == 0) {
  144.             return data;
  145.         }
  146.         return toByteArray(
  147.                 encrypt(toIntArray(data, true), toIntArray(fixKey(key), false)), false);
  148.     }
  149.     public static final byte[] encrypt(String data, byte[] key) {
  150.         try {
  151.             return encrypt(data.getBytes("UTF-8"), key);
  152.         }
  153.         catch (UnsupportedEncodingException e) {
  154.             return null;
  155.         }
  156.     }
  157.     public static final byte[] encrypt(byte[] data, String key) {
  158.         try {
  159.             return encrypt(data, key.getBytes("UTF-8"));
  160.         }
  161.         catch (UnsupportedEncodingException e) {
  162.             return null;
  163.         }
  164.     }
  165.     public static final byte[] encrypt(String data, String key) {
  166.         try {
  167.             return encrypt(data.getBytes("UTF-8"), key.getBytes("UTF-8"));
  168.         }
  169.         catch (UnsupportedEncodingException e) {
  170.             return null;
  171.         }
  172.     }
  173.     public static final String encryptToBase64String(byte[] data, byte[] key) {
  174.         byte[] bytes = encrypt(data, key);
  175.         if (bytes == null) return null;
  176.         return TeaBase64.encode(bytes);
  177.     }
  178.     public static final String encryptToBase64String(String data, byte[] key) {
  179.         byte[] bytes = encrypt(data, key);
  180.         if (bytes == null) return null;
  181.         return TeaBase64.encode(bytes);
  182.     }
  183.     public static final String encryptToBase64String(byte[] data, String key) {
  184.         byte[] bytes = encrypt(data, key);
  185.         if (bytes == null) return null;
  186.         return TeaBase64.encode(bytes);
  187.     }
  188.     public static final String encryptToBase64String(String data, String key) {
  189.         byte[] bytes = encrypt(data, key);
  190.         if (bytes == null) return null;
  191.         return TeaBase64.encode(bytes);
  192.     }
  193.     public static final byte[] decrypt(byte[] data, byte[] key) {
  194.         if (data.length == 0) {
  195.             return data;
  196.         }
  197.         return toByteArray(
  198.                 decrypt(toIntArray(data, false), toIntArray(fixKey(key), false)), true);
  199.     }
  200.     public static final byte[] decrypt(byte[] data, String key) {
  201.         try {
  202.             return decrypt(data, key.getBytes("UTF-8"));
  203.         }
  204.         catch (UnsupportedEncodingException e) {
  205.             return null;
  206.         }
  207.     }
  208.     public static final byte[] decryptBase64String(String data, byte[] key) {
  209.         return decrypt(TeaBase64.decode(data), key);
  210.     }
  211.     public static final byte[] decryptBase64String(String data, String key) {
  212.         return decrypt(TeaBase64.decode(data), key);
  213.     }
  214.     public static final String decryptToString(byte[] data, byte[] key) {
  215.         try {
  216.             byte[] bytes = decrypt(data, key);
  217.             if (bytes == null) return null;
  218.             return new String(bytes, "UTF-8");
  219.         }
  220.         catch (UnsupportedEncodingException ex) {
  221.             return null;
  222.         }
  223.     }
  224.     public static final String decryptToString(byte[] data, String key) {
  225.         try {
  226.             byte[] bytes = decrypt(data, key);
  227.             if (bytes == null) return null;
  228.             return new String(bytes, "UTF-8");
  229.         }
  230.         catch (UnsupportedEncodingException ex) {
  231.             return null;
  232.         }
  233.     }
  234.     public static final String decryptBase64StringToString(String data, byte[] key) {
  235.         try {
  236.             byte[] bytes = decrypt(TeaBase64.decode(data), key);
  237.             if (bytes == null) return null;
  238.             return new String(bytes, "UTF-8");
  239.         }
  240.         catch (UnsupportedEncodingException ex) {
  241.             return null;
  242.         }
  243.     }
  244.     public static final String decryptBase64StringToString(String data, String key) {
  245.         try {
  246.             byte[] bytes = decrypt(TeaBase64.decode(data), key);
  247.             if (bytes == null) return null;
  248.             return new String(bytes, "UTF-8");
  249.         }
  250.         catch (UnsupportedEncodingException ex) {
  251.             return null;
  252.         }
  253.     }
  254.  
  255.  
  256.     private static int[] encrypt(int[] v, int[] k) {
  257.         int n = v.length - 1;
  258.  
  259.  
  260.         if (n < 1) {
  261.             return v;
  262.         }
  263.         int p, q = 6 + 52 / (n + 1);
  264.         int z = v[n], y, sum = 0, e;
  265.  
  266.  
  267.         while (q-- > 0) {
  268.             sum = sum + DELTA;
  269.             e = sum >>> 2 & 3;
  270.             for (p = 0; p < n; p++) {
  271.                 y = v[p + 1];
  272.                 z = v[p] += MX(sum, y, z, p, e, k);
  273.             }
  274.             y = v[0];
  275.             z = v[n] += MX(sum, y, z, p, e, k);
  276.         }
  277.         return v;
  278.     }
  279.  
  280.  
  281.     private static int[] decrypt(int[] v, int[] k) {
  282.         int n = v.length - 1;
  283.  
  284.  
  285.         if (n < 1) {
  286.             return v;
  287.         }
  288.         int p, q = 6 + 52 / (n + 1);
  289.         int z, y = v[0], sum = q * DELTA, e;
  290.  
  291.  
  292.         while (sum != 0) {
  293.             e = sum >>> 2 & 3;
  294.             for (p = n; p > 0; p--) {
  295.                 z = v[p - 1];
  296.                 y = v[p] -= MX(sum, y, z, p, e, k);
  297.             }
  298.             z = v[n];
  299.             y = v[0] -= MX(sum, y, z, p, e, k);
  300.             sum = sum - DELTA;
  301.         }
  302.         return v;
  303.     }
  304.  
  305.  
  306.     private static byte[] fixKey(byte[] key) {
  307.         if (key.length == 16) return key;
  308.         byte[] fixedkey = new byte[16];
  309.         if (key.length < 16) {
  310.             System.arraycopy(key, 0, fixedkey, 0, key.length);
  311.         }
  312.         else {
  313.             System.arraycopy(key, 0, fixedkey, 0, 16);
  314.         }
  315.         return fixedkey;
  316.     }
  317.  
  318.  
  319.     private static int[] toIntArray(byte[] data, boolean includeLength) {
  320.         int n = (((data.length & 3) == 0)
  321.                 ? (data.length >>> 2)
  322.                 : ((data.length >>> 2) + 1));
  323.         int[] result;
  324.  
  325.  
  326.         if (includeLength) {
  327.             result = new int[n + 1];
  328.             result[n] = data.length;
  329.         }
  330.         else {
  331.             result = new int[n];
  332.         }
  333.         n = data.length;
  334.         for (int i = 0; i < n; ++i) {
  335.             result[i >>> 2] |= (0x000000ff & data[i]) << ((i & 3) << 3);
  336.         }
  337.         return result;
  338.     }
  339.  
  340.  
  341.     private static byte[] toByteArray(int[] data, boolean includeLength) {
  342.         int n = data.length << 2;
  343.  
  344.  
  345.         if (includeLength) {
  346.             int m = data[data.length - 1];
  347.             n -= 4;
  348.             if ((m < n - 3) || (m > n)) {
  349.                 return null;
  350.             }
  351.             n = m;
  352.         }
  353.         byte[] result = new byte[n];
  354.  
  355.  
  356.         for (int i = 0; i < n; ++i) {
  357.             result[i] = (byte) (data[i >>> 2] >>> ((i & 3) << 3));
  358.         }
  359.         return result;
  360.     }
  361.  
  362.  
  363. }
Add Comment
Please, Sign In to add comment