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

📄 aes.c

📁 加密解密,安全工具!很有意思的代码
💻 C
📖 第 1 页 / 共 3 页
字号:
        for ( rnd = 0; rnd < f_dat(cx,Nrnd) - 1; ++rnd )
        {
            cf_round(p1, p0, kp);
            t = p0, p0 = p1, p1 = t; kp += n_col;
        }

        lcf_round(p1, p0, kp);
    }   
#elif defined(UNROLL)
    {
        if ( f_dat(cx,Nrnd) > 12 )
        {
            f_round(b1, b0, kp);
            f_round(b0, b1, kp + n_col); kp += 2 * n_col;
        }

        if ( f_dat(cx,Nrnd) > 10 )
        {
            f_round(b1, b0, kp);
            f_round(b0, b1, kp + n_col); kp += 2 * n_col;
        }

        f_round(b1, b0, kp);            f_round(b0, b1, kp +     n_col);
        f_round(b1, b0, kp + 2 * n_col); f_round(b0, b1, kp + 3 * n_col);
        f_round(b1, b0, kp + 4 * n_col); f_round(b0, b1, kp + 5 * n_col);
        f_round(b1, b0, kp + 6 * n_col); f_round(b0, b1, kp + 7 * n_col);
        f_round(b1, b0, kp + 8 * n_col); kp += 9 * n_col;

        lf_round(b0, b1, kp);
    }
#else
    {   word    rnd;

        for ( rnd = 0; rnd < (f_dat(cx,Nrnd) >> 1) - 1; ++rnd )
        {
            f_round(b1, b0, kp);
            f_round(b0, b1, kp + n_col); kp += 2 * n_col;
        }

        f_round(b1, b0, kp); kp += n_col;

        lf_round(b0, b1, kp);
    }
#endif

    state_out(b0);
    return aes_good;
}

    #if defined(AES_IN_CPP)
rval aes::_decrypt(const byte in_blk[], byte out_blk[]) const
    #else
cf_dec _decrypt(const byte in_blk[], byte out_blk[], const aes *cx)
    #endif
{
    word        b0[Mcol], b1[Mcol];

#if defined(COMPACT)
    const word  *kp = f_dat(cx,e_key) + n_col * f_dat(cx,Nrnd);
#else
    const word  *kp = f_dat(cx,d_key) + n_col * f_dat(cx,Nrnd);
#endif

    if ( !(f_dat(cx,mode) & 0x02) ) return aes_bad;
    state_in(b0, kp);
    kp -= n_col;

#if defined(COMPACT)
    {   word *p0 = b0, *p1 = b1, *t, rnd, u, f2, f4, f8, f9; 

        for ( rnd = 0; rnd < f_dat(cx,Nrnd) - 1; ++rnd )
        {
            ci_round(p1, p0, kp);
            t = p0, p0 = p1, p1 = t; kp -= n_col;
        }

        lci_round(p1, p0, kp);
    }
#elif defined(UNROLL)
    {
        if ( f_dat(cx,Nrnd) > 12 )
        {
            i_round(b1, b0, kp);
            i_round(b0, b1, kp - n_col); kp -= 2 * n_col;
        }

        if ( f_dat(cx,Nrnd) > 10 )
        {
            i_round(b1, b0, kp);
            i_round(b0, b1, kp - n_col); kp -= 2 * n_col;
        }

        i_round(b1, b0, kp);            i_round(b0, b1, kp -     n_col);
        i_round(b1, b0, kp - 2 * n_col); i_round(b0, b1, kp - 3 * n_col);
        i_round(b1, b0, kp - 4 * n_col); i_round(b0, b1, kp - 5 * n_col);
        i_round(b1, b0, kp - 6 * n_col); i_round(b0, b1, kp - 7 * n_col);
        i_round(b1, b0, kp - 8 * n_col); kp -= 9 * n_col;

        li_round(b0, b1, kp);
    }
#else
    {   word rnd;

        for ( rnd = 0; rnd < (f_dat(cx,Nrnd) >> 1) - 1; ++rnd )
        {
            i_round(b1, b0, kp);
            i_round(b0, b1, kp - n_col); kp -= 2 * n_col;
        }

        i_round(b1, b0, kp); kp -= n_col;

        li_round(b0, b1, kp);
    }
#endif

    state_out(b0);
    return aes_good;
}

#endif // !defined(AES_DLL) || !defined(TEST)

// Testing Code that is not required for AES (Rijndael) implementation

#if defined(TEST)

    #include <memory.h>

    #if defined(AES_IN_CPP)

        #include <iostream>
        #include <iomanip>

void oblk(char m[], byte v[], word n = 16)
{
    std::cout << std::hex << std::setfill('0') << '\n' << m;

    for ( int i = 0; i < n; ++i )

        std::cout << std::setw(2) << static_cast<word>(v[i]);
}

void message(char *s)
{
    std::cout << s;
};

    #else

        #include <stdio.h>

void oblk(char m[], byte v[], word n)
{
    word i;

    printf("\n%s", m);

    for ( i = 0; i < n; ++i )

        printf("%02x", v[i]);
}

void message(char *s)
{
    printf(s);
};

    #endif

    #if defined(AES_DLL)

        #include "windows.h"

HINSTANCE init_dll(fn_ptrs *fn)
{
    HINSTANCE   h_dll;

    if ( !(h_dll = LoadLibrary(dll_path)) )
    {
        message("\n\nDynamic link Library AES_DLL not found\n\n"); return 0;
    }

    fn->fn_set_key = (f_ky*)GetProcAddress(h_dll, "_set_key@16");
    fn->fn_encrypt = (f_ed*)GetProcAddress(h_dll, "_encrypt@12");
    fn->fn_decrypt = (f_ed*)GetProcAddress(h_dll, "_decrypt@12");
    fn->fn_set_blk = (f_bl*)GetProcAddress(h_dll, "_set_blk@8");

#if !defined(BLOCK_SIZE_R)
    if ( !fn->fn_set_key || !fn->fn_encrypt || !fn->fn_decrypt || !fn->fn_set_blk )
#else
    if ( !fn->fn_set_key || !fn->fn_encrypt || !fn->fn_decrypt )
#endif
    {
        message("\n\nRequired DLL Entry Point(s) not found\n\n"); 
        FreeLibrary(h_dll); 
        return 0;
    }

    return h_dll;
}

    #endif // AES_DLL

byte pih[32] = // hex digits of pi
{
    0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d,
    0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34,
    0x4a, 0x40, 0x93, 0x82, 0x22, 0x99, 0xf3, 0x1d,
    0x00, 0x82, 0xef, 0xa9, 0x8e, 0xc4, 0xe6, 0xc8
};

byte exh[32] =  // hex digits of e
{
    0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
    0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
    0x76, 0x2e, 0x71, 0x60, 0xf3, 0x8b, 0x4d, 0xa5,
    0x6a, 0x78, 0x4d, 0x90, 0x45, 0x19, 0x0c, 0xfe
};

byte res[3][3][32] = 
{ 
    {
        { 0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb, 
            0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32 
        },
        { 0xf9, 0xfb, 0x29, 0xae, 0xfc, 0x38, 0x4a, 0x25, 
            0x03, 0x40, 0xd8, 0x33, 0xb8, 0x7e, 0xbc, 0x00
        },
        { 0x1a, 0x6e, 0x6c, 0x2c, 0x66, 0x2e, 0x7d, 0xa6, 
            0x50, 0x1f, 0xfb, 0x62, 0xbc, 0x9e, 0x93, 0xf3
        }
    },
    {
        { 0xb2, 0x4d, 0x27, 0x54, 0x89, 0xe8, 0x2b, 0xb8, 
            0xf7, 0x37, 0x5e, 0x0d, 0x5f, 0xcd, 0xb1, 0xf4, 
            0x81, 0x75, 0x7c, 0x53, 0x8b, 0x65, 0x14, 0x8a
        },
        { 0x72, 0x5a, 0xe4, 0x3b, 0x5f, 0x31, 0x61, 0xde, 
            0x80, 0x6a, 0x7c, 0x93, 0xe0, 0xbc, 0xa9, 0x3c,
            0x96, 0x7e, 0xc1, 0xae, 0x1b, 0x71, 0xe1, 0xcf
        },
        { 0x0e, 0xba, 0xcf, 0x19, 0x9e, 0x33, 0x15, 0xc2, 
            0xe3, 0x4b, 0x24, 0xfc, 0xc7, 0xc4, 0x6e, 0xf4, 
            0x38, 0x8a, 0xa4, 0x75, 0xd6, 0x6c, 0x19, 0x4c
        }
    },
    {
        { 0x7d, 0x15, 0x47, 0x90, 0x76, 0xb6, 0x9a, 0x46, 
            0xff, 0xb3, 0xb3, 0xbe, 0xae, 0x97, 0xad, 0x83, 
            0x13, 0xf6, 0x22, 0xf6, 0x7f, 0xed, 0xb4, 0x87, 
            0xde, 0x9f, 0x06, 0xb9, 0xed, 0x9c, 0x8f, 0x19
        },
        { 0x5d, 0x71, 0x01, 0x72, 0x7b, 0xb2, 0x57, 0x81, 
            0xbf, 0x67, 0x15, 0xb0, 0xe6, 0x95, 0x52, 0x82, 
            0xb9, 0x61, 0x0e, 0x23, 0xa4, 0x3c, 0x2e, 0xb0, 
            0x62, 0x69, 0x9f, 0x0e, 0xbf, 0x58, 0x87, 0xb2
        },
        { 0xa4, 0x94, 0x06, 0x11, 0x5d, 0xfb, 0x30, 0xa4, 
            0x04, 0x18, 0xaa, 0xfa, 0x48, 0x69, 0xb7, 0xc6, 
            0xa8, 0x86, 0xff, 0x31, 0x60, 0x2a, 0x7d, 0xd1, 
            0x9c, 0x88, 0x9d, 0xc6, 0x4f, 0x7e, 0x4e, 0x7a
        }
    }
};

    #if !defined(BLOCK_SIZE_R)
        #define STR 0
        #define CNT 3
    #elif BLOCK_SIZE_R == 16
        #define STR 0
        #define CNT 1
    #elif BLOCK_SIZE_R == 24
        #define STR 1
        #define CNT 2
    #elif BLOCK_SIZE_R == 32
        #define STR 2
        #define CNT 3
    #else
        #error Illegal block size
    #endif

int main(void)
{
    byte        out[32], ret[32], err = 0, i, j;
    aes         alg;

#if defined(AES_DLL)
    HINSTANCE   h_dll;
    fn_ptrs     fn;

    if ( !(h_dll = init_dll(&fn)) ) return -1;
#endif

#if !defined(AES_IN_CPP)
    alg.mode = 0;
#endif

    for ( i = STR; i < CNT; ++i )
    {
#if !defined(BLOCK_SIZE_R)
        f_set_blk(&alg, 16 + 8 * i);
#elif defined(AES_DLL)
        if ( fn.fn_set_blk ) f_set_blk(&alg, BLOCK_SIZE_R);
#endif
        for ( j = 0; j < 3; ++j )
        {
            f_set_key(&alg, exh, 16 + 8 * j, both); 
            oblk("\nkey     = ", exh, 16 + 8 * j); 
            oblk("input   = ", pih, 16 + 8 * i); 
            f_encrypt(&alg, pih, out); 
            oblk("enc     = ", out, 16 + 8 * i);
            if ( memcmp(out, res[i][j], 16 + 8 * i) ) err++;
            f_decrypt(&alg, out, ret); 
            oblk("dec     = ", ret, 16 + 8 * i); 
            if ( memcmp(ret, pih, 16 + 8 * i) ) err++;
        }
    }

    message("\n\nThese values are ");
    message(err ? "in error\n\n" : "correct\n\n");

#if defined(AES_DLL)
    if ( h_dll ) FreeLibrary(h_dll);
#endif

    return 0;
}

#endif // TEST
char aes_c[]="$Id: aes.c,v 1.3 2002/10/29 07:11:46 crypt Rel-1.6-3 $";

⌨️ 快捷键说明

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