AES128加密算法实现(C语言:ECB加密模式实现)

AES简介
高级加密标准AES(Advanced Encryption Standard)是一种常见的对称加密算法.
详细介绍如下链接:
https://blog.youkuaiyun.com/qq_28205153/article/details/55798628
在这里插入图片描述
其余基本概念,我这不再加以描述了,主要攻克一些技术难点:
实现AES算法主要包括以下学习步骤:
1、GF(2^8)域上的多项式运算
2、扩展的欧几里德算法
3、生成S盒
4、生成逆S盒
5、S盒置换
6、行移位
7、列混合
8、生成秘钥
9、循环加密
详细学习路径如下:
https://blog.youkuaiyun.com/u013605322/article/details/83443612
以上两篇链接,你就可以对AES算法有个大致的了解

算法流程如下:
在这里插入图片描述
具体实现如下:
加密接口函数:

// 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};

    if (NULL == key || NULL == pt || NULL == ct){
        printf("param err.\n");
        return -1;
    }

    if (keyLen > 16){
        printf("keyLen must be 16.\n");
        return -1;
    }

    if (len % BLOCKSIZE){
        printf("inLen is invalid.\n");
        return -1;
    }

    memcpy(actualKey, key, keyLen);
    keyExpansion(actualKey, 16, &aesKey);  // 秘钥扩展

	// 使用ECB模式循环加密多个分组长度的数据
    for (int i = 0; i < len; i += BLOCKSIZE) {
		// 把16字节的明文转换为4x4状态矩阵来进行处理
        loadStateArray(state, pt);
        // 轮秘钥加
        addRoundKey(state, rk);

        for (int 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;
}

秘钥扩展函数:

//秘钥扩展
int keyExpansion(const uint8_t *key, uint32_t keyLen, AesKey *aesKey) {

    if (NULL == key || NULL == aesKey){
        printf("keyExpansion param is NULL\n");
        return -1;
    }

    if (keyLen != 16){
        printf("keyExpansion keyLen = %d, Not support.\n", keyLen);
        return -1;
    }

    uint32_t *w = aesKey->eK;  //加密秘钥
    uint32_t *v = aesKey->dK;  //解密秘钥

    /* keyLen is 16 Bytes, generate uint32_t W[44]. */

    /* W[0-3] */
    for (int i = 0; i < 4; ++i) {
        LOAD32H(w[i], key + 4*i);
    }

    /* W[4-43] */
    for (int 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 (int j = 0; j < 11; ++j) {

        for (int i = 0; i < 4; ++i) {
            v[i] = w[i];
        }
        w -= 4;
        v += 4;
    }
    return 0;
}

使用ECB模式循环加密多个分组长度的数据

/* copy in[16] to state[4][4] */
int loadStateArray(uint8_t (*state)[4], const uint8_t *in) {
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            state[j][i] = *in++;
        }
    }
    return 0;
}

轮秘钥加

// 轮秘钥加
int addRoundKey(uint8_t (*state)[4], const uint32_t *key) {
    uint8_t k[4][4];

    /* i: row, j: col */
    for (int i = 0; i < 4; ++i) {
        for (int 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;
}
// 从uint32_t x中提取从低位开始的第n个字节
#define BYTE(x, n) (((x) >> (8 * (n))) & 0xff)

字节替换,行移位,列混合

//字节替换
int subBytes(uint8_t (*state)[4]) {
    /* i: row, j: col */
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            state[i][j] = S[state[i][j]]; //直接使用原始字节作为S盒数据下标
        }
    }

    return 0;
}
//行移位
int shiftRows(uint8_t (*state)[4]) {
    uint32_t block[4] = {0};

    /* i: row */
    for (int 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 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}};

    /* copy state[4][4] to tmp[4][4] */
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j){
            tmp[i][j] = state[i][j];
        }
    }

    for (int i = 0; i < 4; ++i) {
        for (int 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;
}

把4X4状态矩阵转换为uint8_t一维数组输出保存

/* copy state[4][4] to out[16] */
int storeStateArray(uint8_t (*state)[4], uint8_t *out) {
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            *out++ = state[j][i];
        }
    }
    return 0;
}

解密即为上面解密的逆运算:
解密函数:

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};

    if (NULL == key || NULL == ct || NULL == pt){
        printf("param err.\n");
        return -1;
    }

    if (keyLen > 16){
        printf("keyLen must be 16.\n");
        return -1;
    }

    if (len % BLOCKSIZE){
        printf("inLen is invalid.\n");
        return -1;
    }

    memcpy(actualKey, key, keyLen);
    keyExpansion(actualKey, 16, &aesKey);  //秘钥扩展,同加密

    for (int i = 0; i < len; i += BLOCKSIZE) {
        // 把16字节的密文转换为4x4状态矩阵来进行处理
        loadStateArray(state, ct);
        // 轮秘钥加,同加密
        addRoundKey(state, rk);

        for (int 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;
}
//逆行移位
int invShiftRows(uint8_t (*state)[4]) {
    uint32_t block[4] = {0};

    /* i: row */
    for (int i = 0; i < 4; ++i) {
        LOAD32H(block[i], state[i]);
        block[i] = ROR32(block[i], 8*i);
        STORE32H(block[i], state[i]);
    }

    return 0;
}
//逆字节替换
int invSubBytes(uint8_t (*state)[4]) {
    /* i: row, j: col */
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            state[i][j] = inv_S[state[i][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}};  //使用列混合矩阵的逆矩阵

    /* copy state[4][4] to tmp[4][4] */
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j){
            tmp[i][j] = state[i][j];
        }
    }

    for (int i = 0; i < 4; ++i) {
        for (int 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;
}

罗华域乘法运算的实现

* Galois Field (256) Multiplication of two Bytes */
// 两字节的伽罗华域乘法运算
uint8_t GMul(uint8_t u, uint8_t v) {
    uint8_t p = 0;

    for (int i = 0; i < 8; ++i) {
        if (u & 0x01) {    //
            p ^= v;
        }

        int flag = (v & 0x80);
        v <<= 1;
        if (flag) {
            v ^= 0x1B; /* x^8 + x^4 + x^3 + x + 1 */
        }

        u >>= 1;
    }
    return p;
}

最后测试一下对应的加解密的输出数据和结果

/*
   AES加解密:采用对称分组密码体制
   待加解密字符串:32字节长度字符串明文
   算法模式: ECB (Electronic Code Book , 电子密码本) 模式
   秘钥长度:秘钥长度输入key应为16字节长度,输入长度应该是16字节整倍数,
   秘钥:key[]="1234567890123456"
   秘钥偏移量:None
   补码方式:None
   输出编码:十六进制(Hex)
*/
#include "AES128.h"

int main() 
{
/* 加密步骤
            1.申明秘钥 key2[]
            2.明文  *data
            3.加密  aesEncrypt
*/
//****************************************************************************//
    // 16字节字符串形式秘钥
    const uint8_t key[]="1234567800000000";
    // 32字节长度字符串明文
    const uint8_t *P = (uint8_t*)"WOSHINIANGQINGDEGONGCHENGSHI111";  
    //外部申请输出数据内存,用于存放加密后数据
    uint8_t Encrypted_data[32] = {0};   
    //外部申请输出数据内存,用于存放解密后数据
    uint8_t Decrypted_data[32] = {0}; 
    //加密32字节明文
    aesEncrypt(key, 16, P, Encrypted_data, 32);  //加密
//****************************************************************************//
    printf("\n原始明文:\n%s\n", P);  //原始明文数据
    printHex(Encrypted_data, 32, "加密编码(HEX):");//加密后Hex
/* 解密步骤
                解密函数 aesDecrypt(key, 密钥数据长度, 加密后的数据存放的地址, 用于存放解密后的数据地址, 解密数据的长度)
*/  
//****************************************************************************//
    // 解密32字节密文
    aesDecrypt(key, 16, Encrypted_data, Decrypted_data, 32);  
//****************************************************************************//
    // 打印16进制形式的解密后的明文
    printHex(Decrypted_data, 32, "解密编码(HEX):");  //解密后Hex
    // 因为加密前的数据为可见字符的字符串,打印解密后的明文字符,与加密前明文进行对比
    printf("解密后反编译出原始明文 \n"); 
    for (int i = 0; i < 32; ++i) {
        printf("%c ", Decrypted_data[i]);
    }
    return 0;
}

输出结果如下:
在这里插入图片描述

用C语言实现128AES加密算法,可以运行在JAVA的JNI 中AIS加密算法c语言实现代码 nt cnt for(ent =0: cnt< 8: cnt++) BvtcToBit(*(ch+cnt), bit+(ent<<3)) return /将二进制位串转为长度为8的字符串水 int Bit64ToChar8 (ElemType bitL64, ElemType ch18) int cnt memset(ch, 0, 8) for(ent-0: cnt<8: cnt++i BitToByte(bit+(cnt<<3), ch+cnt) return 0 /*生成子密钥 int DES Make Subkeys(ElemType key _64, ElemType subkeys [16][48]) ElemType temp 56 int cnt DES PCI Transform(key,temp):/*PCI置换* for(cnt=0;cnt<16;cnt+-){*16轮跌代,产生16个子密钥米 DES ROL(tenp, MOVE TIMES[cnt]);循坏左移* DES PC2 Transform(temp, subkeys cnt]);/PC2置换,产生子密钥体 return o /*密钥置換1*/ int DES PCI Transform(ElemType key [64, ElemType tempts[56])t int cnt for(cnt=0: cnt( 56 cnt++) )empts[cnt]= key[ Ilant] r巳turn /*密钥置換2* int DES PC2 Transform(Elem Type key [56], ElemType tempts[48])i t cnt for(ent =0: cnt< 48: cnt+)I )pbts [cnt]= key [PC 2[cnt]] return /*循环左移*/ int DES ROL (Elem Type data[56], int time)t Elem l'ype temp _56 /*保存将要循环栘动到右边的位* memcpy(temp, data, time) memcpy(temg-time, data+28, time) /*前28位移动 (data-28-time, temp, time) /*后28位移动* memcpy(data 28, data+28+time, 28-time memcpy (data-56-time, temp+time, time) return o /*P置换*/ int DES IP) Iransform(Elemlype data[64)[ ElemType temp _64]: for(cnt templet- datalIP Tablelcnt」」 memcpy(data, temp, 64) return o 第3页 AIS加密算法c语言实现代码 /*IP逆置換* int DES IP 1 Transform(ElemType data[64)( int cnt ElemType temp _64 for(cnt =0: cnt 64: cnt+-)i templet」- dataLIP1 Tablelcrt]」 memcpy(data, temp, 64) return o /*扩展置换*/ int DES E Transform(ElemType data[48])( Int cn ElemType temp48」 for(ent-0: cnt 48: cnt-) temp lent= datale Tablelent memcpy( data, temp, 48 return o P置换 int DES P Transform(ElemType data[32])( t ElemType temp_32] for(ent =0; cnt 32; cnt+-) temp ent-datalP Tablel 11 me.mcpy(data, temp, 32) return 0 /水异或* int DES XOR(Elem Type R[48, Elem Type L[48], int count)I int cnt for(cnt-0: cnt< count: cnt++)i RIant]= lent] return 0 /*S盒置换*/ int DES SBOX (Elem Type data[48]) int cnt int line, row, output int curl, cur for(ent=0; cnt( 8: cnt++i curl cnt:6 cur2= cnt<<2 /*计算在S盒中的行与列来 line=(data cur1<<1)+ data[ cur1+5 row=(data[cur1+1]<<3)+(data[cur1+2]<<2) +(data「cur1-31<<1)+data「cur1+41 output s[cnt][line]trow] /*化为2进制*/ data[cur2]=(output&0X08)>>3 data[cur2+1]=(output&0X04)>>2 data (output&0X02)>1 datalcur 2+3= output&0x01 return o 交换 int DES Swap(ElemType left[32], ElemType right [32]) memcpy(temp, left, 32 memcpy(left, right, 32 memcpy (right, temp, 32 return o 第4页 AIS加密算法c语言实现代码 /*加密单个分组 int DES EncryptBlockElem Type plainBlock[8, ElemType subKeys[l6][48, ElemType cipherBlock[8])I ElemT'ype plainTs [54] ElemType copyRight[48] int cnt Char8ToBit64(plainBlock, plairBits) /米初始置换(IP置换)* DES IP Transform(plainBits /*16轮迭代* for(cnt=0: cnt< 16: cnt+-) memcpy(copyRight, plainBits- 32, 32 /*将右半部分进行扩展置换,从32位扩展到48位*/ DES E Trans form(copyRight) /*将右半部分与子密钥进行异或操作 DES XOR (copy Righ 48) /*异或结果进入S盒,输出32位结果*/ DES SBOX (copyRight) /P置换 DES P Transform(copyRight) /*将明文左半部分与右半部分进行异或* DES XOR (plainBits, copyRight, 32) 最终完成左右部的交换* DES Swap(plainBits, plainBits-32) /逆初始置换(IPI置换)* DES IP 1 Transform (plainBits) Bit64ToChar8(plainBits, cipherBlock) turn o /*解密单个分组 int DES DecryptBlock(ElemType cipherBlock[8, ElemType subKeys[16] 18], ElemType plainBlock [81) ElemType cipherBits[ 641 Elem Type copy Right [48] int cnt Char8ToBit64(cipherBlock, cipherBits) /初始置换(IP置换)* DES IP Transform(cipherBits /*16轮迭代*/ for(cnt-15: cnt >-0: cnt--)i memcpy(copyRight, cipherBits+32, 32 /*将右半部分进行扩展置换,从32位扩展到48位 DES T Trans form(copyright) /*将右半部分与子密钥进行异或操作 DES XOR(copy Right, subKeys [ent], 48) /*异或结果进入S盒,输出32位结果* DES SBOX(copyRight) /米P置换* DES P Transform(copyright) /*将明文h半部分与右半部分进行异或* DES XOR (cipherBits, copy Right, 32) f(cnt /米最终完成左右部的交换* DES Swap(cipherBits, cipherBits+32) /*逆初始置换(IP1置换)* DES IP 1 Transform(cipherBits) Bit64ToChar8(cipherBits, plainBlock) return 0: *加密文件 int DES Encrypt (char xplainFile, char *keyStr, char *cipherFile)t FILE xplain, i*cipher; int count ElcmType plainBlock[81, ciphcrBlock [8, keyBlock 8 第5页 AIS加密算法c语言实现代码 Elem Type bEy 64] ElemType subKeys[16][18] if((plain- fopen(plainFilc, " rb"))--NULL) return Plain FILe OPEN ERROR return CIPHER FILE OPEN ERROR: ))== NULL)( if ((cipher fopen(cipherFile, wb /设置密钥 memcpy (keyBlock, key Str, 8) 将密钥转换为二进制流* Char8ToBit64(keyBlock, bKcy /牛成子密钥* DES Make SubKeys(bEy, subKeys while(!feof plain))( /*每次读8个字节,并返回成功读取的字节数* if((count- fread(plainBlock, sizeof(char),8, plain)) 8){ DES EncryptBlock (plainBlock, subKeys, cipherBlock f(count)[ /*填充*/ memset(plainBlock ount, \0, 7- count) /*最后一个字符休存包括最后一个字符在内的所填充的字符数量水 plainblockl7-8-count DES EncryptBlock (plainBlock, subkeys, cipherBlock fwrite(cipherBlock, sizeof (char), 8, cipher) fclose (plain) f'c. lose(cipher return oK /*解密文件* int DES Decrypt(char *cipherFile, char *key Str, char xplainFile)i FILE* plain,米 cipher int count, times 0 long fileLen Eleml'ype plainBlock [8], cipherBlock[8], keyBlock[8 ElemType bEy _6 ElemType subKeys[16][48] if ((cipher fopen(cipherFile, rb ))= NULL)[ return CIPHEr FILe OPEN ERROR if((plain= fopen(plainFile, wb" ))= NULL) rcturn plain FIle OPEN ERROR /*设置密钥* memcpy(key Block, keyStr, 8) /将密钥转换为二进制流* Char8ToBit64 (keyBlock, bKey) /水生成子密钥* ES Make SubKeys(bKey subKeys) /取文什长度*/ fseek( cipher,0, SEEK END);/将文件指针置尾 fi lelen= ftel l( cipher);/*取文件指针当前位置*/ rewind( CIpher);/*将文件指针重指向文件头* while(1)i 密文的字节数一定是8的整数倍* fread(cipherBlock, sizeof(char), 8, cipher DES DecryptBlock(cipherBlock, subKeys, plainBlock) times +-8 if(times< fileLen fwrite(plainBlock, sizeof(char), 8, plain) /*判断末尾是否被填充米 if(plainBlock 71< 8)i 第6页 AIS加密算法c语言实现代码 for(count=8- plainBlock[7]; count< 7; count++)( if(plainBlock[ count!='\0i break if( count==7){/*有填充* fwrite(plairBlock, sizeof (char), 8- plainBlockL7, plain) else{/*无填充 fwrite(plainBlock, sizeof(char), 8, plain) t'close ( plain) fclose(cipher) return OK int main() clock t a, b a= clockO DES Encrypt( 1. txt, key. txt, 2. txt b=clock o printf("加密消耗%毫秒Ⅶn",b-a); system("pause") a= clock( DES Decrypt( 2. txt, key. txt", 3. txt") printf("解密消耗%毫秒、n",o-a) getcharo return 第7页
### PyCharm 打开文件显示不全的解决方案 当遇到PyCharm打开文件显示不全的情况时,可以尝试以下几种方法来解决问题。 #### 方法一:清理缓存并重启IDE 有时IDE内部缓存可能导致文件加载异常。通过清除缓存再启动程序能够有效改善此状况。具体操作路径为`File -> Invalidate Caches / Restart...`,之后按照提示完成相应动作即可[^1]。 #### 方法二:调整编辑器字体设置 如果是因为字体原因造成的内容显示问题,则可以通过修改编辑区内的文字样式来进行修复。进入`Settings/Preferences | Editor | Font`选项卡内更改合适的字号大小以及启用抗锯齿功能等参数配置[^2]。 #### 方法三:检查项目结构配置 对于某些特定场景下的源码视图缺失现象,可能是由于当前工作空间未能正确识别全部模块所引起。此时应该核查Project Structure里的Content Roots设定项是否涵盖了整个工程根目录;必要时可手动添加遗漏部分,并保存变更生效[^3]。 ```python # 示例代码用于展示如何获取当前项目的根路径,在实际应用中可根据需求调用该函数辅助排查问题 import os def get_project_root(): current_file = os.path.abspath(__file__) project_dir = os.path.dirname(current_file) while not os.path.exists(os.path.join(project_dir, '.idea')): parent_dir = os.path.dirname(project_dir) if parent_dir == project_dir: break project_dir = parent_dir return project_dir print(f"Current Project Root Directory is {get_project_root()}") ```
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值