jhoel99

MilitaryGradeEncrypt.java

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