现在网上能找到的AES代码九成都是ECB模式的,剩下的一成里又有九成只对十六个一组的数进行了加解密处理,压根没有分组。我在网上扒拉了好久都没有找到CBC模式的完整代码,只有接口函数。
CBC的分组原理就是这个图嘛,但是所有的接口函数都只告诉你,需要用到初始向量IV,不告诉你初始向量IV到底是什么,然后我被某个教程坑的老惨了,它告诉我第一次就是当前分组,我这样写出来的代码就一直出错。我测了一下发现无论我加解密的长度是多少,永远都是第一个分组的解密有问题,而且每回的错误解密结果都一模一样,就开始到处加打印输出,一输出就发现我是真蠢得不清,异或的逻辑是相同为0不同为1啊,它如果异或它本身的话结果永远为0,这也是为什么无论我输入什么,解密结果错误的那一段一直相同了。
目前代码是加密文明、密钥、解密结果全部使用正常字符串,没来得及写字节填充,明文要求是16的倍数,密钥必须是16位,加密结果放在本地文件中,解密时也需要从本地文件里读数据。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
using namespace std;
unsigned char xtime (unsigned char input);
void keyexpansion(unsigned char S_BOX[][16], unsigned char keys[][44]);
void bytesub(unsigned char S_BOX[][16], unsigned char B[][4]);
void shiftrow(unsigned char B[][4]);
void mixcolumn(unsigned char input[][4]);
void invbytesub(unsigned char N_S_BOX[][16], unsigned char B[][4]);
void invshiftrow(unsigned char B[][4]);
void invmixcolum(unsigned char input[][4]);
int encrypt(unsigned char S_BOX[][16]);
int decrypt(unsigned char S_BOX[][16], unsigned char N_S_BOX[][16]);
static int getIntFromChar(char c);
static void convertArrayToStr(int array[4][4], char* str);
void writeStrToFile(char* str, int len, char* fileName);
void xorWithiv(unsigned char iv[4][4], unsigned char B[4][4]);
static int getIntFromChar(char c) {
int result = (int)c;
return result & 0x000000ff;
}
static void convertArrayToStr(int array[4][4], char* str) {
int i, j;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
*str++ = (char)array[j][i];
}
void writeStrToFile(char* str, int len, char* fileName) {
FILE* fp;
fp = fopen(fileName, "wb");
for (int i = 0; i < len; i++)
putc(str[i], fp);
fclose(fp);
}
int readStrFromFile(char* fileName, char* str) {
FILE* fp = fopen(fileName, "rb");
if (fp == NULL) {
printf("打开文件出错,请确认文件存在当前目录下!\n");
exit(0);
}
int i;
for (i = 0; i < 1024 && (str[i] = getc(fp)) != EOF; i++);
if (i >= 1024) {
printf("解密文件过大!\n");
exit(0);
}
str[i] = '\0';
fclose(fp);
return i;
}
void xorWithiv(unsigned char iv[4][4], unsigned char B[4][4]){
for(int i = 0; i <= 3; i ++){
for(int j = 0; j <= 3; j ++){
B[i][j]^= iv[i][j];
}
}
}
void keyexpansion(unsigned char S_BOX[][16], unsigned char keys[][44]){
unsigned char Rcon[11] = {0, 1, 2, 4, 8, 16, 32, 64, 128, 27, 54};
unsigned char past[4];
register int i, j;
char str[16];
int k = 0;
printf("Please enter the key:\n");
scanf("%s", str);
for(i = 0; i <= 3; i ++)
for(j = 0; j <= 3; j ++){
keys[j][i] = getIntFromChar(str[k]);
k ++;
}
for(i = 4; i <= 43; i ++){
if(i % 4 == 0){
for(j = 1; j <= 4; j ++)
past[j - 1] = keys[j % 4][i -1];
for(j = 0; j <= 3; j ++){
if(j == 0)
keys[j][i] = S_BOX[past[j] / 16][past[j] % 16] ^ Rcon[i / 4] ^ keys[j][i - 4];
else
keys[j][i] = S_BOX[past[j] / 16][past[j] % 16] ^ keys[j][i - 4];
}
}
else{
for(j = 0; j <= 3; j ++){
keys[j][i] = keys[j][i - 4] ^ keys[j][i - 1];
}
}
}
}
unsigned char xtime (unsigned char input){
int temp;
temp = input << 1;
if(input & 0x80){
temp ^= 0x1b;
}
return temp;
}
void mixcolumn(unsigned char input[][4]){
int i, j;
unsigned char output[4][4];
for(j = 0; j <= 3; j++)
for(i = 0; i <= 3; i++)
output[i][j] = xtime(input[i%4][j])
^ ( input[ ( i + 1 ) % 4][j] ^ xtime( input[ ( i + 1 ) % 4][j] ) )
^ input[ ( i + 2 ) % 4][j]
^ input[ ( i + 3 ) % 4][j];
for(i = 0; i <= 3; i ++)
for(j = 0; j <= 3; j ++)
input[i][j] = output[i][j];
}
void shiftrow(unsigned char B[][4]){
int i, temp;
temp = B[1][0];
for(i = 0; i <= 2; i ++)
B[1][i] = B[1][i + 1];
B[1][3] = temp;
for(i = 0; i <= 1; i ++){
temp = B[2][i];
B[2][i] = B[2][i + 2];
B[2][i + 2] = temp;
}
temp = B[3][3];
for(i = 3; i >= 1; i --)
B[3][i] = B[3][i - 1];
B[3][0] = temp;
}
void bytesub(unsigned char S_BOX[][16], unsigned char B[][4]){
register int i, j;
for(i = 0; i <= 3; i ++)
for(j = 0; j <= 3; j ++)
B[i][j] = S_BOX[B[i][j] / 16][B[i][j] % 16];
}
void invshiftrow(unsigned char B[][4]){
int i, temp;
temp = B[1][3];
for(i = 3; i >= 1; i --)
B[1][i] = B[1][i - 1];
B[1][0] = temp;
for(i = 0; i <= 1; i ++){
temp = B[2][i];
B[2][i] = B[2][i + 2];
B[2][i + 2] = temp;
}
temp = B[3][0];
for(i = 0; i <= 2; i ++)
B[3][i] = B[3][i + 1];
B[3][3] = temp;
}
void invmixcolum(unsigned char input[][4]){
int i, j;
unsigned char output[4][4];
for(j = 0; j < 4; j++)
for(i = 0; i < 4; i++)
output[i][j] = (xtime(xtime(xtime(input[i % 4][j]))) ^ xtime(xtime(input[i % 4][j])) ^ xtime(input[i % 4][j]))
^ (xtime(xtime(xtime(input[ ( i + 1 ) % 4][j]))) ^ xtime(input[ ( i + 1 ) % 4][j]) ^ input[ ( i + 1 ) % 4][j])
^ (xtime(xtime(xtime(input[ ( i + 2 ) % 4][j]))) ^ xtime(xtime(input[ ( i + 2 ) % 4][j])) ^ input[ ( i + 2 ) % 4][j])
^ (xtime(xtime(xtime(input[ ( i + 3 ) % 4][j]))) ^ input[ ( i + 3 ) % 4][j]);
for(i = 0; i <= 3; i ++)
for(j = 0; j <= 3; j ++)
input[i][j] = output[i][j];
}
void invbytesub(unsigned char N_S_BOX[][16], unsigned char B[][4]){
register int i, j;
for(i = 0; i <= 3; i ++)
for(j = 0; j <= 3; j ++)
B[i][j] = N_S_BOX[B[i][j] / 16][B[i][j] % 16];
}
int encrypt(unsigned char S_BOX[][16]){
unsigned char e, B[4][4], iv[4][4];
unsigned char keys[4][44];
int i, j;
int level;
char str[1024],result[1024];
int cArray[4][4];
int len, k, l;
printf("Please enter the plaintext:\n");
scanf("%s", str);
getchar();
len = strlen(str);
if(len % 16 != 0){
printf("请输入十六的倍数长度\n");
return 0;
}
keyexpansion(S_BOX, keys);
k = 0;
for(l = 0; l < len; l += 16){
k = l;
for(i = 0; i <= 3; i ++){
for(j = 0; j <= 3; j ++){
B[j][i] = getIntFromChar(str[k]);
k++;
}
}
if(l == 0){
for(i = 0; i < 4; i ++){
for(j = 0; j < 4; j ++){
iv[i][j] = 0;
}
}
}
xorWithiv(iv, B);
for(i = 0; i <= 3; i ++)
for(j = 0; j <= 3; j ++){
B[i][j] ^= keys[i][j];
}
for(level = 1; level <= 9; level ++){
bytesub(S_BOX, B);
shiftrow(B);
mixcolumn(B);
for(i = 0; i <= 3; i ++)
for(j = 0; j <= 3; j ++)
B[i][j] ^= keys[i][level * 4 + j];
}
bytesub(S_BOX, B);
shiftrow(B);
for(i = 0; i <= 3; i ++)
for(j = 0; j <= 3; j ++){
B[i][j] ^= keys[i][40 + j];
cArray[i][j] = (int)B[i][j];
}
convertArrayToStr(cArray, result+l);
for(i = 0; i < 4; i ++){
for(j = 0; j < 4; j ++){
iv[i][j] = B[i][j];
}
}
}
result[len]='\0';
char fileName[64];
printf("请输入你想要写进的文件名,比如'test.txt':\n");
if (scanf("%s", fileName) == 1) {
writeStrToFile(result, len, fileName);
printf("已经将密文写进%s中了,可以在运行该程序的当前目录中找到它。\n", fileName);
}
return 0;
}
int decrypt(unsigned char S_BOX[][16], unsigned char N_S_BOX[][16]){
unsigned char B[4][4], iv[4][4], bef[4][4];
unsigned char keys[4][44];
int temp, i, j;
int level;
char str[1024];
char result[1024];
int cArray[4][4];
int len, l, k;
char fileName[64];
printf("请输入要解密的文件名,该文件必须和本程序在同一个目录\n");
if (scanf("%s", fileName) == 1){
int clen = readStrFromFile(fileName, str);
}
len = strlen(str);
keyexpansion(S_BOX, keys);
printf("The output information as:\n");
for(l = 0; l < len; l +=16){
k = l;
for(i = 0;i <=3; i ++){
for(j = 0; j <=3; j ++){
B[j][i] = getIntFromChar(str[k]);
bef[j][i] = B[j][i];
k ++;
}
}
if(l == 0){
for(i = 0; i < 4; i ++){
for(j = 0; j < 4; j ++){
iv[i][j] = 0;
}
}
}
for(i = 0; i <= 3; i ++)
for(j = 0; j <= 3; j ++)
B[i][j] ^= keys[i][j + 40];
for(level = 1; level <= 9; level ++){
invshiftrow(B);
invbytesub(N_S_BOX, B);
for(i = 0; i <= 3; i ++)
for(j = 0; j <= 3; j ++)
B[i][j] ^= keys[i][40 - level * 4 + j];
invmixcolum(B);
}
invshiftrow(B);
invbytesub(N_S_BOX, B);
for(i = 0; i <= 3; i ++)
for(j = 0; j <= 3; j ++){
B[i][j] ^= keys[i][j];
}
xorWithiv(iv, B);
for(i = 0; i < 4; i ++){
for(j = 0; j < 4; j ++){
iv[i][j] = bef[i][j];
cArray[i][j] = (int)B[i][j];
}
}
convertArrayToStr(cArray, result+l);
}
result[len]='\0';
printf("%s",result);
return 0;
}
int main()
{
unsigned char S_BOX[16][16] =
{
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,
0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, 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 choose;
printf("This is C implementation of AES:\n");
loop:
printf("\nEncryption please enter: 1\n");
printf("Decryption please input: 2\n");
printf("Exit please input: 0 \n\n");
printf("Please enter your choice:");
scanf("%d", &choose);
switch(choose){
case 1:
encrypt(S_BOX);
goto loop;
case 2:
decrypt(S_BOX, N_S_BOX);
goto loop;
case 0:
exit(0);
default :
exit(0);
}
return 0;
}
贴一下我参考的相关链:
AES五种加密模式(CBC、ECB、CTR、OCF、CFB) https://www.cnblogs.com/starwolf/p/3365834.html
aes相关资料整理及代码C/C++(这我自己写的,里边放着应用密码学书上的aes代码,我就是以这个为基础开始改动增加功能的) https://blog.csdn.net/mandiheyanyu/article/details/123197186
AES加密算法的详细介绍与实现(文件读取什么的是从这里借鉴的) https://blog.csdn.net/qq_28205153/article/details/55798628
C++实现AES算法,ECB/CBC模式,PKCS#7填充加解密(我cbc主要看的这个里边的接口函数) https://www.cnblogs.com/dayq/p/15359184.html
C++ 和 java 使用 AES CBC 128 加解密(救我一命告诉我IV到底是个啥玩意的代码,我居然还是从java部分看的) https://www.cnblogs.com/diegodu/p/6273611.html
|