HIT·信息安全数学基础·实验四

实验背景

高級加密标准(Advaneed Encryption Standard, AES)由比利时著名密码学家
Joan Daemen 和 Vincent Rijmen 设计,是美因联邦政府采用的一种分组加密标准,
用来替代之前的 DES 算法,目前为对称密钥加密中最流行的算法之一。
本次实验的目的包括:

  1. 理解和掌握 AES 的概念,以及 AES 在计算机加密通信领域的应用。
  2. 了解 AES 加密解密的流程,将其和 DES 进行对比。
  3. 编码实践 AES-128 加密解密的流程。

实验内容

  1. 字节代替
  2. 列混淆
  3. 行移位
  4. 轮秘钥加

注意事项

这个实验是四次实验里最难的,感觉当时的实验指导书写的也很赶,体验远远不如前几次实验,好多人在实验时直接选择开摆,希望我的代码能够提供一个参考,祝大家实验顺利。

实验代码

AES package

package AES;

import java.math.BigInteger;
import java.util.Random;
import java.util.Scanner;

public abstract class AES {
    private Boolean debug = false;
    private static int[][] s_box = {
            //0     1    2      3     4    5     6     7      8    9     A      B    C     D     E     F
            {0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76},
            {0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0},
            {0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15},
            {0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75},
            {0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84},
            {0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf},
            {0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8},
            {0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2},
            {0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73},
            {0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb},
            {0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79},
            {0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08},
            {0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a},
            {0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e},
            {0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf},
            {0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16}
    };

    private static int[][] inverseS_box = {
            //0     1    2      3     4    5     6     7      8    9     A      B    C     D     E     F
            {0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb},
            {0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb},
            {0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e},
            {0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25},
            {0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92},
            {0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84},
            {0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06},
            {0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b},
            {0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73},
            {0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e},
            {0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b},
            {0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4},
            {0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f},
            {0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef},
            {0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61},
            {0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d}
    };

    private static byte[][] mix_column = {
            {(byte)0x02, (byte)0x03, (byte)0x01, (byte)0x01},
            {(byte)0x01, (byte)0x02, (byte)0x03, (byte)0x01},
            {(byte)0x01, (byte)0x01, (byte)0x02, (byte)0x03},
            {(byte)0x03, (byte)0x01, (byte)0x01, (byte)0x02}
    };

    private static byte[][] inverseMix_column = {
            {(byte)0x0e, (byte)0x0b, (byte)0x0d, (byte)0x09},
            {(byte)0x09, (byte)0x0e, (byte)0x0b, (byte)0x0d},
            {(byte)0x0d, (byte)0x09, (byte)0x0e, (byte)0x0b},
            {(byte)0x0b, (byte)0x0d, (byte)0x09, (byte)0x0e}
    };

    private static byte[][] Rcon = {
            {(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00},
            {(byte)0x01, (byte)0x00, (byte)0x00, (byte)0x00},
            {(byte)0x02, (byte)0x00, (byte)0x00, (byte)0x00},
            {(byte)0x04, (byte)0x00, (byte)0x00, (byte)0x00},
            {(byte)0x08, (byte)0x00, (byte)0x00, (byte)0x00},
            {(byte)0x10, (byte)0x00, (byte)0x00, (byte)0x00},
            {(byte)0x20, (byte)0x00, (byte)0x00, (byte)0x00},
            {(byte)0x40, (byte)0x00, (byte)0x00, (byte)0x00},
            {(byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00},
            {(byte)0x1b, (byte)0x00, (byte)0x00, (byte)0x00},
            {(byte)0x36, (byte)0x00, (byte)0x00, (byte)0x00}
    };


    private static void printBytes(byte[] bytes, int len) {
        System.out.printf("0x");
        for (int i = 0; i < len; i++) {
            System.out.printf("%02X", bytes[i]);
        }
    }

    private static void printBytes(byte[][] bytes, int len0, int len1) {
        System.out.printf("0x");
        for (int i = 0; i < len0; i++) {
            for (int j = 0; j < len1; j++) {
                System.out.printf("%02X", bytes[i][j]);
            }
        }
    }

    private void matrixTranspose_4_4(byte[][] mat) {
        for (int i = 0; i < 4; i++) {
            for (int j = i + 1; j < 4; j++) {
                if (i != j) {
                    byte tmp = mat[i][j];
                    mat[i][j] = mat[j][i];
                    mat[j][i] = tmp;
                }
            }
        }
    }

    // 4*4的byte矩阵在GF(2 ^ 8)上的乘法
    private void matrixMulti_4_4(byte[][] a, byte[][] b, byte[][] result) {
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                result[i][j] = 0;
                for (int k = 0; k < 4; k++) {
                    result[i][j] ^= multiply(a[i][k], b[k][j]);
                }
            }
        }
    }

    private byte multiply(byte a, byte b) {
        byte result = 0;
        byte temp;
        for (int i = 0; i < 8; i++) {
            if ((b & 1) != 0) {
                result ^= a;
            }
            temp = (byte)(a & 0x80);
            a <<= 1;
            if (temp != 0) {
                a ^= 0x1b;
            }
            b >>= 1;
        }
        return result;
    }

    // 轮密钥加
    private byte[][] addKey(byte[][] state, byte[][] roundKey) {
        byte[][] newState = new byte[4][4];
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                newState[i][j] = (byte)(state[i][j] ^ roundKey[i][j]);
            }
        }
        return newState;
    }



    // 字节代替
    private byte[][] byteSub(byte[][] state) {
        byte[][] newState = new byte[4][4];
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                int row = (state[i][j] & 0xf0) >> 4;
                int col = state[i][j] & 0x0f;
                newState[i][j] = (byte)(s_box[row][col]);
            }
        }
        return newState;
    }



    // 行移位
    private byte[][] shiftRow(byte[][] state) {
        byte[][] newState = new byte[4][4];
        // 复制原数组到新数组
        for (int i = 0; i < 4; i++) {
            System.arraycopy(state[i], 0, newState[i], 0, 4);
        }

        // 第二行左移1位
        byte temp = newState[1][0];
        System.arraycopy(newState[1], 1, newState[1], 0, 3);
        newState[1][3] = temp;

        // 第三行左移2位
        temp = newState[2][0];
        newState[2][0] = newState[2][2];
        newState[2][2] = temp;
        temp = newState[2][1];
        newState[2][1] = newState[2][3];
        newState[2][3] = temp;

        // 第四行左移3位
        temp = newState[3][3];
        System.arraycopy(newState[3], 0, newState[3], 1, 3);
        newState[3][0] = temp;

        return newState;
    }



    // 列混淆
    private byte[][] mixColumn(byte[][] state) {
        byte[][] newState = new byte[4][4];
        matrixMulti_4_4(mix_column, state, newState);
        return newState;
    }


    private byte[][] inverseByteSub(byte[][] state) {
        byte[][] afterInverseByteSub = new byte[4][4];
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                int row = (state[i][j] & 0xf0) >> 4;
                int column = state[i][j] & 0x0f;
                afterInverseByteSub[i][j] = (byte)inverseS_box[row][column];
            }
        }
        return afterInverseByteSub;
    }

    private byte[][] inverseShiftRow(byte[][] afterInverseByteSub) {
        byte[][] afterInverseShiftRow = new byte[4][4];
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                afterInverseShiftRow[i][j] = afterInverseByteSub[i][(j + (4 - i)) % 4];
            }
        }
        return afterInverseShiftRow;
    }

    private byte[][] inverseMixColumn(byte[][] afterInverseShiftRow) {
        byte[][] afterInverseMixColumn = new byte[4][4];
        matrixMulti_4_4(inverseMix_column, afterInverseShiftRow, afterInverseMixColumn);
        return afterInverseMixColumn;
    }


//cnmd为什么不对
//    public byte[][][] generateKey(byte[] initialKey, int N) {
//        final int Nk = initialKey.length / 4; // 密钥长度(以32位字为单位)
//        final int Nr = N; // 使用传入的轮数
//        byte[][][] roundKeys = new byte[Nr + 1][4][4]; // 存储所有轮密钥
//
//        // 将初始密钥按列复制到第一轮密钥中
//        for (int i = 0; i < Nk; i++) {
//            roundKeys[0][i % 4][i / 4] = initialKey[i];
//        }
//
//        // 生成后续轮的密钥
//        for (int round = 1; round <= Nr; round++) {
//            byte[] temp = new byte[4];
//
//            // 处理前一个字
//            for (int i = 0; i < 4; i++) {
//                temp[i] = roundKeys[round - 1][i][3];
//            }
//
//            // 字循环(RotWord)和S盒替换
//            byte tmp = temp[0];
//            for (int i = 0; i < 3; i++) {
//                temp[i] = (byte) (s_box[(temp[i + 1] & 0xf0) >> 4][temp[i + 1] & 0x0f] & 0xff);
//            }
//            temp[3] = (byte) (s_box[(tmp & 0xf0) >> 4][tmp & 0x0f] & 0xff);
//
//            // 轮常量异或
//            for (int i = 0; i < 4; i++) {
//                temp[i] ^= Rcon[round][i];
//            }
//
//            // 生成本轮密钥
//            for (int i = 0; i < 4; i++) {
//                for (int j = 0; j < 4; j++) {
//                    if (i == 0) {
//                        roundKeys[round][j][i] = (byte) (roundKeys[round - 1][j][i] ^ temp[j]);
//                    } else {
//                        roundKeys[round][j][i] = (byte) (roundKeys[round - 1][j][i] ^ roundKeys[round][j][i - 1]);
//                    }
//                }
//            }
//        }
//
//        return roundKeys;
//    }
//public byte[][][] generateKey(byte[] initialKey, int N) {
//    final int Nk = initialKey.length / 4; // 密钥长度(以32位字为单位)
//    final int Nr = N; // 使用传入的轮数
//    byte[][][] roundKeys = new byte[Nr + 1][4][4]; // 存储所有轮密钥
//
//    // 将初始密钥按列复制到第一轮密钥中
//    for (int i = 0; i < Nk; i++) {
//        roundKeys[0][i % 4][i / 4] = initialKey[i];
//    }
//
//    // 生成后续轮的密钥
//    for (int round = 1; round <= Nr; round++) {
//        byte[] temp = new byte[4];
//
//        // 处理前一个字
//        for (int i = 0; i < 4; i++) {
//            temp[i] = roundKeys[round - 1][i][3];
//        }
//
//        // 字循环(RotWord)
//        byte tmp = temp[0];
//        for (int i = 0; i < 3; i++) {
//            temp[i] = temp[i + 1];
//        }
//        temp[3] = tmp;
//
//        // S盒替换
//        for (int i = 0; i < 4; i++) {
//            int row = (temp[i] & 0xf0) >> 4;
//            int col = temp[i] & 0x0f;
//            temp[i] = (byte) (s_box[row][col]);
//        }
//
//        // 轮常量异或
//        for (int i = 0; i < 4; i++) {
//            temp[i] ^= Rcon[round][i];
//        }
//
//        // 生成本轮密钥
//        for (int i = 0; i < 4; i++) {
//            for (int j = 0; j < 4; j++) {
//                if (i == 0) {
//                    roundKeys[round][j][i] = (byte) (roundKeys[round - 1][j][i] ^ temp[j]);
//                } else {
//                    roundKeys[round][j][i] = (byte) (roundKeys[round - 1][j][i] ^ roundKeys[round][j][i - 1]);
//                }
//            }
//        }
//    }
//
//    return roundKeys;
//}
    //好好好终于对了
public byte[][][] generateKey(byte[] initialKey, int N) {
    final int Nk = initialKey.length / 4; // 密钥长度(以32位字为单位)
    final int Nr = N; // 使用传入的轮数
    byte[][][] roundKeys = new byte[Nr + 1][4][4]; // 存储所有轮密钥
    //一定是按列复制
    // 将初始密钥按列复制到第一轮密钥中
    for (int i = 0; i < Nk * 4; i++) {
        roundKeys[0][i % 4][i / 4] = initialKey[i];
    }
    // 生成后续轮的密钥
    for (int round = 1; round <= Nr; round++) {
        byte[] temp = new byte[4];

        // 处理前一个字
        for (int i = 0; i < 4; i++) {
            temp[i] = roundKeys[round - 1][i][3];
        }
        // 字循环(RotWord)
        byte tmp = temp[0];
        for (int i = 0; i < 3; i++) {
            temp[i] = temp[i + 1];
        }
        temp[3] = tmp;
        // S盒替换
        for (int i = 0; i < 4; i++) {
            int row = (temp[i] & 0xf0) >> 4;
            int col = temp[i] & 0x0f;
            temp[i] = (byte) (s_box[row][col]);
        }
        // 轮常量异或
        temp[0] ^= Rcon[round][0];
        // 生成本轮密钥
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (i == 0) {
                    roundKeys[round][j][i] = (byte) (roundKeys[round - 1][j][i] ^ temp[j]);
                } else {
                    roundKeys[round][j][i] = (byte) (roundKeys[round - 1][j][i] ^ roundKeys[round][j][i - 1]);
                }
            }
        }
    }
    return roundKeys;
}



    // 加密
    public byte[][] encrypt(byte[][] plaintext, byte[] key, int N) {
        if (debug) {
            System.out.printf("encrypt(");
            printBytes(plaintext, 4, 4);
            System.out.printf(", ");
            printBytes(key, 16);
            System.out.printf(", %d)\n----------------------------------------------\n", N);
        }

        // 生成N轮的轮密钥
        byte[][][] roundKey = generateKey(key, N);

        if (debug) {
            System.out.printf("roundKey:\n");
            for (int i = 0; i < N + 1; i++) {
                System.out.printf("\tround %d: ", i);
                printBytes(roundKey[i], 4, 4);
                System.out.printf("\n");
            }
        }

        byte[][] state = addKey(plaintext, roundKey[0]);

        if (debug) {
            System.out.printf("round 0:\n");
            System.out.printf("\tstate:");
            printBytes(state, 4, 4);
            System.out.printf("\n");
        }

        byte[][] afterByteSub;
        byte[][] afterShiftRow;
        byte[][] afterMixColumn;
        // 进行N-1轮运算
        for (int i = 1; i < N; i++) {
            afterByteSub = byteSub(state);
            afterShiftRow = shiftRow(afterByteSub);
            afterMixColumn = mixColumn(afterShiftRow);
            state = addKey(afterMixColumn, roundKey[i]);

            if (debug) {
                System.out.printf("round %d:\n", i);
                System.out.printf("\tafterByteSub: ");
                printBytes(afterByteSub, 4, 4);
                System.out.printf("\n");
                System.out.printf("\tafterShiftRow: ");
                printBytes(afterShiftRow, 4, 4);
                System.out.printf("\n");
                System.out.printf("\tafterMixColumn: ");
                printBytes(afterMixColumn, 4, 4);
                System.out.printf("\n");
                System.out.printf("\tstate: ");
                printBytes(state, 4, 4);
                System.out.printf("\n");
            }
        }

        // 最后一轮
        afterByteSub = byteSub(state);
        afterShiftRow = shiftRow(afterByteSub);
        byte[][] ciphertext = addKey(afterShiftRow, roundKey[N]);

        if (debug) {
            System.out.printf("round %d:\n", N);
            System.out.printf("\tafterByteSub: ");
            printBytes(afterByteSub, 4, 4);
            System.out.printf("\n");
            System.out.printf("\tafterShiftRow: ");
            printBytes(afterShiftRow, 4, 4);
            System.out.printf("\n");
            System.out.printf("\tciphertext: ");
            printBytes(ciphertext, 4, 4);
            System.out.printf("\n");
            System.out.printf("----------------------------------------------\n", N);
        }

        return ciphertext;
    }

    // 解密
    public byte[][] decrypt(byte[][] ciphertext, byte[] key, int N) {
        if (debug) {
            System.out.printf("decrypt(");
            printBytes(ciphertext, 4, 4);
            System.out.printf(", ");
            printBytes(key, 16);
            System.out.printf(", %d)\n----------------------------------------------\n", N);
        }

        // 生成N轮的轮密钥
        byte[][][] roundKey = generateKey(key, N);

        if (debug) {
            System.out.printf("roundKey:\n");
            for (int i = 0; i < N + 1; i++) {
                System.out.printf("\tround %d: ", i);
                printBytes(roundKey[i], 4, 4);
                System.out.printf("\n");
            }
        }

        byte[][] state = addKey(ciphertext, roundKey[N]);

        if (debug) {
            System.out.printf("round 0:\n");
            System.out.printf("\tstate:");
            printBytes(state, 4, 4);
            System.out.printf("\n");
        }

        byte[][] afterByteSub;
        byte[][] afterShiftRow;
        byte[][] afterMixColumn = null;
        // 进行N-1轮运算
        for (int i = N - 1; i > 0; i--) {
            afterShiftRow = inverseShiftRow(state);
            afterByteSub = inverseByteSub(afterShiftRow);
            state = addKey(afterByteSub, roundKey[i]);
            afterMixColumn = inverseMixColumn(state);

            if (debug) {
                System.out.printf("round %d:\n", N - i);
                System.out.printf("\tafterShiftRow: ");
                printBytes(afterShiftRow, 4, 4);
                System.out.printf("\n");
                System.out.printf("\tafterByteSub: ");
                printBytes(afterByteSub, 4, 4);
                System.out.printf("\n");
                System.out.printf("\tstate: ");
                printBytes(state, 4, 4);
                System.out.printf("\n");
                System.out.printf("\tafterMixColumn: ");
                printBytes(afterMixColumn, 4, 4);
                System.out.printf("\n");
            }

            state = afterMixColumn;
        }

        // 最后一轮
        afterShiftRow = inverseShiftRow(afterMixColumn);
        afterByteSub = inverseByteSub(afterShiftRow);
        byte[][] plaintext = addKey(afterByteSub, roundKey[0]);

        if (debug) {
            System.out.printf("round %d:\n", N);
            System.out.printf("\tafterShiftRow: ");
            printBytes(afterShiftRow, 4, 4);
            System.out.printf("\n");
            System.out.printf("\tafterByteSub: ");
            printBytes(afterByteSub, 4, 4);
            System.out.printf("\n");
            System.out.printf("\tplaintext: ");
            printBytes(plaintext, 4, 4);
            System.out.printf("\n");
            System.out.printf("----------------------------------------------\n", N);
        }

        return plaintext;
    }

    private static Boolean compareBytes(byte[] bytes0, byte[] bytes1, int len) {
        for (int i = 0; i < len; i++) {
            if (bytes0[i] != bytes1[i]) {
                return false;
            }
        }
        return true;
    }

    private static Boolean compareBytes(byte[][] bytes0, byte[][] bytes1, int len0, int len1) {
        for (int i = 0; i < len0; i++) {
            for (int j = 0; j < len1; j++) {
                if (bytes0[i][j] != bytes1[i][j]) {
                    return false;
                }
            }
        }
        return true;
    }

    private Boolean test(int index, byte[] key, byte[][] plaintext, byte[][] ciphertext) {
        Boolean res = true;
        System.out.printf("Test %d\n====================================================================\n", index);
        System.out.printf("Key:\n");
        printBytes(key, 16);
        System.out.printf("\nPlain Text:\n");
        matrixTranspose_4_4(plaintext);
        printBytes(plaintext, 4, 4);
        matrixTranspose_4_4(plaintext);
        System.out.printf("\nCipher Text:\n");
        matrixTranspose_4_4(ciphertext);
        printBytes(ciphertext, 4, 4);
        matrixTranspose_4_4(ciphertext);
        System.out.printf("\n\n");
        byte[][] encryptedText = encrypt(plaintext, key, 10);
        byte[][] decryptedText = decrypt(ciphertext, key, 10);
        if (compareBytes(encryptedText, ciphertext, 4, 4) == false) {
            System.out.printf("encrypt failed, your ciphertext after ecrypting is:\n");
            printBytes(encryptedText, 4, 4);
            System.out.printf("\n");
            res = false;
        }
        else
        {
            System.out.printf("encrypt success!\n");
        }
        if (compareBytes(decryptedText, plaintext, 4, 4) == false) {
            System.out.printf("decrypt failed, your ciphertext after ecrypting is:\n");
            printBytes(decryptedText, 4, 4);
            System.out.printf("\n");
            res = false;
        }
        else
        {
            System.out.printf("decrypt success!\n");
        }
        System.out.printf("\n\n\n");
        return res;
    }

    public static void main(String[] args) {
        AES aes = new AES() {};
        aes.debug = true;

        // 测试0
        byte[] key0 = {(byte)0x0f, (byte)0x15, (byte)0x71, (byte)0xc9, (byte)0x47, (byte)0xd9, (byte)0xe8, (byte)0x59, (byte)0x0c, (byte)0xb7, (byte)0xad, (byte)0xd6, (byte)0xaf, (byte)0x7f, (byte)0x67, (byte)0x98};
        byte[][] plaintext0 = {
                {(byte)0x01, (byte)0x89, (byte)0xfe, (byte)0x76},
                {(byte)0x23, (byte)0xab, (byte)0xdc, (byte)0x54},
                {(byte)0x45, (byte)0xcd, (byte)0xba, (byte)0x32},
                {(byte)0x67, (byte)0xef, (byte)0x98, (byte)0x10}
        };
        byte[][] ciphertext0 = {
                {(byte)0xff, (byte)0x08, (byte)0x69, (byte)0x64},
                {(byte)0x0b, (byte)0x53, (byte)0x34, (byte)0x14},
                {(byte)0x84, (byte)0xbf, (byte)0xab, (byte)0x8f},
                {(byte)0x4a, (byte)0x7c, (byte)0x43, (byte)0xb9}
        };
        aes.test(0, key0, plaintext0, ciphertext0);

        // 测试1
        byte[] key1 = {(byte)0x34, (byte)0x75, (byte)0xbd, (byte)0x76, (byte)0xfa, (byte)0x04, (byte)0x0b, (byte)0x73, (byte)0xf5, (byte)0x21, (byte)0xff, (byte)0xcd, (byte)0x9d, (byte)0xe9, (byte)0x3f, (byte)0x24};
        byte[][] plaintext1 = {
                {(byte)0x1b, (byte)0x1b, (byte)0x80, (byte)0x04},
                {(byte)0x5e, (byte)0xc7, (byte)0x64, (byte)0x83},
                {(byte)0x8b, (byte)0x8d, (byte)0x82, (byte)0x0c},
                {(byte)0x0f, (byte)0x23, (byte)0x67, (byte)0xdb}
        };
        byte[][] ciphertext1 = {
                {(byte)0xf3, (byte)0xdd, (byte)0xd4, (byte)0xe6},
                {(byte)0x85, (byte)0xf4, (byte)0x2c, (byte)0x86},
                {(byte)0x52, (byte)0x01, (byte)0x80, (byte)0xc6},
                {(byte)0x16, (byte)0xd4, (byte)0x02, (byte)0xe7}
        };
        aes.test(1, key1, plaintext1, ciphertext1);

        // 测试2
        byte[] key2 = {(byte)0x2b, (byte)0x24, (byte)0x42, (byte)0x4b, (byte)0x9f, (byte)0xed, (byte)0x59, (byte)0x66, (byte)0x59, (byte)0x84, (byte)0x2a, (byte)0x4d, (byte)0x0b, (byte)0x00, (byte)0x7c, (byte)0x61};
        byte[][] plaintext2 = {
                {(byte)0x41, (byte)0x59, (byte)0xcd, (byte)0xda},
                {(byte)0xb2, (byte)0x05, (byte)0x69, (byte)0xee},
                {(byte)0x67, (byte)0xf0, (byte)0x1b, (byte)0x14},
                {(byte)0xbc, (byte)0xa3, (byte)0x3d, (byte)0x9d}
        };
        byte[][] ciphertext2 = {
                {(byte)0xfb, (byte)0x02, (byte)0xed, (byte)0x72},
                {(byte)0xa4, (byte)0x0f, (byte)0x28, (byte)0x86},
                {(byte)0xec, (byte)0x15, (byte)0xb4, (byte)0xd2},
                {(byte)0x67, (byte)0x73, (byte)0x7d, (byte)0x98}
        };
        aes.test(2, key2, plaintext2, ciphertext2);
    }
}

用到的有限域算法

package FiniteField;


import java.math.BigInteger;
import java.util.Random;
import java.util.Scanner;

public abstract class FiniteField {
    //GF(2)上,加减法等价,都是异或运算
    public int addorMinus(int a1, int a2) {
        return a1 ^ a2;
    }


    public static int multiply(int a1, int a2, int poly) {
        int result = 0;
        for (int i = 0; i < 8; i++) {
            // Step 3: If the lowest bit of a2 is 1, xor a1 with result
            if ((a2 & 1) != 0) {
                result ^= a1;
            }
            // Left shift a1
            a1 <<= 1;
            // Step 4: If the highest bit of a1 (now 9th bit) is 1, xor a1 with poly
            if ((a1 & 0x100) != 0) {
                a1 ^= poly;
            }
            // Ensure a1 stays within 8 bits
            a1 &= 0xFF;
            // Step 5: Right shift a2
            a2 >>= 1;
        }
        return result;
    }


    public int divide(int a1, int a2) {
        int ans = 0;
        while (bitLength(a1) >= bitLength(a2)) {
            int rec = bitLength(a1) - bitLength(a2);
            a1 ^= (a2 << rec);
            ans ^= (1 << rec);
        }
        return ans;
    }

    // 辅助函数,计算数字的二进制长度
    private int bitLength(int a) {
        int length = 0;
        while (a != 0) {
            length++;
            a >>= 1;
        }
        return length;
    }


    public int euclid(int a1, int a2) {
        while (a2 != 0) {
            // 使用多项式除法计算余数
            int temp = mod(a1, a2);
            a1 = a2;
            a2 = temp;
        }
        return a1;
    }

    // 辅助函数,计算多项式除法的余数
    private int mod(int a, int b) {
        if (bitLength(a) < bitLength(b)) {
            return a;
        }
        int div = divide(a, b);
        return a ^ multiply(div, b, 0x11b); // a - (div * b) 在 GF(2^n) 中
    }


    //    public int inverse(int a, int m) {
//        int b = m, x0 = 1, x1 = 0, temp;
//
//        while (a > 1 && b != 0) {
//            int q = divide(a, b);
//            int r = mod(a, b); // 多项式取余
//            a = b;
//            b = r;
//
//            temp = x1;
//            x1 = addorMinus(x0, multiply(q, x1, m)); // GF(2^n)中的减法实际上是加法
//            x0 = temp;
//        }
//
//        if (a != 1) { // 如果a不等于1,说明没有逆元
//            return 0;
//        }
//
//        return x0;
//    }
    public int inverse(int a, int m) {
        int b = m, x0 = 1, x1 = 0, temp;

        while (a != 0) {
            while ((a & 1) == 0) {
                a >>= 1;
                x0 = (x0 & 1) != 0 ? addorMinus(x0, m) >> 1 : x0 >> 1;
            }

            while ((b & 1) == 0) {
                b >>= 1;
                x1 = (x1 & 1) != 0 ? addorMinus(x1, m) >> 1 : x1 >> 1;
            }

            if (a >= b) {
                a = addorMinus(a, b);
                x0 = addorMinus(x0, x1);
            } else {
                b = addorMinus(b, a);
                x1 = addorMinus(x1, x0);
            }
        }

        if (b != 1) { // 如果b不等于1,说明没有逆元
            return 0;
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值