⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aes.cpp

📁 使用vc编写的加密解密程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:

byte bmul(byte x, byte y)
{
    if (x && y) return ptab[(ltab[x]+ltab[y])%255];
    else return 0;
}

byte product(word x, word y)
{
    byte xb[4],yb[4];
    Unpack(x,xb);
    Unpack(y,yb); 
    return bmul(xb[0],yb[0])^bmul(xb[1],yb[1])^bmul(xb[2],yb[2])^bmul(xb[3],yb[3]);
}

word InvMixColumns(word x)
{
    word y,m;
    byte b[4];
    byte InCo[4]={0xB,0xD,0x9,0xE};
    
    m=Pack(InCo);
    b[3]=product(m,x);
    m=ROTWORD8(m);
    b[2]=product(m,x);
    m=ROTWORD8(m);
    b[1]=product(m,x);
    m=ROTWORD8(m);
    b[0]=product(m,x);
    y=Pack(b);
    return y;
}

void AddRoundKey(word w[4],  word key[4])
{
    int i;
    for (i = 0; i < 4; i++)
        w[i] ^= key[i];
}

void KeyExpansion(byte key[16], word w[44])
{
    word temp;
    int i;
    for (i = 0; i < 4; i++)
        w[i] = Pack((byte *)&key[4*i]);

    for (i = 4; i < 44; i++) {
        temp = w[i-1];
        if (i%4 == 0)
            temp = SubWord(ROTWORD8(temp)) ^ Rcon[i/4];
        w[i] = w[i-4] ^ temp;
    }
}

void AES_jiami(byte in[16], byte out[16], byte key[16])
{
    word state[4], temp[4], w[44];
    int r,i;
    word * round_key = w;
    
    for (i = 0; i < 4; i++)
        state[i] = Pack(&in[i*4]);

    KeyExpansion(key, round_key);

    AddRoundKey(state, &round_key[0]);

    r = 5;
    for (;;) {
        temp[0] =
            Te0[(state[0]      ) & 0xff] ^
            Te1[(state[1] >>  8) & 0xff] ^
            Te2[(state[2] >> 16) & 0xff] ^
            Te3[(state[3] >> 24)       ] ^
            round_key[4];
        temp[1] =
            Te0[(state[1]      ) & 0xff] ^
            Te1[(state[2] >>  8) & 0xff] ^
            Te2[(state[3] >> 16) & 0xff] ^
            Te3[(state[0] >> 24)       ] ^
            round_key[5];
        temp[2] =
            Te0[(state[2]      ) & 0xff] ^
            Te1[(state[3] >>  8) & 0xff] ^
            Te2[(state[0] >> 16) & 0xff] ^
            Te3[(state[1] >> 24)       ] ^
            round_key[6];
        temp[3] =
            Te0[(state[3]      ) & 0xff] ^
            Te1[(state[0] >>  8) & 0xff] ^
            Te2[(state[1] >> 16) & 0xff] ^
            Te3[(state[2] >> 24)       ] ^
            round_key[7];

        round_key += 8;
        if (--r == 0) {
            break;
        }

        state[0] =
            Te0[(temp[0]      ) & 0xff] ^
            Te1[(temp[1] >>  8) & 0xff] ^
            Te2[(temp[2] >> 16) & 0xff] ^
            Te3[(temp[3] >> 24)       ] ^
            round_key[0];
        state[1] =
            Te0[(temp[1]      ) & 0xff] ^
            Te1[(temp[2] >>  8) & 0xff] ^
            Te2[(temp[3] >> 16) & 0xff] ^
            Te3[(temp[0] >> 24)       ] ^
            round_key[1];
        state[2] =
            Te0[(temp[2]      ) & 0xff] ^
            Te1[(temp[3] >>  8) & 0xff] ^
            Te2[(temp[0] >> 16) & 0xff] ^
            Te3[(temp[1] >> 24)       ] ^
            round_key[2];
        state[3] =
            Te0[(temp[3]      ) & 0xff] ^
            Te1[(temp[0] >>  8) & 0xff] ^
            Te2[(temp[1] >> 16) & 0xff] ^
            Te3[(temp[2] >> 24)       ] ^
            round_key[3];
    }
    
    state[0] =
        (Te4[(temp[0]      ) & 0xff] & 0x000000ff) ^
        (Te4[(temp[1] >>  8) & 0xff] & 0x0000ff00) ^
        (Te4[(temp[2] >> 16) & 0xff] & 0x00ff0000) ^
        (Te4[(temp[3] >> 24)       ] & 0xff000000) ^
        round_key[0];
    state[1] =
        (Te4[(temp[1]      ) & 0xff] & 0x000000ff) ^
        (Te4[(temp[2] >>  8) & 0xff] & 0x0000ff00) ^
        (Te4[(temp[3] >> 16) & 0xff] & 0x00ff0000) ^
        (Te4[(temp[0] >> 24)       ] & 0xff000000) ^
        round_key[1];
    state[2] =
        (Te4[(temp[2]      ) & 0xff] & 0x000000ff) ^
        (Te4[(temp[3] >>  8) & 0xff] & 0x0000ff00) ^
        (Te4[(temp[0] >> 16) & 0xff] & 0x00ff0000) ^
        (Te4[(temp[1] >> 24)       ] & 0xff000000) ^
        round_key[2];
    state[3] =
        (Te4[(temp[3]      ) & 0xff] & 0x000000ff) ^
        (Te4[(temp[0] >>  8) & 0xff] & 0x0000ff00) ^
        (Te4[(temp[1] >> 16) & 0xff] & 0x00ff0000) ^
        (Te4[(temp[2] >> 24)       ] & 0xff000000) ^
        round_key[3];

    for (i = 0; i < 4; i++)
        Unpack(state[i], &out[i*4]);
}

void AES_jiemi(byte in[16], byte out[16], byte key[16])
{
    word state[4], temp[4], w[44];
    int i, r;
    word * round_key = w;
    
    for (i = 0; i < 4; i++)
        state[i] = Pack(&in[i*4]);

    KeyExpansion(key, round_key);
    for (i = 4; i < 40; i++) {
        round_key[i] = InvMixColumns(round_key[i]);
    }

    AddRoundKey(state, &round_key[10 * 4]);

    round_key = & w[9 * 4];
    r = 5;
    for (;;) {
        temp[0] =
            Td0[(state[0]      ) & 0xff] ^
            Td1[(state[3] >>  8) & 0xff] ^
            Td2[(state[2] >> 16) & 0xff] ^
            Td3[(state[1] >> 24)       ] ^
            round_key[0];
        temp[1] =
            Td0[(state[1]      ) & 0xff] ^
            Td1[(state[0] >>  8) & 0xff] ^
            Td2[(state[3] >> 16) & 0xff] ^
            Td3[(state[2] >> 24)       ] ^
            round_key[1];
        temp[2] =
            Td0[(state[2]      ) & 0xff] ^
            Td1[(state[1] >>  8) & 0xff] ^
            Td2[(state[0] >> 16) & 0xff] ^
            Td3[(state[3] >> 24)       ] ^
            round_key[2];
        temp[3] =
            Td0[(state[3]      ) & 0xff] ^
            Td1[(state[2] >>  8) & 0xff] ^
            Td2[(state[1] >> 16) & 0xff] ^
            Td3[(state[0] >> 24)       ] ^
            round_key[3];

        round_key -= 8;
        if (--r == 0) {
            break;
        }
        
        state[0] =
            Td0[(temp[0]      ) & 0xff] ^
            Td1[(temp[3] >>  8) & 0xff] ^
            Td2[(temp[2] >> 16) & 0xff] ^
            Td3[(temp[1] >> 24)       ] ^
            round_key[4];
        state[1] =
            Td0[(temp[1]      ) & 0xff] ^
            Td1[(temp[0] >>  8) & 0xff] ^
            Td2[(temp[3] >> 16) & 0xff] ^
            Td3[(temp[2] >> 24)       ] ^
            round_key[5];
        state[2] =
            Td0[(temp[2]      ) & 0xff] ^
            Td1[(temp[1] >>  8) & 0xff] ^
            Td2[(temp[0] >> 16) & 0xff] ^
            Td3[(temp[3] >> 24)       ] ^
            round_key[6];
        state[3] =
            Td0[(temp[3]      ) & 0xff] ^
            Td1[(temp[2] >>  8) & 0xff] ^
            Td2[(temp[1] >> 16) & 0xff] ^
            Td3[(temp[0] >> 24)       ] ^
            round_key[7];
    }

    state[0] =
        (Td4[(temp[0]      ) & 0xff] & 0x000000ff) ^
        (Td4[(temp[3] >>  8) & 0xff] & 0x0000ff00) ^
        (Td4[(temp[2] >> 16) & 0xff] & 0x00ff0000) ^
        (Td4[(temp[1] >> 24)       ] & 0xff000000) ^
        round_key[4];
    state[1] =
        (Td4[(temp[1]      ) & 0xff] & 0x000000ff) ^
        (Td4[(temp[0] >>  8) & 0xff] & 0x0000ff00) ^
        (Td4[(temp[3] >> 16) & 0xff] & 0x00ff0000) ^
        (Td4[(temp[2] >> 24)       ] & 0xff000000) ^
        round_key[5];
    state[2] =
        (Td4[(temp[2]      ) & 0xff] & 0x000000ff) ^
        (Td4[(temp[1] >>  8) & 0xff] & 0x0000ff00) ^
        (Td4[(temp[0] >> 16) & 0xff] & 0x00ff0000) ^
        (Td4[(temp[3] >> 24)       ] & 0xff000000) ^
        round_key[6];
    state[3] =
        (Td4[(temp[3]      ) & 0xff] & 0x000000ff) ^
        (Td4[(temp[2] >>  8) & 0xff] & 0x0000ff00) ^
        (Td4[(temp[1] >> 16) & 0xff] & 0x00ff0000) ^
        (Td4[(temp[0] >> 24)       ] & 0xff000000) ^
        round_key[7];

    for (i = 0; i < 4; i++)
        Unpack(state[i], &out[i*4]);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -