java aes ecb_Java 实现 AES256-ECB-PKCS7Padding(无需修改策略文件)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值