用 java 实现 AES,经过两天的时间,终于把 AES 的 java 写出来了,虽然冗余部分很多,因为本人懒得用循环结构了。。。。。。。。不多说,下面是代码。
- /** * Created by samsung on 2017/3/27. */
- public class AES_vc { //定义S盒 static short[] sbox = { 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 }; //定义逆S盒 char isbox[] = { 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 }; //定义一个异或轮常数矩阵,在密钥扩展时使用 static short [][] rcon = { {0x00, 0x00, 0x00, 0x00}, {0x01, 0x00, 0x00, 0x00}, {0x02, 0x00, 0x00, 0x00}, {0x04, 0x00, 0x00, 0x00}, {0x08, 0x00, 0x00, 0x00}, {0x10, 0x00, 0x00, 0x00}, {0x20, 0x00, 0x00, 0x00}, {0x40, 0x00, 0x00, 0x00}, {0x80, 0x00, 0x00, 0x00}, {0x1b, 0x00, 0x00, 0x00}, {0x36, 0x00, 0x00, 0x00} }; //定义一个明文矩阵 static short[] mingwen = { 0x11, 0x55, 0x99, 0xcc, 0x22, 0x66, 0x00, 0xdd, 0x33, 0x77, 0xaa, 0xee, 0x44, 0x88, 0xbb, 0xff }; //定义初始密钥 static short[] key0= { 0x13,0x02,0x12,0x90, 0x57,0x46,0x34,0xAB, 0x9B,0x8A,0x56,0xCD, 0xDF,0xCE,0x78,0xEF }; //定义一个printkey函数 public static void printkey0(){ for (int i=0;i<16;i++){ if(i%4==3){ System.out.print(tentohex(key0[i])+" "); System.out.println(); } else { System.out.print(tentohex(key0[i])+" "); } } } //定义密钥调度表所需的10个key组 /* public static void creat(){ short[] key1; short[] key2; short[] key3; short[] key4; short[] key5; short[] key6; short[] key7; short[] key8; short[] key9; short[] key10; }*/ //定义密钥扩展------------------------------------------------------------------------------------- static short[] key1 = new short[16]; static short[] key2 = new short[16]; static short[] key3 = new short[16]; static short[] key4 = new short[16]; static short[] key5 = new short[16]; static short[] key6 = new short[16]; static short[] key7 = new short[16]; static short[] key8 = new short[16]; static short[] key9 = new short[16]; static short[] key10 = new short[16]; //定义一个临时数组来存放正在处理的列 static short[] temp; //定义一个交换函数用于rotword中 public static void swapkey(int a,int b){ short tempkey; tempkey=temp[a]; temp[a]=temp[b]; temp[b]=tempkey; } //定义rotwords public static void rotwords(){ swapkey(0,1); swapkey(1,2); swapkey(2,3); } //定义subwords public static void subwords(){ for (int i=0;i<4;i++){ int s=0; s= ((temp[i]>>4)*16+(temp[i]&0x0f));//利用s来记录明文【i】所对应的S盒【s】的位置 System.out.println("第"+i+"个位置对应S盒的第"+s+"个位置"); temp[i]=sbox[s]; } } //定义密钥扩展keyexpansion public static void keyexpansion(){ //第一轮 //把第四列的值存进临时数组temp中 temp[0]=key0[3]; temp[1]=key0[7]; temp[2]=key0[11]; temp[3]=key0[15]; //rotwords,subwords rotwords(); subwords(); //前后两组再加上RCON常数异或 temp[0]= (short) (temp[0]^key0[0]^rcon[1][0]); temp[1]= (short) (temp[1]^key0[4]^rcon[1][1]); temp[2]= (short) (temp[2]^key0[8]^rcon[1][2]); temp[3]= (short) (temp[3]^key0[12]^rcon[1][3]); //把经过rotwords/subwords的变量存进下一个key数组 key1[0]=temp[0]; key1[4]=temp[1]; key1[8]=temp[2]; key1[12]=temp[3]; //异或得到第一组第二列 key1[1]= (short) (key1[0]^key0[1]); key1[5]= (short) (key1[4]^key0[5]); key1[9]= (short) (key1[8]^key0[9]); key1[13]= (short) (key1[12]^key0[13]); //异或得到第一组第三列 key1[2]= (short) (key1[1]^key0[2]); key1[6]= (short) (key1[5]^key0[6]); key1[10]= (short) (key1[9]^key0[10]); key1[14]= (short) (key1[13]^key0[14]); //异或得到第一组第四列 key1[3]= (short) (key1[2]^key0[3]); key1[7]= (short) (key1[6]^key0[7]); key1[11]= (short) (key1[10]^key0[11]); key1[15]= (short) (key1[14]^key0[15]); //第二轮 //把第四列的值存进临时数组temp中 temp[0]=key1[3]; temp[1]=key1[7]; temp[2]=key1[11]; temp[3]=key1[15]; //rotwords,subwords rotwords(); subwords(); //前后两组再加上RCON常数异或 temp[0]= (short) (temp[0]^key1[0]^rcon[2][0]); temp[1]= (short) (temp[1]^key1[4]^rcon[2][1]); temp[2]= (short) (temp[2]^key1[8]^rcon[2][2]); temp[3]= (short) (temp[3]^key1[12]^rcon[2][3]); //把经过rotwords/subwords的变量存进下一个key数组 key2[0]=temp[0]; key2[4]=temp[1]; key2[8]=temp[2]; key2[12]=temp[3]; //异或得到第二组第二列 key2[1]= (short) (key2[0]^key1[1]); key2[5]= (short) (key2[4]^key1[5]); key2[9]= (short) (key2[8]^key1[9]); key2[13]= (short) (key2[12]^key1[13]); //异或得到第二组第三列 key2[2]= (short) (key2[1]^key1[2]); key2[6]= (short) (key2[5]^key1[6]); key2[10]= (short) (key2[9]^key1[10]); key2[14]= (short) (key2[13]^key1[14]); //异或得到第二组第四列 key2[3]= (short) (key2[2]^key1[3]); key2[7]= (short) (key2[6]^key1[7]); key2[11]= (short) (key2[10]^key1[11]); key2[15]= (short) (key2[14]^key1[15]); //第三轮 //把第四列的值存进临时数组temp中 temp[0]=key2[3]; temp[1]=key2[7]; temp[2]=key2[11]; temp[3]=key2[15]; //rotwords,subwords rotwords(); subwords(); //前后两组再加上RCON常数异或 temp[0]= (short) (temp[0]^key2[0]^rcon[3][0]); temp[1]= (short) (temp[1]^key2[4]^rcon[3][1]); temp[2]= (short) (temp[2]^key2[8]^rcon[3][2]); temp[3]= (short) (temp[3]^key2[12]^rcon[3][3]); //把经过rotwords/subwords的变量存进下一个key数组 key3[0]=temp[0]; key3[4]=temp[1]; key3[8]=temp[2]; key3[12]=temp[3]; //异或得到第三组第二列 key3[1]= (short) (key2[0]^key2[1]); key3[5]= (short) (key2[4]^key2[5]); key3[9]= (short) (key2[8]^key2[9]); key3[13]= (short) (key2[12]^key2[13]); //异或得到第三组第三列 key3[2]= (short) (key2[1]^key2[2]); key3[6]= (short) (key2[5]^key2[6]); key3[10]= (short) (key2[9]^key2[10]); key3[14]= (short) (key2[13]^key2[14]); //异或得到第三组第四列 key3[3]= (short) (key2[2]^key2[3]); key3[7]= (short) (key2[6]^key2[7]); key3[11]= (short) (key2[10]^key2[11]); key3[15]= (short) (key2[14]^key2[15]); //第四轮 //把第四列的值存进临时数组temp中 temp[0]=key3[3]; temp[1]=key3[7]; temp[2]=key3[11]; temp[3]=key3[15]; //rotwords,subwords rotwords(); subwords(); //前后两组再加上RCON常数异或 temp[0]= (short) (temp[0]^key3[0]^rcon[4][0]); temp[1]= (short) (temp[1]^key3[4]^rcon[4][1]); temp[2]= (short) (temp[2]^key3[8]^rcon[4][2]); temp[3]= (short) (temp[3]^key3[12]^rcon[4][3]); //把经过rotwords/subwords的变量存进下一个key数组 key4[0]=temp[0]; key4[4]=temp[1]; key4[8]=temp[2]; key4[12]=temp[3]; //异或得到第四组第二列 key4[1]= (short) (key4[0]^key3[1]); key4[5]= (short) (key4[4]^key3[5]); key4[9]= (short) (key4[8]^key3[9]); key4[13]= (short) (key4[12]^key3[13]); //异或得到第四组第三列 key4[2]= (short) (key4[1]^key3[2]); key4[6]= (short) (key4[5]^key3[6]); key4[10]= (short) (key4[9]^key3[10]); key4[14]= (short) (key4[13]^key3[14]); //异或得到第四组第四列 key4[3]= (short) (key4[2]^key3[3]); key4[7]= (short) (key4[6]^key3[7]); key4[11]= (short) (key4[10]^key3[11]); key4[15]= (short) (key4[14]^key3[15]); //第五轮 //把第四列的值存进临时数组temp中 temp[0]=key4[3]; temp[1]=key4[7]; temp[2]=key4[11]; temp[3]=key4[15]; //rotwords,subwords rotwords(); subwords(); //前后两组再加上RCON常数异或 temp[0]= (short) (temp[0]^key4[0]^rcon[5][0]); temp[1]= (short) (temp[1]^key4[4]^rcon[5][1]); temp[2]= (short) (temp[2]^key4[8]^rcon[5][2]); temp[3]= (short) (temp[3]^key4[12]^rcon[5][3]); //把经过rotwords/subwords的变量存进下一个key数组 key5[0]=temp[0]; key5[4]=temp[1]; key5[8]=temp[2]; key5[12]=temp[3]; //异或得到第5组第二列 key5[1]= (short) (key5[0]^key4[1]); key5[5]= (short) (key5[4]^key4[5]); key5[9]= (short) (key5[8]^key4[9]); key5[13]= (short) (key5[12]^key4[13]); //异或得到第5组第三列 key5[2]= (short) (key5[1]^key4[2]); key5[6]= (short) (key5[5]^key4[6]); key5[10]= (short) (key5[9]^key4[10]); key5[14]= (short) (key5[13]^key4[14]); //异或得到第5组第四列 key5[3]= (short) (key5[2]^key4[3]); key5[7]= (short) (key5[6]^key4[7]); key5[11]= (short) (key5[10]^key4[11]); key5[15]= (short) (key5[14]^key4[15]); //第六轮 //把第四列的值存进临时数组temp中 temp[0]=key5[3]; temp[1]=key5[7]; temp[2]=key5[11]; temp[3]=key5[15]; //rotwords,subwords rotwords(); subwords(); //前后两组再加上RCON常数异或 temp[0]= (short) (temp[0]^key5[0]^rcon[6][0]); temp[1]= (short) (temp[1]^key5[4]^rcon[6][1]); temp[2]= (short) (temp[2]^key5[8]^rcon[6][2]); temp[3]= (short) (temp[3]^key5[12]^rcon[6][3]); //把经过rotwords/subwords的变量存进下一个key数组 key6[0]=temp[0]; key6[4]=temp[1]; key6[8]=temp[2]; key6[12]=temp[3]; //异或得到第6组第二列 key6[1]= (short) (key6[0]^key5[1]); key6[5]= (short) (key6[4]^key5[5]); key6[9]= (short) (key6[8]^key5[9]); key6[13]= (short) (key6[12]^key5[13]); //异或得到第6组第三列 key6[2]= (short) (key6[1]^key5[2]); key6[6]= (short) (key6[5]^key5[6]); key6[10]= (short) (key6[9]^key5[10]); key6[14]= (short) (key6[13]^key5[14]); //异或得到第6组第四列 key6[3]= (short) (key6[2]^key5[3]); key6[7]= (short) (key6[6]^key5[7]); key6[11]= (short) (key6[10]^key5[11]); key6[15]= (short) (key6[14]^key5[15]); //第七轮 //把第四列的值存进临时数组temp中 temp[0]=key6[3]; temp[1]=key6[7]; temp[2]=key6[11]; temp[3]=key6[15]; //rotwords,subwords rotwords(); subwords(); //前后两组再加上RCON常数异或 temp[0]= (short) (temp[0]^key6[0]^rcon[7][0]); temp[1]= (short) (temp[1]^key6[4]^rcon[7][1]); temp[2]= (short) (temp[2]^key6[8]^rcon[7][2]); temp[3]= (short) (temp[3]^key6[12]^rcon[7][3]); //把经过rotwords/subwords的变量存进下一个key数组 key7[0]=temp[0]; key7[4]=temp[1]; key7[8]=temp[2]; key7[12]=temp[3]; //异或得到第7组第二列 key7[1]= (short) (key7[0]^key6[1]); key7[5]= (short) (key7[4]^key6[5]); key7[9]= (short) (key7[8]^key6[9]); key7[13]= (short) (key7[12]^key6[13]); //异或得到第7组第三列 key7[2]= (short) (key7[1]^key6[2]); key7[6]= (short) (key7[5]^key6[6]); key7[10]= (short) (key7[9]^key6[10]); key7[14]= (short) (key7[13]^key6[14]); //异或得到第7组第四列 key7[3]= (short) (key7[2]^key6[3]); key7[7]= (short) (key7[6]^key6[7]); key7[11]= (short) (key7[10]^key6[11]); key7[15]= (short) (key7[14]^key6[15]); //第八轮 //把第四列的值存进临时数组temp中 temp[0]=key7[3]; temp[1]=key7[7]; temp[2]=key7[11]; temp[3]=key7[15]; //rotwords,subwords rotwords(); subwords(); //前后两组再加上RCON常数异或 temp[0]= (short) (temp[0]^key7[0]^rcon[8][0]); temp[1]= (short) (temp[1]^key7[4]^rcon[8][1]); temp[2]= (short) (temp[2]^key7[8]^rcon[8][2]); temp[3]= (short) (temp[3]^key7[12]^rcon[8][3]); //把经过rotwords/subwords的变量存进下一个key数组 key8[0]=temp[0]; key8[4]=temp[1]; key8[8]=temp[2]; key8[12]=temp[3]; //异或得到第8组第二列 key8[1]= (short) (key8[0]^key7[1]); key8[5]= (short) (key8[4]^key7[5]); key8[9]= (short) (key8[8]^key7[9]); key8[13]= (short) (key8[12]^key7[13]); //异或得到第8组第三列 key8[2]= (short) (key8[1]^key7[2]); key8[6]= (short) (key8[5]^key7[6]); key8[10]= (short) (key8[9]^key7[10]); key8[14]= (short) (key8[13]^key7[14]); //异或得到第8组第四列 key8[3]= (short) (key8[2]^key7[3]); key8[7]= (short) (key8[6]^key7[7]); key8[11]= (short) (key8[10]^key7[11]); key8[15]= (short) (key8[14]^key7[15]); //第九轮 //把第四列的值存进临时数组temp中 temp[0]=key8[3]; temp[1]=key8[7]; temp[2]=key8[11]; temp[3]=key8[15]; //rotwords,subwords rotwords(); subwords(); //前后两组再加上RCON常数异或 temp[0]= (short) (temp[0]^key8[0]^rcon[9][0]); temp[1]= (short) (temp[1]^key8[4]^rcon[9][1]); temp[2]= (short) (temp[2]^key8[8]^rcon[9][2]); temp[3]= (short) (temp[3]^key8[12]^rcon[9][3]); //把经过rotwords/subwords的变量存进下一个key数组 key9[0]=temp[0]; key9[4]=temp[1]; key9[8]=temp[2]; key9[12]=temp[3]; //异或得到第9组第二列 key9[1]= (short) (key9[0]^key8[1]); key9[5]= (short) (key9[4]^key8[5]); key9[9]= (short) (key9[8]^key8[9]); key9[13]= (short) (key9[12]^key8[13]); //异或得到第9组第三列 key9[2]= (short) (key9[1]^key8[2]); key9[6]= (short) (key9[5]^key8[6]); key9[10]= (short) (key9[9]^key8[10]); key9[14]= (short) (key9[13]^key8[14]); //异或得到第9组第四列 key9[3]= (short) (key9[2]^key8[3]); key9[7]= (short) (key9[6]^key8[7]); key9[11]= (short) (key9[10]^key8[11]); key9[15]= (short) (key9[14]^key8[15]); //第十轮 //把第四列的值存进临时数组temp中 temp[0]=key9[3]; temp[1]=key9[7]; temp[2]=key9[11]; temp[3]=key9[15]; //rotwords,subwords rotwords(); subwords(); //前后两组再加上RCON常数异或 temp[0]= (short) (temp[0]^key9[0]^rcon[10][0]); temp[1]= (short) (temp[1]^key9[4]^rcon[10][1]); temp[2]= (short) (temp[2]^key9[8]^rcon[10][2]); temp[3]= (short) (temp[3]^key9[12]^rcon[10][3]); //把经过rotwords/subwords的变量存进下一个key数组 key10[0]=temp[0]; key10[4]=temp[1]; key10[8]=temp[2]; key10[12]=temp[3]; //异或得到第10组第二列 key10[1]= (short) (key10[0]^key9[1]); key10[5]= (short) (key10[4]^key9[5]); key10[9]= (short) (key10[8]^key9[9]); key10[13]= (short) (key10[12]^key9[13]); //异或得到第10组第三列 key10[2]= (short) (key10[1]^key9[2]); key10[6]= (short) (key10[5]^key9[6]); key10[10]= (short) (key10[9]^key9[10]); key10[14]= (short) (key10[13]^key9[14]); //异或得到第10组第四列 key10[3]= (short) (key10[2]^key9[3]); key10[7]= (short) (key10[6]^key9[7]); key10[11]= (short) (key10[10]^key9[11]); key10[15]= (short) (key10[14]^key9[15]); }//---------------------------------------------------------------------------------------------------------- //定义十进制转换成16进制函数 public static String tentohex(short s){ return Integer.toHexString(s); } //定义打印函数,把数组输出 public static void print(){ for (int i=0;i<16;i++){ if(i%4==3){ System.out.print(tentohex(mingwen[i])+" "); System.out.println(); } else { System.out.print(tentohex(mingwen[i])+" "); } } } //定义字节替换函数subbytes,因为是一维数组,利用高位×16+低位正好等于S盒中的位置------------------- public static void subbytes(){ // System.out.println(mingwen[1]>>4) ; //System.out.println(mingwen[1]&0x0f) ;对位运算符的调试 // System.out.println((mingwen[0]>>4)*16+(mingwen[0]&0x0f)); for (int i=0;i<16;i++){ int s=0; s= ((mingwen[i]>>4)*16+(mingwen[i]&0x0f));//利用s来记录明文【i】所对应的S盒【s】的位置 // System.out.println("第"+i+"个位置对应S盒的第"+s+"个位置"); mingwen[i]=sbox[s]; } } //------------------------------------------------------------------------------------------------ //定义行位移函数------------------------------------------------------------------------------- //定义一个交换函数用于交换 public static void swap(int a,int b){ short temp; temp=mingwen[a]; mingwen[a]=mingwen[b]; mingwen[b]=temp; } //定义行位移函数shiftrows, public static void shiftrows(){ for (int i=0;i<4;i++){ //实现左移一位 if (i==1){ swap(4,5); swap(5,6); swap(6,7); } //实现左移两位 if (i==2){ swap(8,10); swap(9,11); } //实现左移三位 if (i==3){ swap(12,13); swap(12,14); swap(12,15); } } }//--------------------------------------------------------------------------------------------------//定义列混合函数----------------------------------------------------------------------------------- //定义一个×01的函数, public static short cheng01(int a){ return mingwen[a]; } //定义一个×02函数, public static short cheng02(int a){ short b = 0; //如果该数的二进制最高位为一,即高位大于7则 if((mingwen[a]>>4)>7){ b= (short) ((mingwen[a]<<1)^0x011b); } //如果高位为0 if ((mingwen[a]>>4)<8){ b= (short) (mingwen[a]<<1); } return b; } //定义一个×03的函数 public static short cheng03(int a){ return (short) (cheng01(a)^cheng02(a)); } public static void mixcolumns(){ mingwen[0]= (short) (cheng02(0)^cheng03(4)^cheng01(8)^cheng01(12)); mingwen[4]=(short)(cheng01(0)^cheng02(4)^cheng03(8)^cheng01(12)); mingwen[8]= (short) (cheng01(0)^cheng01(4)^cheng02(8)^cheng03(12)); mingwen[12]= (short) (cheng03(0)^cheng01(4)^cheng01(8)^cheng02(12)); mingwen[1]= (short) (cheng02(1)^cheng03(5)^cheng01(9)^cheng01(13)); mingwen[5]=(short)(cheng01(1)^cheng02(5)^cheng03(9)^cheng01(13)); mingwen[9]= (short) (cheng01(1)^cheng01(5)^cheng02(9)^cheng03(13)); mingwen[13]= (short) (cheng03(1)^cheng01(5)^cheng01(9)^cheng02(13)); mingwen[2]= (short) (cheng02(2)^cheng03(6)^cheng01(10)^cheng01(14)); mingwen[6]=(short)(cheng01(2)^cheng02(6)^cheng03(10)^cheng01(14)); mingwen[10]= (short) (cheng01(2)^cheng01(6)^cheng02(10)^cheng03(14)); mingwen[14]= (short) (cheng03(2)^cheng01(6)^cheng01(10)^cheng02(14)); mingwen[3]= (short) (cheng02(3)^cheng03(7)^cheng01(11)^cheng01(15)); mingwen[7]=(short)(cheng01(3)^cheng02(7)^cheng03(11)^cheng01(15)); mingwen[11]= (short) (cheng01(3)^cheng01(7)^cheng02(11)^cheng03(15)); mingwen[15]= (short) (cheng03(3)^cheng01(7)^cheng01(11)^cheng02(15)); } //------------------------------------------------------------------------------------------ //定义轮密钥加函数----------------------------------------------------------------- public static void addroundkey0(int a){ mingwen[a]= (short) (mingwen[a]^key0[a]); } public static void addroundkey1(int a){ mingwen[a]= (short) (mingwen[a]^key1[a]); } public static void addroundkey2(int a){ mingwen[a]= (short) (mingwen[a]^key2[a]); } public static void addroundkey3(int a){ mingwen[a]= (short) (mingwen[a]^key3[a]); } public static void addroundkey4(int a){ mingwen[a]= (short) (mingwen[a]^key4[a]); } public static void addroundkey5(int a){ mingwen[a]= (short) (mingwen[a]^key5[a]); } public static void addroundkey6(int a){ mingwen[a]= (short) (mingwen[a]^key6[a]); } public static void addroundkey7(int a){ mingwen[a]= (short) (mingwen[a]^key7[a]); } public static void addroundkey8(int a){ mingwen[a]= (short) (mingwen[a]^key8[a]); } public static void addroundkey9(int a){ mingwen[a]= (short) (mingwen[a]^key9[a]); } public static void addroundkey10(int a){ mingwen[a]= (short) (mingwen[a]^key10[a]); }//------------------------------------------------------------------------------------------ //主方法 public static void main(String[] args){ System.out.println("初始密钥为"); AES_vc.printkey0(); System.out.println("初始明文为"); AES_vc.print(); System.out.println("经过第一轮轮密钥加操作之后"); AES_vc.print(); for (int i=0;i<16;i++){ AES_vc.addroundkey0(i); } //----------------------------------------------------------------------------- AES_vc.subbytes(); AES_vc.shiftrows(); AES_vc.mixcolumns(); for (int i=0;i<16;i++){ AES_vc.addroundkey1(i); }//--------------------------------------------------------------------------------------- AES_vc.subbytes(); AES_vc.shiftrows(); AES_vc.mixcolumns(); for (int i=0;i<16;i++){ AES_vc.addroundkey2(i); } //--------------------------------------------------------------------------------------- AES_vc.subbytes(); AES_vc.shiftrows(); AES_vc.mixcolumns(); for (int i=0;i<16;i++){ AES_vc.addroundkey3(i); }//--------------------------------------------------------------------------------------- AES_vc.subbytes(); AES_vc.shiftrows(); AES_vc.mixcolumns(); for (int i=0;i<16;i++){ AES_vc.addroundkey4(i); }//--------------------------------------------------------------------------------------- AES_vc.subbytes(); AES_vc.shiftrows(); AES_vc.mixcolumns(); for (int i=0;i<16;i++){ AES_vc.addroundkey5(i); }//--------------------------------------------------------------------------------------- AES_vc.subbytes(); AES_vc.shiftrows(); AES_vc.mixcolumns(); for (int i=0;i<16;i++){ AES_vc.addroundkey6(i); }//--------------------------------------------------------------------------------------- AES_vc.subbytes(); AES_vc.shiftrows(); AES_vc.mixcolumns(); for (int i=0;i<16;i++){ AES_vc.addroundkey7(i); }//--------------------------------------------------------------------------------------- AES_vc.subbytes(); AES_vc.shiftrows(); AES_vc.mixcolumns(); for (int i=0;i<16;i++){ AES_vc.addroundkey8(i); }//--------------------------------------------------------------------------------------- AES_vc.subbytes(); AES_vc.shiftrows(); AES_vc.mixcolumns(); for (int i=0;i<16;i++){ AES_vc.addroundkey9(i); }//---------------------------------------------------------------------------------------最后一轮不用mixcolunms AES_vc.subbytes(); AES_vc.shiftrows(); for (int i=0;i<16;i++){ AES_vc.addroundkey10(i); }//--------------------------------------------------------------------------------------- System.out.println("最后得到的明文矩阵为"); AES_vc.print(); }}
就爱阅读 www.92to.com 网友整理上传, 为您提供最全的知识大全, 期待您的分享,转载请注明出处。
来源: http://www.92to.com/bangong/2017/03-30/19695207.html