精华内容
下载资源
问答
  • C语言 AES算法 加密解密
    2020-09-27 14:04:04

    C语言 实现 AES 128 位加密解密
    1、调用函数

    #include "stdio.h"  
    #include "stdlib.h"
    #include <string.h>
    
    #include "aes.h"
    
    extern OL_APITABLE_T *AP_interface;
    
    typedef struct{
        uint32_t eK[44], dK[44];    // encKey, decKey
        int Nr; // 10 rounds
    }AesKey;
    
    #define BLOCKSIZE 16  //AES-128分组长度为16字节
    
    // uint8_t y[4] -> uint32_t x
    #define LOAD32H(x, y) \
      do { (x) = ((uint32_t)((y)[0] & 0xff)<<24) | ((uint32_t)((y)[1] & 0xff)<<16) | \
                 ((uint32_t)((y)[2] & 0xff)<<8)  | ((uint32_t)((y)[3] & 0xff));} while(0)
    
    // uint32_t x -> uint8_t y[4]
    #define STORE32H(x, y) \
      do { (y)[0] = (uint8_t)(((x)>>24) & 0xff); (y)[1] = (uint8_t)(((x)>>16) & 0xff);   \
           (y)[2] = (uint8_t)(((x)>>8) & 0xff); (y)[3] = (uint8_t)((x) & 0xff); } while(0)
    
    // 从uint32_t x中提取从低位开始的第n个字节
    #define BYTE(x, n) (((x) >> (8 * (n))) & 0xff)
    
    /* used for keyExpansion */
    // 字节替换然后循环左移1位
    #define MIX(x) (((S[BYTE(x, 2)] << 24) & 0xff000000) ^ ((S[BYTE(x, 1)] << 16) & 0xff0000) ^ \
                    ((S[BYTE(x, 0)] << 8) & 0xff00) ^ (S[BYTE(x, 3)] & 0xff))
    
    // uint32_t x循环左移n位
    #define ROF32(x, n)  (((x) << (n)) | ((x) >> (32-(n))))
    // uint32_t x循环右移n位
    #define ROR32(x, n)  (((x) >> (n)) | ((x) << (32-(n))))
    
    /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */
    // AES-128轮常量
    static const uint32_t rcon[10] = {
            0x01000000UL, 0x02000000UL, 0x04000000UL, 0x08000000UL, 0x10000000UL,
            0x20000000UL, 0x40000000UL, 0x80000000UL, 0x1B000000UL, 0x36000000UL
    };
    // S盒
    unsigned char S[256] = {
            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盒
    unsigned char inv_S[256] = {
            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
    };
    
    /* copy in[16] to state[4][4] */
    int loadStateArray(uint8_t (*state)[4], const uint8_t *in) {
    	int i=0,j=0;
    
        for (i = 0; i < 4; ++i) {
            for (j = 0; j < 4; ++j) {
                state[j][i] = *in++;
            }
        }
        return 0;
    }
    
    /* copy state[4][4] to out[16] */
    int storeStateArray(uint8_t (*state)[4], uint8_t *out) {
    	int i=0,j=0;
    
        for (i = 0; i < 4; ++i) {
            for (j = 0; j < 4; ++j) {
                *out++ = state[j][i];
            }
        }
        return 0;
    }
    //秘钥扩展
    int keyExpansion(const uint8_t *key, uint32_t keyLen, AesKey *aesKey) {
    
        uint32_t *w = aesKey->eK;  //加密秘钥
        uint32_t *v = aesKey->dK;  //解密秘钥
    	int i=0,j=0;
    
        if (NULL == key || NULL == aesKey){
            AP_interface->ol_print("keyExpansion param is NULL\n");
            return -1;
        }
    
        if (keyLen != 16){
            AP_interface->ol_print("keyExpansion keyLen = %d, Not support.\n", keyLen);
            return -1;
        }
    
        /* keyLen is 16 Bytes, generate uint32_t W[44]. */
    
        /* W[0-3] */
        for (i = 0; i < 4; ++i) {
            LOAD32H(w[i], key + 4*i);
        }
    
        /* W[4-43] */
        for (i = 0; i < 10; ++i) {
            w[4] = w[0] ^ MIX(w[3]) ^ rcon[i];
            w[5] = w[1] ^ w[4];
            w[6] = w[2] ^ w[5];
            w[7] = w[3] ^ w[6];
            w += 4;
        }
    
        w = aesKey->eK+44 - 4;
        //解密秘钥矩阵为加密秘钥矩阵的倒序,方便使用,把ek的11个矩阵倒序排列分配给dk作为解密秘钥
        //即dk[0-3]=ek[41-44], dk[4-7]=ek[37-40]... dk[41-44]=ek[0-3]
        for (j = 0; j < 11; ++j) {
    
            for (i = 0; i < 4; ++i) {
                v[i] = w[i];
            }
            w -= 4;
            v += 4;
        }
    
        return 0;
    }
    
    // 轮秘钥加
    int addRoundKey(uint8_t (*state)[4], const uint32_t *key) {
        uint8_t k[4][4];
    	int i=0,j=0;
    
        /* i: row, j: col */
        for (i = 0; i < 4; ++i) {
            for (j = 0; j < 4; ++j) {
                k[i][j] = (uint8_t) BYTE(key[j], 3 - i);  /* 把 uint32 key[4] 先转换为矩阵 uint8 k[4][4] */
                state[i][j] ^= k[i][j];
            }
        }
    
        return 0;
    }
    
    //字节替换
    int subBytes(uint8_t (*state)[4]) {
        /* i: row, j: col */
    	int i=0,j=0;
    
        for (i = 0; i < 4; ++i) {
            for (j = 0; j < 4; ++j) {
                state[i][j] = S[state[i][j]]; //直接使用原始字节作为S盒数据下标
            }
        }
    
        return 0;
    }
    
    //逆字节替换
    int invSubBytes(uint8_t (*state)[4]) {
        /* i: row, j: col */
    	int i=0,j=0;
    
        for (i = 0; i < 4; ++i) {
            for (j = 0; j < 4; ++j) {
                state[i][j] = inv_S[state[i][j]];
            }
        }
        return 0;
    }
    
    //行移位
    int shiftRows(uint8_t (*state)[4]) {
        uint32_t block[4] = {0};
    	int i=0,j=0;
    
        /* i: row */
        for (i = 0; i < 4; ++i) {
        //便于行循环移位,先把一行4字节拼成uint_32结构,移位后再转成独立的4个字节uint8_t
            LOAD32H(block[i], state[i]);
            block[i] = ROF32(block[i], 8*i);
            STORE32H(block[i], state[i]);
        }
    
        return 0;
    }
    
    //逆行移位
    int invShiftRows(uint8_t (*state)[4]) {
        uint32_t block[4] = {0};
    	int i=0,j=0;
    
        /* i: row */
        for (i = 0; i < 4; ++i) {
            LOAD32H(block[i], state[i]);
            block[i] = ROR32(block[i], 8*i);
            STORE32H(block[i], state[i]);
        }
    
        return 0;
    }
    
    /* Galois Field (256) Multiplication of two Bytes */
    // 两字节的伽罗华域乘法运算
    uint8_t GMul(uint8_t u, uint8_t v) {
        uint8_t p = 0;
    	int i=0,j=0,flag = 0;
    
        for (i = 0; i < 8; ++i) {
            if (u & 0x01) {    //
                p ^= v;
            }
    
            flag = (v & 0x80);
            v <<= 1;
            if (flag) {
                v ^= 0x1B; /* x^8 + x^4 + x^3 + x + 1 */
            }
    
            u >>= 1;
        }
    
        return p;
    }
    
    // 列混合
    int mixColumns(uint8_t (*state)[4]) {
        uint8_t tmp[4][4];
        uint8_t M[4][4] = {{0x02, 0x03, 0x01, 0x01},
                           {0x01, 0x02, 0x03, 0x01},
                           {0x01, 0x01, 0x02, 0x03},
                           {0x03, 0x01, 0x01, 0x02}};
    	int i=0,j=0;
    
        /* copy state[4][4] to tmp[4][4] */
        for (i = 0; i < 4; ++i) {
            for (j = 0; j < 4; ++j){
                tmp[i][j] = state[i][j];
            }
        }
    
        for (i = 0; i < 4; ++i) {
            for (j = 0; j < 4; ++j) {  //伽罗华域加法和乘法
                state[i][j] = GMul(M[i][0], tmp[0][j]) ^ GMul(M[i][1], tmp[1][j])
                            ^ GMul(M[i][2], tmp[2][j]) ^ GMul(M[i][3], tmp[3][j]);
            }
        }
    
        return 0;
    }
    
    // 逆列混合
    int invMixColumns(uint8_t (*state)[4]) {
        uint8_t tmp[4][4];
        uint8_t M[4][4] = {{0x0E, 0x0B, 0x0D, 0x09},
                           {0x09, 0x0E, 0x0B, 0x0D},
                           {0x0D, 0x09, 0x0E, 0x0B},
                           {0x0B, 0x0D, 0x09, 0x0E}};  //使用列混合矩阵的逆矩阵
    	int i=0,j=0;
    
        /* copy state[4][4] to tmp[4][4] */
        for (i = 0; i < 4; ++i) {
            for (j = 0; j < 4; ++j){
                tmp[i][j] = state[i][j];
            }
        }
    
        for (i = 0; i < 4; ++i) {
            for (j = 0; j < 4; ++j) {
                state[i][j] = GMul(M[i][0], tmp[0][j]) ^ GMul(M[i][1], tmp[1][j])
                              ^ GMul(M[i][2], tmp[2][j]) ^ GMul(M[i][3], tmp[3][j]);
            }
        }
    
        return 0;
    }
    
    

    2、加密方法

    // AES-128加密接口,输入key应为16字节长度,输入长度应该是16字节整倍数,
    // 这样输出长度与输入长度相同,函数调用外部为输出数据分配内存
    int aesEncrypt(const uint8_t *key, uint32_t keyLen, const uint8_t *pt, uint8_t *ct, uint32_t len) {
    
        AesKey aesKey;
        uint8_t *pos = ct;
        const uint32_t *rk = aesKey.eK;  //解密秘钥指针
        uint8_t out[BLOCKSIZE] = {0};
        uint8_t actualKey[16] = {0};
        uint8_t state[4][4] = {0};
    	int i=0,j=0;
    
        if (NULL == key || NULL == pt || NULL == ct){
            AP_interface->ol_print("param err.\n");
            return -1;
        }
    
        if (keyLen > 16){
            AP_interface->ol_print("keyLen must be 16.\n");
            return -1;
        }
    
        if (len % BLOCKSIZE){
            AP_interface->ol_print("inLen is invalid.\n");
            return -1;
        }
    
        memcpy(actualKey, key, keyLen);
        keyExpansion(actualKey, 16, &aesKey);  // 秘钥扩展
    
    	// 使用ECB模式循环加密多个分组长度的数据
        for (i = 0; i < len; i += BLOCKSIZE) {
    		// 把16字节的明文转换为4x4状态矩阵来进行处理
            loadStateArray(state, pt);
            // 轮秘钥加
            addRoundKey(state, rk);
    
            for (j = 1; j < 10; ++j) {
                rk += 4;
                subBytes(state);   // 字节替换
                shiftRows(state);  // 行移位
                mixColumns(state); // 列混合
                addRoundKey(state, rk); // 轮秘钥加
            }
    
            subBytes(state);    // 字节替换
            shiftRows(state);  // 行移位
            // 此处不进行列混合
            addRoundKey(state, rk+4); // 轮秘钥加
    		
    		// 把4x4状态矩阵转换为uint8_t一维数组输出保存
            storeStateArray(state, pos);
    
            pos += BLOCKSIZE;  // 加密数据内存指针移动到下一个分组
            pt += BLOCKSIZE;   // 明文数据指针移动到下一个分组
            rk = aesKey.eK;    // 恢复rk指针到秘钥初始位置
        }
        return 0;
    }


    3、解密方法

    // AES128解密, 参数要求同加密
    int aesDecrypt(const uint8_t *key, uint32_t keyLen, const uint8_t *ct, uint8_t *pt, uint32_t len) {
        AesKey aesKey;
        uint8_t *pos = pt;
        const uint32_t *rk = aesKey.dK;  //解密秘钥指针
        uint8_t out[BLOCKSIZE] = {0};
        uint8_t actualKey[16] = {0};
        uint8_t state[4][4] = {0};
    	int i=0,j=0;
    
        if (NULL == key || NULL == ct || NULL == pt){
            AP_interface->ol_print("param err.\n");
            return -1;
        }
    
        if (keyLen > 16){
            AP_interface->ol_print("keyLen must be 16.\n");
            return -1;
        }
    
        if (len % BLOCKSIZE){
            AP_interface->ol_print("inLen is invalid.\n");
            return -1;
        }
    
        memcpy(actualKey, key, keyLen);
        keyExpansion(actualKey, 16, &aesKey);  //秘钥扩展,同加密
    
        for (i = 0; i < len; i += BLOCKSIZE) {
            // 把16字节的密文转换为4x4状态矩阵来进行处理
            loadStateArray(state, ct);
            // 轮秘钥加,同加密
            addRoundKey(state, rk);
    
            for (j = 1; j < 10; ++j) {
                rk += 4;
                invShiftRows(state);    // 逆行移位
                invSubBytes(state);     // 逆字节替换,这两步顺序可以颠倒
                addRoundKey(state, rk); // 轮秘钥加,同加密
                invMixColumns(state);   // 逆列混合
            }
    
            invSubBytes(state);   // 逆字节替换
            invShiftRows(state);  // 逆行移位
            // 此处没有逆列混合
            addRoundKey(state, rk+4);  // 轮秘钥加,同加密
    
            storeStateArray(state, pos);  // 保存明文数据
            pos += BLOCKSIZE;  // 输出数据内存指针移位分组长度
            ct += BLOCKSIZE;   // 输入数据内存指针移位分组长度
            rk = aesKey.dK;    // 恢复rk指针到秘钥初始位置
        }
        return 0;
    }
    

     

    更多相关内容
  • C语言实现的128位AES加密算法,可以运行在JAVA的JNI 中AIS加密算法c语言实现代码 nt cnt for(ent =0: cnt< 8: cnt++) BvtcToBit(*(ch+cnt), bit+(ent<<3)) return /将二进制位串转为长度为8的字符串水 ...
  • AES加密算法,AES的全称是Advanced Encryption Standard,意思是高级加密标准。它的出现主要是为了取代DES加密算法的,因为我们都知道DES算法的密钥长度是56Bit,因此算法的理论安全强度是2的56次方。 AES密码与...
  • AES加密解密C语言实现

    2021-11-24 09:23:29
    一、在线AES加密 网址:https://the-x.cn/cryptography/Aes.aspx 长度:128bit/16字节 明文:32313131313131313131313131313132(hex格式) 密钥:1111111111111111(字符格式,根据ASCII表可以转换,即:31 31 31 ...

    一、在线AES加密

    网址:https://the-x.cn/cryptography/Aes.aspx

    长度:128bit/16字节

    明文:32313131313131313131313131313132(hex格式)

    密钥:1111111111111111(字符格式,根据ASCII表可以转换,即:31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31)

    结果:

    在这里插入图片描述

    二、C代码实现

    环境:VS 2019

    代码:

    // ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
    //
    
    #include <iostream>
    
    
    void AddRoundKey(unsigned char* plaintext, unsigned char* CipherKey)/*轮密钥加*/
    {
    	for (int j = 0; j < 16; j++)  plaintext[j] = plaintext[j] ^ CipherKey[j];
    }
    
    void SubBytes(unsigned char* plaintext, unsigned char* plaintextencrypt, int count)/*S盒置换*/
    {
    	unsigned int row, column;
    	unsigned char Sbox[16][16] = {
    		/* 0     1     2     3     4     5     6     7     8     9     a     b     c     d     e     f */
    		/*0*/{ 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76 },
    		/*1*/{ 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0 },
    		/*2*/{ 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15 },
    		/*3*/{ 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75 },
    		/*4*/{ 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84 },
    		/*5*/{ 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf },
    		/*6*/{ 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8 },
    		/*7*/{ 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2 },
    		/*8*/{ 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73 },
    		/*9*/{ 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb },
    		/*a*/{ 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79 },
    		/*b*/{ 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08 },
    		/*c*/{ 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a },
    		/*d*/{ 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e },
    		/*e*/{ 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf },
    		/*f*/{ 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 }
    	};// 填充Sbox矩阵
    	for (int i = 0; i < count; i++)
    	{
    		row = (plaintext[i] & 0xF0) >> 4;
    		column = plaintext[i] & 0x0F;
    		plaintextencrypt[i] = Sbox[row][column];
    	}
    }
    
    void SubBytesRe(unsigned char* plaintext, unsigned char* plaintextencrypt, int count)/*S盒逆置换*/
    {
    	unsigned int row, column;
    	unsigned char Sbox[16][16] = {
    		/* 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}
    	};	// 填充Sbox矩阵
    	for (int i = 0; i < count; i++)
    	{
    		row = (plaintext[i] & 0xF0) >> 4;
    		column = plaintext[i] & 0x0F;
    		plaintextencrypt[i] = Sbox[row][column];
    	}
    }
    
    void ShiftRowsRe(unsigned char* plaintextencrypt)/*行移位的逆*/
    {
    	unsigned char temp = 0;
    	for (int i = 0; i < 4; i++)//第i行
    	{
    		for (int j = 0; j < 4 - i; j++)//第j次左移
    		{
    			temp = plaintextencrypt[i];
    			for (int k = 0; k < 4; k++)
    				plaintextencrypt[i + 4 * k] = plaintextencrypt[i + 4 * (k + 1)];
    			plaintextencrypt[i + 12] = temp;
    		}
    	}
    }
    void ShiftRows(unsigned char* plaintextencrypt)/*行移位*/
    {
    	unsigned char temp = 0;
    	for (int i = 0; i < 4; i++)//第i行
    	{
    		for (int j = 0; j < i; j++)//第j次左移
    		{
    			temp = plaintextencrypt[i];
    			for (int k = 0; k < 4; k++)
    				plaintextencrypt[i + 4 * k] = plaintextencrypt[i + 4 * (k + 1)];
    			plaintextencrypt[i + 12] = temp;
    		}
    	}
    }
    
    unsigned char Mult2(unsigned char num)/*列混淆*/
    {
    	unsigned char temp = num << 1;
    	if ((num >> 7) & 0x01)
    		temp = temp ^ 27;
    	return temp;
    }
    unsigned char Mult3(unsigned char num)
    {
    	return Mult2(num) ^ num;
    }
    void MixColumns(unsigned char* plaintextencrypt, unsigned char* plaintextcrypt)
    {
    	int i;
    	for (i = 0; i < 4; i++)
    		plaintextcrypt[4 * i] = Mult2(plaintextencrypt[4 * i]) ^ Mult3(plaintextencrypt[4 * i + 1]) ^ plaintextencrypt[4 * i + 2] ^ plaintextencrypt[4 * i + 3];
    	for (i = 0; i < 4; i++)
    		plaintextcrypt[4 * i + 1] = plaintextencrypt[4 * i] ^ Mult2(plaintextencrypt[4 * i + 1]) ^ Mult3(plaintextencrypt[4 * i + 2]) ^ plaintextencrypt[4 * i + 3];
    	for (i = 0; i < 4; i++)
    		plaintextcrypt[4 * i + 2] = plaintextencrypt[4 * i] ^ plaintextencrypt[4 * i + 1] ^ Mult2(plaintextencrypt[4 * i + 2]) ^ Mult3(plaintextencrypt[4 * i + 3]);
    	for (i = 0; i < 4; i++)
    		plaintextcrypt[4 * i + 3] = Mult3(plaintextencrypt[4 * i]) ^ plaintextencrypt[4 * i + 1] ^ plaintextencrypt[4 * i + 2] ^ Mult2(plaintextencrypt[4 * i + 3]);
    }
    /*逆列混淆*/
    #define xtime(x)   ((x<<1) ^ (((x>>7) & 1) * 0x1b))
    #define Multiply(x,y) (((y & 1) * x) ^ ((y>>1 & 1) * xtime(x)) ^ ((y>>2 & 1) * xtime(xtime(x))) ^ ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ ((y>>4 & 1) * xtime(xtime(xtime(xtime(x))))))
    void MixColumnsRe(unsigned char* state)
    {
    
    	unsigned char a, b, c, d;
    	for (int i = 0; i < 4; i++)
    	{
    		a = state[4 * i];
    		b = state[4 * i + 1];
    		c = state[4 * i + 2];
    		d = state[4 * i + 3];
    		state[4 * i] = Multiply(a, 0x0e) ^ Multiply(b, 0x0b) ^ Multiply(c, 0x0d) ^ Multiply(d, 0x09);
    		state[4 * i + 1] = Multiply(a, 0x09) ^ Multiply(b, 0x0e) ^ Multiply(c, 0x0b) ^ Multiply(d, 0x0d);
    		state[4 * i + 2] = Multiply(a, 0x0d) ^ Multiply(b, 0x09) ^ Multiply(c, 0x0e) ^ Multiply(d, 0x0b);
    		state[4 * i + 3] = Multiply(a, 0x0b) ^ Multiply(b, 0x0d) ^ Multiply(c, 0x09) ^ Multiply(d, 0x0e);
    	}
    }
    
    int CharToWord(unsigned char* character, int first)/*字节转字*/
    {
    	return (((int)character[first] & 0x000000ff) << 24) | (((int)character[first + 1] & 0x000000ff) << 16) | (((int)character[first + 2] & 0x000000ff) << 8) | ((int)character[first + 3] & 0x000000ff);
    }
    void WordToChar(unsigned int word, unsigned char* character)/*字转字节*/
    {
    	for (int i = 0; i < 4; character[i++] = (word >> (8 * (3 - i))) & 0xFF);
    }
    void ExtendCipherKey(unsigned int* CipherKey_word, int round)/*密钥扩展*/
    {
    	unsigned char CipherKeyChar[4] = { 0 }, CipherKeyCharEncrypt[4] = { 0 };
    	unsigned int Rcon[10] = { 0x01000000,0x02000000,0x04000000,0x08000000,0x10000000,0x20000000,0x40000000,0x80000000,0x1B000000,0x36000000 };	//轮常量
    	for (int i = 4; i < 8; i++)
    	{
    		if (!(i % 4))
    		{
    			WordToChar((CipherKey_word[i - 1] >> 24) | (CipherKey_word[i - 1] << 8), CipherKeyChar);
    			SubBytes(CipherKeyChar, CipherKeyCharEncrypt, 4);
    			CipherKey_word[i] = CipherKey_word[i - 4] ^ CharToWord(CipherKeyCharEncrypt, 0) ^ Rcon[round];
    		}
    		else
    			CipherKey_word[i] = CipherKey_word[i - 4] ^ CipherKey_word[i - 1];
    	}
    }
    
    
    void main()
    {
    	printf("**************AES加解密***************\n");
    	int i = 0, k;
    	unsigned char PlainText[16] = { 0x32,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x32 },
    		CipherKey[16] = { 0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31 },
    		CipherKey1[16] = { 0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31 },
    		PlainText1[16] = { 0 },
    		PlainText2[16] = { 0 };
    	unsigned int CipherKey_word[44] = { 0 };
    	for (i = 0; i < 4; CipherKey_word[i++] = CharToWord(CipherKey, 4 * i));
    	printf("密钥:");
    	for (k = 0; k < 16; k++) printf("%2X ", CipherKey[k]);
    	printf("\n明文:");
    	for (k = 0; k < 16; k++) printf("%02X ", PlainText[k]);
    	printf("\n**************开始加密****************");
    	AddRoundKey(PlainText, CipherKey);
    	for (i = 0; i < 9; i++)
    	{
    		printf("\n第%d轮循环:\n", i + 1);
    		SubBytes(PlainText, PlainText1, 16);/*S盒置换*/
    		ShiftRows(PlainText1);	/*行移位*/
    		MixColumns(PlainText1, PlainText2);	/*列混淆*/
    		ExtendCipherKey(CipherKey_word + 4 * i, i);/*子密钥生成*/
    		for (k = 0; k < 4; k++)  WordToChar(CipherKey_word[k + 4 * (i + 1)], CipherKey + 4 * k);
    		printf("此时的子密钥为:    ");
    		for (k = 0; k < 16; k++)  printf("%02X ", CipherKey[k]);
    		AddRoundKey(PlainText2, CipherKey);/*轮密钥加*/
    		for (k = 0; k < 16; k++)  PlainText[k] = PlainText2[k];
    		printf("\n当前明文加密之后为:");
    		for (k = 0; k < 16; k++)  printf("%02X ", PlainText2[k]);
    		printf("\n");
    	}
    	printf("\n最后一次循环:\n");
    	SubBytes(PlainText, PlainText1, 16);
    	ShiftRows(PlainText1);
    	ExtendCipherKey(CipherKey_word + 4 * i, i);
    	for (k = 0; k < 4; WordToChar(CipherKey_word[k + 4 * (i + 1)], CipherKey + 4 * k), k++);
    	printf("此时的子密钥为:     ");
    	for (k = 0; k < 16; k++)  printf("%02X ", CipherKey[k]);
    	AddRoundKey(PlainText1, CipherKey);
    	printf("\n\n最终AES加密后的密文为:");
    	for (i = 0; i < 16; i++)  printf("%02X ", PlainText1[i]);
    	printf("\n\n**************开始解密***************");
    	AddRoundKey(PlainText1, CipherKey);
    	for (i = 0; i < 9; i++)
    	{
    		printf("\n第%d次循环:", i + 1);
    		SubBytesRe(PlainText1, PlainText, 16);/*S盒置换*/
    		for (k = 0; k < 4; WordToChar(CipherKey_word[k + 40 - 4 * (i + 1)], CipherKey + 4 * k), k++);/*子密钥生成*/
    		ShiftRowsRe(PlainText);/*行移位逆*/
    		AddRoundKey(PlainText, CipherKey);/*轮密钥加*/
    		MixColumnsRe(PlainText);/*列混淆逆运算*/
    		for (k = 0; k < 16; PlainText1[k] = PlainText[k], k++);
    		printf("\n当前密文解密之后为:");
    		for (k = 0; k < 16; k++)printf("%02X ", PlainText[k]);
    		printf("\n");
    	}
    	printf("\n最后一次循环:");
    	ShiftRowsRe(PlainText);/*行移位逆*/
    	SubBytesRe(PlainText, PlainText1, 16);/*S盒置换*/
    	AddRoundKey(PlainText1, CipherKey1);
    	printf("\n最终AES解密后的明文为:");
    	for (i = 0; i < 16; i++)  printf("%02X ", PlainText1[i]);
    	printf("\n");
    	system("pause");
    }
    
    //int main()
    //{
     //   std::cout << "Hello World!\n";
    //}
    
    // 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
    // 调试程序: F5 或调试 >“开始调试”菜单
    
    // 入门使用技巧: 
    //   1. 使用解决方案资源管理器窗口添加/管理文件
    //   2. 使用团队资源管理器窗口连接到源代码管理
    //   3. 使用输出窗口查看生成输出和其他消息
    //   4. 使用错误列表窗口查看错误
    //   5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
    //   6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
    
    

    三、结果:

    VS中加密后的结果:6B 88 8F D8 67 48 C2 CF 1E D6 2E ED 49 FE D2 1B;

    网站中加密后结果:6B 88 8F D8 67 48 C2 CF 1E D6 2E ED 49 FE D2 1B。证明结果正确,达到了目的。

    展开全文
  • C语言实现AES加密解密

    2017-06-26 10:57:27
    实现C语言AES加密解密。包含各种加密模式,ECB、CBC
  • aes.c aes.h test.c 本人实测可以使用,基于C的AES算法代码,希望能帮到有需要的。以下是相关模式调用的函数。 void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key); void AES_init_ctx_iv(struct AES_ctx...
  • AES加密解密C语言实现)

    千次阅读 2021-04-27 17:40:32
    。。 测试明文: 0x32,0x43,0xf6,0xa8,0x88,0x5a,0x30,0x8d,0x31,0x31,0x98,0xa2,0xe0,0x37,0x07,0x34 测试密钥: 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c ...

    这几天在做学校的密码学报告,贴一下源代码。。。

    测试明文:

    0x32,0x43,0xf6,0xa8,0x88,0x5a,0x30,0x8d,0x31,0x31,0x98,0xa2,0xe0,0x37,0x07,0x34 
    

    测试密钥:

    0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c 
    

    原创源码

    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    void AddRoundKey(unsigned char *plaintext, unsigned char * CipherKey)/*轮密钥加*/
    {
    	for (int j = 0; j < 16; j++)  plaintext[j] = plaintext[j] ^ CipherKey[j];
    }
    void SubBytes(unsigned char *plaintext, unsigned char *plaintextencrypt, int count)/*S盒置换*/
    {
    	unsigned int row, column;
    	unsigned char Sbox[16][16] = {
    		      /* 0     1     2     3     4     5     6     7     8     9     a     b     c     d     e     f */
    		/*0*/{ 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76 },
    		/*1*/{ 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0 },
    		/*2*/{ 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15 },
    		/*3*/{ 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75 },
    		/*4*/{ 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84 },
    		/*5*/{ 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf },
    		/*6*/{ 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8 },
    		/*7*/{ 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2 },
    		/*8*/{ 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73 },
    		/*9*/{ 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb },
    		/*a*/{ 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79 },
    		/*b*/{ 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08 },
    		/*c*/{ 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a },
    		/*d*/{ 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e },
    		/*e*/{ 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf },
    		/*f*/{ 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 }
    	};// 填充Sbox矩阵
    	for (int i = 0; i < count; i++)
    	{
    		row = (plaintext[i] & 0xF0) >> 4;
    		column = plaintext[i] & 0x0F;
    		plaintextencrypt[i] = Sbox[row][column];
    	}
    }
    void SubBytesRe(unsigned char *plaintext, unsigned char *plaintextencrypt, int count)/*S盒逆置换*/
    {
    	unsigned int row, column;
    	unsigned char Sbox[16][16] = {
    		/* 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}
    	};	// 填充Sbox矩阵
    	for (int i = 0; i < count; i++)
    	{
    		row = (plaintext[i] & 0xF0) >> 4;
    		column = plaintext[i] & 0x0F;
    		plaintextencrypt[i] = Sbox[row][column];
    	}
    }
    void ShiftRowsRe(unsigned char *plaintextencrypt)/*行移位的逆*/
    {
    	unsigned char temp = 0;
    	for (int i = 0; i < 4; i++)//第i行
    	{
    		for (int j = 0; j < 4 - i; j++)//第j次左移
    		{
    			temp = plaintextencrypt[i];
    			for (int k = 0; k < 4; k++)
    				plaintextencrypt[i + 4 * k] = plaintextencrypt[i + 4 * (k + 1)];
    			plaintextencrypt[i + 12] = temp;
    		}
    	}
    }
    void ShiftRows(unsigned char *plaintextencrypt)/*行移位*/
    {
    	unsigned char temp = 0;
    	for (int i = 0; i < 4; i++)//第i行
    	{
    		for (int j = 0; j < i; j++)//第j次左移
    		{
    			temp = plaintextencrypt[i];
    			for (int k = 0; k < 4; k++)
    				plaintextencrypt[i + 4 * k] = plaintextencrypt[i + 4 * (k + 1)];
    			plaintextencrypt[i + 12] = temp;
    		}
    	}
    }
    unsigned char Mult2(unsigned char num)/*列混淆*/
    {
    	unsigned char temp = num << 1;
    	if ((num >> 7) & 0x01)
    		temp = temp ^ 27;
    	return temp;
    }
    unsigned char Mult3(unsigned char num)
    {
    	return Mult2(num) ^ num;
    }
    void MixColumns(unsigned char *plaintextencrypt, unsigned char *plaintextcrypt)
    {
    	int i;
    	for (i = 0; i < 4; i++)
    		plaintextcrypt[4 * i] = Mult2(plaintextencrypt[4 * i]) ^ Mult3(plaintextencrypt[4 * i + 1]) ^ plaintextencrypt[4 * i + 2] ^ plaintextencrypt[4 * i + 3];
    	for (i = 0; i < 4; i++)
    		plaintextcrypt[4 * i + 1] = plaintextencrypt[4 * i] ^ Mult2(plaintextencrypt[4 * i + 1]) ^ Mult3(plaintextencrypt[4 * i + 2]) ^ plaintextencrypt[4 * i + 3];
    	for (i = 0; i < 4; i++)
    		plaintextcrypt[4 * i + 2] = plaintextencrypt[4 * i] ^ plaintextencrypt[4 * i + 1] ^ Mult2(plaintextencrypt[4 * i + 2]) ^ Mult3(plaintextencrypt[4 * i + 3]);
    	for (i = 0; i < 4; i++)
    		plaintextcrypt[4 * i + 3] = Mult3(plaintextencrypt[4 * i]) ^ plaintextencrypt[4 * i + 1] ^ plaintextencrypt[4 * i + 2] ^ Mult2(plaintextencrypt[4 * i + 3]);
    }
    /*逆列混淆*/ 
    #define xtime(x)   ((x<<1) ^ (((x>>7) & 1) * 0x1b))
    #define Multiply(x,y) (((y & 1) * x) ^ ((y>>1 & 1) * xtime(x)) ^ ((y>>2 & 1) * xtime(xtime(x))) ^ ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ ((y>>4 & 1) * xtime(xtime(xtime(xtime(x))))))
    void MixColumnsRe(unsigned char *state)
    {
    	
    	unsigned char a, b, c, d;
    	for (int i = 0; i < 4; i++)
    	{
    		a = state[4*i];
    		b = state[4*i+1];
    		c = state[4*i+2];
    		d = state[4*i+3];
    		state[4 * i] = Multiply(a, 0x0e) ^ Multiply(b, 0x0b) ^ Multiply(c, 0x0d) ^ Multiply(d, 0x09);
    		state[4 * i + 1] = Multiply(a, 0x09) ^ Multiply(b, 0x0e) ^ Multiply(c, 0x0b) ^ Multiply(d, 0x0d);
    		state[4 * i + 2] = Multiply(a, 0x0d) ^ Multiply(b, 0x09) ^ Multiply(c, 0x0e) ^ Multiply(d, 0x0b);
    		state[4 * i + 3] = Multiply(a, 0x0b) ^ Multiply(b, 0x0d) ^ Multiply(c, 0x09) ^ Multiply(d, 0x0e);
    	}
    }
    int CharToWord(unsigned char *character, int first)/*字节转字*/
    {
    	return (((int)character[first] & 0x000000ff) << 24) | (((int)character[first + 1] & 0x000000ff) << 16) | (((int)character[first + 2] & 0x000000ff) << 8) | ((int)character[first + 3] & 0x000000ff);
    }
    void WordToChar(unsigned int word, unsigned char *character)/*字转字节*/
    {
    	for (int i = 0; i < 4; character[i++] = (word >> (8 * (3 - i))) & 0xFF);
    }
    void ExtendCipherKey(unsigned int *CipherKey_word, int round)/*密钥扩展*/
    {
    	unsigned char CipherKeyChar[4] = { 0 },CipherKeyCharEncrypt[4] = { 0 };
    	unsigned int Rcon[10] = { 0x01000000,0x02000000,0x04000000,0x08000000,0x10000000,0x20000000,0x40000000,0x80000000,0x1B000000,0x36000000 };	//轮常量
    	for (int i = 4; i < 8; i++)
    	{
    		if (!(i % 4))
    		{
    			WordToChar((CipherKey_word[i - 1] >> 24) | (CipherKey_word[i - 1] << 8), CipherKeyChar);
    			SubBytes(CipherKeyChar, CipherKeyCharEncrypt, 4);
    			CipherKey_word[i] = CipherKey_word[i - 4] ^ CharToWord(CipherKeyCharEncrypt, 0) ^ Rcon[round];
    		}
    		else
    			CipherKey_word[i] = CipherKey_word[i - 4] ^ CipherKey_word[i - 1];
    	}
    }
    void main()
    {
    	printf("**************AES加解密***************\n");
    	int i = 0, k;
    	unsigned char PlainText[16] = { 0x32,0x43,0xf6,0xa8,0x88,0x5a,0x30,0x8d,0x31,0x31,0x98,0xa2,0xe0,0x37,0x07,0x34 },
    		CipherKey[16] = { 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c },
    		CipherKey1[16] = { 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c },
    		PlainText1[16] = { 0 },
    		PlainText2[16] = { 0 };
    	unsigned int CipherKey_word[44] = { 0 };
    	for (i = 0; i < 4; CipherKey_word[i++] = CharToWord(CipherKey, 4 * i));
    	printf("密钥:");
    	for (k = 0; k < 16; k++) printf("%2X ", CipherKey[k]);
    	printf("\n明文:");
    	for (k = 0; k < 16; k++) printf("%02X ", PlainText[k]);
    	printf("\n**************开始加密****************");
    	AddRoundKey(PlainText, CipherKey);
    	for (i = 0; i < 9; i++)
    	{
    		printf("\n第%d轮循环:\n", i + 1);
    		SubBytes(PlainText, PlainText1, 16);/*S盒置换*/
    		ShiftRows(PlainText1);	/*行移位*/
    		MixColumns(PlainText1, PlainText2);	/*列混淆*/
    		ExtendCipherKey(CipherKey_word + 4 * i, i);/*子密钥生成*/
    		for (k = 0; k < 4; k++)  WordToChar(CipherKey_word[k + 4 * (i + 1)], CipherKey + 4 * k);
    		printf("此时的子密钥为:    ");
    		for (k = 0; k < 16; k++)  printf("%02X ", CipherKey[k]);
    		AddRoundKey(PlainText2, CipherKey);/*轮密钥加*/
    		for (k = 0; k < 16; k++)  PlainText[k] = PlainText2[k];
    		printf("\n当前明文加密之后为:");
    		for (k = 0; k < 16; k++)  printf("%02X ", PlainText2[k]);
    		printf("\n");
    	}
    	printf("\n最后一次循环:\n");
    	SubBytes(PlainText, PlainText1, 16);
    	ShiftRows(PlainText1);
    	ExtendCipherKey(CipherKey_word + 4 * i, i);
    	for (k = 0; k < 4;WordToChar(CipherKey_word[k + 4 * (i + 1)], CipherKey + 4 * k), k++);
    	printf("此时的子密钥为:     ");
    	for (k = 0; k < 16; k++)  printf("%02X ", CipherKey[k]);
    	AddRoundKey(PlainText1, CipherKey);
    	printf("\n\n最终AES加密后的密文为:");
    	for (i = 0; i < 16; i++)  printf("%02X ", PlainText1[i]);
    	printf("\n\n**************开始解密***************");
    	AddRoundKey(PlainText1, CipherKey);
    	for (i = 0; i < 9; i++)
    	{
    		printf("\n第%d次循环:", i + 1);
    	    SubBytesRe(PlainText1, PlainText, 16);/*S盒置换*/
    		for (k = 0; k < 4; WordToChar(CipherKey_word[k + 40 - 4 * (i + 1)], CipherKey + 4 * k),k++);/*子密钥生成*/
    		ShiftRowsRe(PlainText);/*行移位逆*/
    		AddRoundKey(PlainText, CipherKey);/*轮密钥加*/
    		MixColumnsRe(PlainText);/*列混淆逆运算*/
    		for (k = 0; k < 16;PlainText1[k] = PlainText[k],k++);
    		printf("\n当前密文解密之后为:");
    		for (k = 0; k < 16; k++)printf("%02X ", PlainText[k]);
    		printf("\n");
    	}
    	printf("\n最后一次循环:");
    	ShiftRowsRe(PlainText);/*行移位逆*/
    	SubBytesRe(PlainText, PlainText1, 16);/*S盒置换*/
    	AddRoundKey(PlainText1, CipherKey1);
    	printf("\n最终AES解密后的明文为:");
    	for (i = 0; i < 16; i++)  printf("%02X ", PlainText1[i]);
    	printf("\n");
    	system("pause");
    }
    

    运行效果:

    • 加密
      在这里插入图片描述
    • 解密:
      在这里插入图片描述
    展开全文
  • C语言实现的AES加密解密算法

    千次下载 热门讨论 2013-08-26 10:36:17
    C语言实现的AES加密解密算法,用C语言实现的AES加密解密算法
  • 支持 CFB, OFB, CBC, ECB 模式。 其中:AES.c 中为算法的实现代码;Test.c为测试程序,加密解密都有
  • 基于c语言实现AES128_ECB、AES128_CBC两种模式加密,数据使用PKCS7Padding算法对齐,程序已验证
  • C语言实现AES加密解密,CBC模式,密钥32位,向量16位;加密结果同Java一致,但是因补码方式有区别,Java需是NoPadding补码方式;加密内容则需16的倍数;
  • AES加密算法C语言实现,有测试代码,可直接使用,提供了加密和解密两个接口,可直接添加到工程中使用,纯C代码,方便移植
  • AES加密解密C语言源代码 Keil C51 51单片机STM32可用,可用128-Bit 192-Bit 256-Bit 加密解密;用于数据传输,提高安全性
  • AES加密算法c语言实现代码
  • 我从网上寻找了很多aes算法,进行了整合和更改,我提供的这个aes算法可以在单片机和PC上运行,可以进行连续加解密,这是比别人做的好的地方。128位密钥,10轮s和变换。
  • C语言实现AES加密解密,加密结果与Java中AES的ECB模式,NoPadding补码方式结果一致;支持中文加密,但是限定密钥32位,加密内容16位;若需要变化可作为参考
  • AES加密解密算法C语言代码实现

    千次阅读 2019-09-13 20:37:40
     //解密函数; void RotByte(int * temp); //!>移位函数(作为Key_Schedule函数的辅助函数) void Key_Schedule(int * cphkey,int RoundKey[10][16]); //生成轮密钥的函数; void InvSubBytes(int * ex...
     /*------------------------------------
      This program demonstrates how to use AES to encrypt/decrypt a 
       block (128 bits) of data.
      Editer:NeilJones
      Date:2014/11/10
     -----------------------------------*/
    #include<stdio.h>
    #include<string.h>
    int  RoundKey[10][16];                      //!>  轮密钥 ;
    int  CipherKey[16]={0x2b,0x28,0xab,0x09,   // !>(例子)加密密钥;
         0x7e,0xae,0xf7,0xcf,
         0x15,0xd2,0x15,0x4f,
         0x16,0xa6,0x88,0x3c};
    int  State[16]={0x32,0x88,0x31,0xe0,     // !>(例子)明文;
        0x43,0x5a,0x31,0x37,
        0xf6,0x30,0x98,0x07,
        0xa8,0x8d,0xa2,0x34};
    int sbox[256] = {
        //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, //0
        0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, //1
        0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, //2
        0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, //3
        0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, //4
        0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, //5
        0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, //6
        0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, //7
        0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, //8
        0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, //9
        0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, //A
        0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, //B
        0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, //C
        0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, //D
        0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, //E
        0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 }; //F
    
    int rsbox[256] = {
        //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 };
     
    int Rcon[40] = {
         0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
     }; 
    
    /*------------------------------
              函数声明部分;
    --------------------------------*/
    void AddRoundKey(int * ex_state, int * RoundKey);    //密钥加函数;
    void SubBytes(int * ex_state, int * S_box);      //字节代换函数;
    void ShiftRows(int * ex_state);         //行移位函数;
    void MixColumns(int * ex_state);        //列混合函数;
    void Round(int * ex_state, int * S_box,int * RoundKey);   //轮函数;
    void Final_Round(int * ex_state, int * S_box,int * RoundKey); //最后轮函数;
    void Aes_Encrypt(int * ex_state, int * S_box,int *cphkey,int RoundKey[10][16]); //加密函数;
    void Aes_Decrypt(int * ex_state, int * S_box,int *cphkey,int RoundKey[10][16]); //解密函数;
    void RotByte(int * temp);                //!>移位函数(作为Key_Schedule函数的辅助函数)
    void Key_Schedule(int * cphkey,int RoundKey[10][16]);   //生成轮密钥的函数;
    void InvSubBytes(int * ex_state, int * RS_box);     //逆字节代换函数;
    void InvShiftRows(int * ex_state);        //逆行移位函数;
    void InvMixColumns(int * ex_state);        //逆列混合函数;
    void InvRound(int * ex_state, int * RS_box,int * RoundKey);  //逆轮函数;
    void InvFinal_Round(int * ex_state, int * RS_box,int * RoundKey);//逆最后轮函数;
    /*---------------------------------*/
     
    /*-----------------------------------
       函数功能具体实现
    ------------------------------------*/
    void SubBytes(int * ex_state, int * S_box)                //字节代换函数;
    {
     int i,k;
     for(i=0;i<16;i++)
     {
      k=ex_state[i];
      ex_state[i]=S_box[k];
     }
    }
    void ShiftRows(int * ex_state)     //行移位函数;
    {
     int k,temp;
     /*--------------*第二行(不好意思,原谅我用的方法比较笨 0.0 );
     temp=ex_state[4];
     ex_state[4]=ex_state[5];
     ex_state[5]=ex_state[6];
     ex_state[6]=ex_state[7];
     ex_state[7]=temp;
     /*-------------------*第三行;
     temp=ex_state[8];
     ex_state[8]=ex_state[10];
     ex_state[10]=temp;
     temp=ex_state[9];
     ex_state[9]=ex_state[11];
     ex_state[11]=temp;
     /*-------------------*///第四行;这里的话就需要两个缓存来辅助了;
        temp=ex_state[13];
     ex_state[13]=ex_state[12];
     k=ex_state[14];
     ex_state[14]=temp;
     temp=ex_state[15];
     ex_state[15]=k;
     ex_state[12]=temp;
     /*--------------------*/ 
    }
    void AddRoundKey(int * ex_state, int * RoundKey)        //密钥加函数;
    {
     int i;
     for(i=0;i<4;i++)
     {
      ex_state[i]^=RoundKey[i];
      ex_state[i+4]^=RoundKey[i+4];
      ex_state[i+8]^=RoundKey[i+8];
      ex_state[i+12]^=RoundKey[i+12];
     }
    }
    /*----------------------------------------------------*以下三个函数都属于MixColumns函数范畴;
    int xtime(int n)   //!> 用来把这个数*0x02;
    {
     int temp;
     temp=n<<1;
     if(n&0x80)
     {
      temp=temp^0x1b;
     }
     return temp;
    }
     
    int mixcolumn(int m,int n)
    {
     int temp;
     for(temp=m,m=0;n;n=n>>1)
     {
      if(n&1)
      {
       m=m^temp;
      }
      temp=xtime(temp);
     }
     return m&0xff;
    }
    void MixColumns(int * ex_state)     //列混合函数;还需要一个mix()函数来帮助这个函数;
    {
     int i;
     int p_state[16]={0};             //定义一个新的p-state来记录输入的ex_state,以免ex_state发生变化时,p-state还能使用原先的那个ex_state;
     for(i=0;i<16;i++)
      p_state[i]=ex_state[i];
     for(i=0;i<16;i++)
     {
      if(i>=0&&i<4)
      {
       ex_state[i]=mixcolumn(ex_state[i],0x02)^mixcolumn(ex_state[i+1*4],0x03)^p_state[i+2*4]^p_state[i+3*4];
      }
      else if(i>=4&&i<8)
      {
       ex_state[i]=p_state[i-4]^mixcolumn(ex_state[i],0x02)^mixcolumn(ex_state[i+1*4],0x03)^p_state[i+2*4]; 
      }
      else if(i>=8&&i<12)
      {
       ex_state[i]=p_state[i-2*4]^p_state[i-1*4]^mixcolumn(ex_state[i],0x02)^mixcolumn(ex_state[i+1*4],0x03);
      }
      else if(i>=12&&i<16)
      {
       ex_state[i]=mixcolumn(p_state[i-3*4],0x03)^p_state[i-2*4]^p_state[i-1*4]^mixcolumn(ex_state[i],0x02);
      }
     } 
    }
    void Round(int * ex_state, int * S_box,int * RoundKey)     //轮函数;
    {
     SubBytes(ex_state,S_box);
     ShiftRows(ex_state);
     MixColumns(ex_state);
     AddRoundKey(ex_state,RoundKey); 
    }
    void Final_Round(int * ex_state, int * S_box,int * RoundKey)     //最后轮函数;
    {
     SubBytes(ex_state,S_box);
     ShiftRows(ex_state);
     AddRoundKey(ex_state,RoundKey); 
    }
    void RotByte(int * temp)     //!>移位函数(作为Key_Schedule函数的辅助函数)
    {
     int j=0;
     j=temp[0];         //!>对它进行移位;
     temp[0]=temp[1];
     temp[1]=temp[2];
     temp[2]=temp[3];
     temp[3]=j;
    }
    void Key_Schedule(int * cphkey,int RoundKey[10][16],int * S_box,int * Rcon) //生成轮密钥的函数;
    {
     int i,j,m,n;
     int temp_first[4]={0};    
     int temp_last[4]={0};   
     for(i=0;i<4;i++)          //!>把最后一列的数据赋给temp_last数组;
     {
      temp_last[i]=cphkey[i*4+3];
     }
     RotByte(temp_last); //!>移位
     for(i=0;i<4;i++)   //!>进行S_box转换;
     {
      temp_last[i]=S_box[temp_last[i]];
     }
     for(i=0;i<4;i++)    //!>把第一列的数据赋给temp_first数组;
     {
      temp_first[i]=cphkey[i*4];
     }
     for(i=0;i<4;i++)    //!>生成第一轮的RoundKey;
     {
      RoundKey[0][i*4]=temp_first[i]^temp_last[i]^Rcon[i*10];  //!>注意Rcon为*10;
     }
     for(i=1;i<4;i++)
     {
      for(j=0;j<4;j++)
      {
       RoundKey[0][j*4+i]=RoundKey[0][j*4+i-1]^cphkey[j*4+i];
      }
     }
     for(i=1;i<10;i++)
     {
      for(m=0;m<4;m++)          //!>把最后一列的数据赋给temp_last数组;
      {
       temp_last[m]=RoundKey[i-1][m*4+3];
      }
      RotByte(temp_last); //!>移位;
      for(m=0;m<4;m++)   //!>进行S_box转换;
      {
       temp_last[m]=S_box[temp_last[m]];
      }
      for(m=0;m<4;m++)  
      {
       RoundKey[i][m*4]=RoundKey[i-1][m*4]^temp_last[m]^Rcon[m*10+i];
      }
      for(j=1;j<4;j++)
      {
       for(n=0;n<4;n++)
       {
        RoundKey[i][n*4+j]=RoundKey[i][n*4+j-1]^RoundKey[i-1][n*4+j];
       }
      }
     }
    }
    void Aes_Encrypt(int * ex_state, int * S_box,int *cphkey,int RoundKey[10][16]) //加密函数;
    {
     int i,j;
     Key_Schedule(cphkey,RoundKey,S_box,Rcon);
     AddRoundKey(ex_state, cphkey);
     for(i=0;i<9;i++)
     {
      Round(ex_state,S_box,RoundKey[i]);
      /*printf("密钥的中间状态为:\n");
      for(j=0;j<16;j++)       //!>想把中间状态输出来,就把for的注释去掉;
      {
       printf("%x ",ex_state[j]);
      }
      printf("\n");*/
     }
     Final_Round(ex_state, S_box,RoundKey[9]);
    }
    /*--------------------------------------*///以下就是解密函数了;
    void InvSubBytes(int * ex_state, int * RS_box)    //逆字节代换函数;
    {
     int i,k;
     for(i=0;i<16;i++)
     {
      k=ex_state[i];
      ex_state[i]=RS_box[k];
     }
    }
    void InvShiftRows(int * ex_state)   //逆行移位函数;
    {
     int k,temp;
     /*--------------*第二行移三位(不好意思,原谅我用的方法比较笨 0.0 );
     temp=ex_state[5];
     ex_state[5]=ex_state[4];
     k=ex_state[6];
     ex_state[6]=temp;
     temp=ex_state[7];
     ex_state[7]=k;
     ex_state[4]=temp;
     /*-------------------*第三行移动两位;
     temp=ex_state[8];
     ex_state[8]=ex_state[10];
     ex_state[10]=temp;
     temp=ex_state[9];
     ex_state[9]=ex_state[11];
     ex_state[11]=temp;
     /*-------------------*///第四行移动一位;这里的话就需要两个缓存来辅助了;
        temp=ex_state[12];
     ex_state[12]=ex_state[13];
     ex_state[13]=ex_state[14];
     ex_state[14]=ex_state[15];
     ex_state[15]=temp;
     /*--------------------*/
     
    }
    void InvMixColumns(int * ex_state)     //逆列混合函数;
    {
     int i;
     int p_state[16]={0};             //定义一个新的p-state来记录输入的ex_state,以免ex_state发生变化时,p-state还能使用原先的那个ex_state;
     for(i=0;i<16;i++)
      p_state[i]=ex_state[i];
     for(i=0;i<16;i++)
     {
      if(i>=0&&i<4)
      {
       ex_state[i]=mixcolumn(ex_state[i],0x0e)^mixcolumn(ex_state[i+1*4],0x0b)^mixcolumn(p_state[i+2*4],0x0d)^mixcolumn(p_state[i+3*4],0x09);
      }
      else if(i>=4&&i<8)
      {
       ex_state[i]=mixcolumn(p_state[i-4],0x09)^mixcolumn(ex_state[i],0x0e)^mixcolumn(ex_state[i+1*4],0x0b)^mixcolumn(p_state[i+2*4],0x0d); 
      }
      else if(i>=8&&i<12)
      {
       ex_state[i]=mixcolumn(p_state[i-2*4],0x0d)^mixcolumn(p_state[i-1*4],0x09)^mixcolumn(ex_state[i],0x0e)^mixcolumn(ex_state[i+1*4],0x0b);
      }
      else if(i>=12&&i<16)
      {
       ex_state[i]=mixcolumn(p_state[i-3*4],0x0b)^mixcolumn(p_state[i-2*4],0x0d)^mixcolumn(p_state[i-1*4],0x09)^mixcolumn(ex_state[i],0x0e);
      }
     } 
    }
    void InvRound(int * ex_state, int * RS_box,int * RoundKey)  //逆轮函数;
    {  
     InvSubBytes(ex_state, RS_box);
     InvShiftRows(ex_state);
     InvMixColumns(ex_state);
     AddRoundKey(ex_state,RoundKey); 
    }
    void InvFinal_Round(int * ex_state, int * RS_box,int * RoundKey) //逆最后轮函数;
    {
     InvSubBytes(ex_state, RS_box);
     InvShiftRows(ex_state);
     AddRoundKey(ex_state,RoundKey); 
    }
    void Aes_Decrypt(int * ex_state, int * RS_box,int *cphkey,int RoundKey[10][16]) //解密函数;
    {
     int i;
     AddRoundKey(ex_state,RoundKey[9]);
     for(i=8;i>=0;i--)
     {
      InvMixColumns(RoundKey[i]);
      InvRound(ex_state,RS_box,RoundKey[i]);
     }
     InvFinal_Round(ex_state,RS_box,cphkey);
    }
    /*----------------------------------------
        主函数部分
      ----------------------------------------*/
    int main()
    {
     int i,j;
     printf("初始的密钥为:\n");
     for(i=0;i<16;i++)
     {
      printf("%x ",CipherKey[i]);
     }
     printf("\n\n");
     printf("绑定的明文为:\n");   //!>如果想更改明文或密钥,请找到改程序最上方的CipherKey和State进行更改;
     for(i=0;i<16;i++)
     {
      printf("%x ",State[i]);
     }
     printf("\n\n");
     Aes_Encrypt(State, sbox, CipherKey, RoundKey); //加密函数;
     printf("加密后的密文为:\n");
      for(j=0;j<16;j++)
      {
       printf("%x ",State[j]);
      }
      printf("\n\n");
     Aes_Decrypt(State, rsbox, CipherKey,RoundKey);
     printf("解密后的明文为:\n");
     for(i=0;i<16;i++)
     {
      printf("%x ",State[i]); 
     }
     printf("\n\n");
     return 0;
    }
    /*-----------------------------------------*/

    转载于:https://my.oschina.net/u/2276409/blog/347111

    展开全文
  • AES加密算法c语言实现代码#include "stdio.h"#include "memory.h"#include "time.h"#include "stdlib.h"……#include "stdio.h"#include "memory.h"#include "time.h"#include "stdlib.h"#define PLAIN_FILE_OPEN_...
  • C语言实现AES解密算法

    千次阅读 2021-11-19 19:28:55
    C语言实现AES解密算法AES解密 AES解密 #include <stdio.h> #include <stdint.h> #include <memory.h> /********************************************************************************...
  • 本文实例讲述了基于C语言实现的aes256加密算法。分享给大家供大家参考,具体如下: aes256.h: #ifndef uint8_t #define uint8_t unsigned char #endif #ifdef __cplusplus extern C { #endif typedef struct { ...
  • c/c++与java互通 AES加密解密; 只使用基本string,数组运算实现加密算法;不依赖其它加密lib
  • AES加密解密。 实现Java服务器与客户端的通信c AES算法的实现有四种,例如CBC / ECB / OFB / CFB,四种Java和C实现。 AES算法和末尾的填充(padding),有三种Java填充NoPadding / PKCS5Padding的支持方式,并且...
  • C语言实现AES-128对称加解密算法。编译环境:VS2010。请参考我的博客https://blog.csdn.net/u013073067/article/details/86529111 分析代码
  • 该文件是AES加密算法C语言的实现,在STM32平台验证通过,ECB加密方式,方便移植。
  • stm32加解密工具类,直接可用,采用AES 对称加密解密 AES_Decrypt(expressText,cipherText , aesKey);
  • AES加密c语言实现

    万次阅读 多人点赞 2018-06-14 16:52:43
    /*s盒矩阵:The AES Substitution Table*/// 256 位的密匙256 位支持长度为32 个字符 static const unsigned char sbox[256]={ //static:内部变量 const:只读,不可变常量 0x63,0x7c,0x77,0x7b...
  • 本文实例讲述了C语言使用openSSL库AES模块实现加密功能。分享给大家供大家参考,具体如下: 概述 在密码学里面一共有3中分类: 1.对称加密/解密 对称加密比较常见的有DES/AES加密方和解密方都持有相同的密钥。对称...
  • 基于C语言AES加密算法实现

    千次阅读 2021-12-04 21:14:13
    本文只说明AES算法的使用

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 1,854
精华内容 741
关键字:

aes加密解密c语言