当前位置:编程学习 > JAVA >>

AES加密解密算法的Java实现

 

工作需要,花了两个下午将Aes算法了解了一下并用java实现了一遍(此处借鉴了老外的C#的实现),本人非信息安全专业和软件,代码还有漏洞,欢迎指出~~~

先贴上代码:

=================================================AES.java主要的类实现====================================================

 

package aesHelper.Aes; 

 

/**

 * AES字节数组加密算法类

 * 

 * @author DC 2011/11/5

 */ 

public class Aes 

    /*************************** 成员变量************************************/ 

 

    private int Nb;// 以32位为单位的字长 

    private int Nk;// 以32位为单位的密钥长度 

    private int Nr;// 轮数 

    private byte[] key;// 密钥 

    private byte[][] Sbox;// S盒矩阵 

    private byte[][] iSbox;// s盒逆矩阵 

    private byte[][] w;// 密钥调度表 

    private byte[][] Rcon;// 轮常数表 

    private byte[][] State;// 状态矩阵 

 

    /**

     * 构造方法

     * 

     * @param keySize

     * @param keyBytes

     */ 

    public Aes(int keySize, byte[] keyBytes, int Nb) 

    { 

        SetNbNkNr(keySize, Nb); 

        this.key = new byte[this.Nk * 4]; 

        this.key = keyBytes; 

 

        BuildSbox(); 

        BuildInvSbox(); 

        BuildRcon(); 

        KeyExpansion(); 

    } 

 

    /*************************** 私有方法************************************/ 

 

    /**

     * 生成Rcon轮常数矩阵

     */ 

    private void BuildRcon() 

    { 

        // 0x00,0x01,0x02,0x04, 

        // 0x08,0x10,0x20,0x40, 

        // 0x80,0x1b,0x36,0x6c, 

        // 0xd8,0xab,0x4d,0x9a, 

        // 0x2f,0x5e,0xbc,0x63, 

        // 0xc6,0x97,0x35,0x6a, 

        // 0xd4,0xb3,0x7d,0xfa, 

        // 0xef,0xc5,0x91,0x39}; 

        this.Rcon = new byte[100][4]; 

        Rcon[0][0]=0x00;//Rcon[1][0]=0x01;       

        for(int i=1;i<100;i++) 

        { 

            Rcon[i][0]=gfmultby02(Rcon[i-1][0]); 

        } 

    } 

 

    /**

     * 设置Nb,Nk,Nr

     * 

     * @param keysize

     */ 

    private void SetNbNkNr(int keysize, int Nb) 

    { 

        this.Nb = Nb; 

        switch (keysize) 

        { 

        case KEYSIZE.Bit128: 

            this.Nk = 4; 

            this.Nr = 10; 

            break; 

        case KEYSIZE.Bit192: 

            this.Nk = 6; 

            this.Nr = 12; 

            break; 

        case KEYSIZE.Bit256: 

            this.Nk = 8; 

            this.Nr = 14; 

            break; 

        } 

    } 

 

    /**

     * 生成S盒矩阵

     */ 

    private void BuildSbox() 

    { 

        this.Sbox = new byte[][] { 

                /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ 

                /* 0 */{ 0x63, 0x7c, 0x77, 0x7b, (byte) 0xf2, 0x6b, 0x6f, 

                        (byte) 0xc5, 0x30, 0x01, 0x67, 0x2b, (byte) 0xfe, 

                        (byte) 0xd7, (byte) 0xab, 0x76 }, 

                /* 1 */{ (byte) 0xca, (byte) 0x82, (byte) 0xc9, 0x7d, 

                        (byte) 0xfa, 0x59, 0x47, (byte) 0xf0, (byte) 0xad, 

                        (byte) 0xd4, (byte) 0xa2, (byte) 0xaf, (byte) 0x9c, 

                        (byte) 0xa4, 0x72, (byte) 0xc0 }, 

                /* 2 */{ (byte) 0xb7, (byte) 0xfd, (byte) 0x93, 0x26, 0x36, 

                        0x3f, (byte) 0xf7, (byte) 0xcc, 0x34, (byte) 0xa5, 

                        (byte) 0xe5, (byte) 0xf1, 0x71, (byte) 0xd8, 0x31, 0x15 }, 

&nbs

补充:综合编程 , 安全编程 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,