import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
public class AES {
public static class Cipher {
public static final int ENCRYPT_MODE = 1;
public static final int DECRYPT_MODE = 2;
public static final String AES_ECB_PKCS7 = "AES/ECB/PKCS7Padding";
private int opmode;
private AESBlock block;
public static Cipher getInstance(String var0) throws NoSuchAlgorithmException, NoSuchPaddingException {
if (AES_ECB_PKCS7.equals(var0)) {
Cipher cipher = new Cipher();
cipher.block = new AESBlock();
return cipher;
}
throw new NoSuchAlgorithmException(var0);
}
public void init(int var1, Key var2) throws InvalidKeyException {
this.opmode = var1;
this.block.init(var1 == DECRYPT_MODE, var2.getEncoded());
}
public byte[] doFinal(byte[] var1) throws IllegalBlockSizeException, BadPaddingException {
int blockSize = this.block.getBlockSize();
if (this.opmode == ENCRYPT_MODE) {
byte[] pad = PKCS7Padding.pad(var1, blockSize);
byte[] dst = new byte[pad.length];
for (int i = 0; i < pad.length; i += blockSize) {
this.block.encryptBlock(pad, i, dst, i);
}
return dst;
} else {
byte[] dst = new byte[var1.length];
for (int i = 0; i < var1.length; i += blockSize) {
this.block.decryptBlock(var1, i, dst, i);
}
return PKCS7Padding.unpad(dst);
}
}
}
public static class PKCS7Padding {
public static byte[] pad(byte[] var1, int blockSize) {
int tail = blockSize - var1.length % blockSize;
byte[] dst = new byte[var1.length + tail];
System.arraycopy(var1, 0, dst, 0, var1.length);
Arrays.fill(dst, var1.length, dst.length, (byte) (tail & 255));
return dst;
}
public static byte[] unpad(byte[] var1) {
int tail = var1[var1.length - 1];
byte[] dst = new byte[var1.length - tail];
System.arraycopy(var1, 0, dst, 0, dst.length);
return dst;
}
}
/**
* Base on openjdk commit 85648e14f25d4448d51db001a4629154ea82b452.
*/
public static final class AESBlock {
private static final int AES_BLOCK_SIZE = 16;
private static final int[] AES_KEYSIZES = new int[]{16, 24, 32};
private static final byte[] S = new byte[256], Si = new byte[256];
private static final int[] T1 = new int[256], T2 = new int[256], T3 = new int[256], T4 = new int[256], T5 = new int[256], T6 = new int[256], T7 = new int[256], T8 = new int[256];
private static final int[] U1 = new int[256], U2 = new int[256], U3 = new int[256], U4 = new int[256];
private static final byte[] rcon = new byte[30];
private static int[] alog = new int[256], log = new int[256];
static {
int ROOT = 0x11B;
int i, j = 0;
alog[0] = 1;
for (i = 1; i < 256; i++) {
j = (alog[i - 1] << 1) ^ alog[i - 1];
if ((j & 0x100) != 0) {
j ^= ROOT;
}
alog[i] = j;
}
for (i = 1; i < 255; i++) {
log[alog[i]] = i;
}
byte[][] A = new byte[][]
{
{1, 1, 1, 1, 1, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 0, 0},
{0, 0, 1, 1, 1, 1, 1, 0},
{0, 0, 0, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 1, 1, 1, 1},
{1, 1, 0, 0, 0, 1, 1, 1},
{1, 1, 1, 0, 0, 0, 1, 1},
{1, 1, 1, 1, 0, 0, 0, 1}
};
byte[] B = new byte[]{0, 1, 1, 0, 0, 0, 1, 1};
int t;
byte[][] box = new byte[256][8];
box[1][7] = 1;
for (i = 2; i < 256; i++) {
j = alog[255 - log[i]];
for (t = 0; t < 8; t++) {
box[i][t] = (byte) ((j >>> (7 - t)) & 0x01);
}
}
byte[][] cox = new byte[256][8];
for (i = 0; i < 256; i++) {
for (t = 0; t < 8; t++) {
cox[i][t] = B[t];
for (j = 0; j < 8; j++) {
cox[i][t] ^= A[t][j] * box[i][j];
}
}
}
for (i = 0; i < 256; i++) {
S[i] = (byte) (cox[i][0] << 7);
for (t = 1; t < 8; t++) {
S[i] ^= cox[i][t] << (7 - t);
}
Si[S[i] & 0xFF] = (byte) i;
}
byte[][] G = new byte[][]{
{2, 1, 1, 3},
{3, 2, 1, 1},
{1, 3, 2, 1},
{1, 1, 3, 2}
};
byte[][] AA = new byte[4][8];
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) AA[i][j] = G[i][j];
AA[i][i + 4] = 1;
}
byte pivot, tmp;
byte[][] iG = new byte[4][4];
for (i = 0; i < 4; i++) {
pivot = AA[i][i];
if (pivot == 0) {
t = i + 1;
while (AA[t][i] == 0) {
t++;
}
for (j = 0; j < 8; j++) {
tmp = AA[i][j];
AA[i][j] = AA[t][j];
AA[t][j] = tmp;
}
pivot = AA[i][i];
}
for (j = 0; j < 8; j++) {
if (AA[i][j] != 0) {
AA[i][j] = (byte)
alog[(255 + log[AA[i][j] & 0xFF] - log[pivot & 0xFF])
% 255