1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
| #include "chacha20.h"
void four_uCh2uLong(u8* in, u32* out) { int i = 0; *out = 0; for (i = 0; i < 4; i++) *out = ((u32)in[i] << (24 - i * 8)) ^ *out; }
void uLong2four_uCh(u32 in, u8* out) { int i = 0; //从32位unsigned long的高位开始取 for (i = 0; i < 4; i++) *(out + i) = (u32)(in >> (24 - i * 8)); }
u32 move(u32 data, int length) { u32 result = 0; result = (data << length) ^ (data >> (32 - length));
return result; }
u32 func_key(u32 input) { int i = 0; u32 ulTmp = 0; u8 ucIndexList[4] = { 0 }; u8 ucSboxValueList[4] = { 0 }; uLong2four_uCh(input, ucIndexList); for (i = 0; i < 4; i++) { ucSboxValueList[i] = TBL_SBOX[ucIndexList[i]]; } four_uCh2uLong(ucSboxValueList, &ulTmp); ulTmp = ulTmp ^ move(ulTmp, 13) ^ move(ulTmp, 23);
return ulTmp; }
u32 func_data(u32 input) { int i = 0; u32 ulTmp = 0; u8 ucIndexList[4] = { 0 }; u8 ucSboxValueList[4] = { 0 }; uLong2four_uCh(input, ucIndexList); for (i = 0; i < 4; i++) { ucSboxValueList[i] = TBL_SBOX[ucIndexList[i]]; } four_uCh2uLong(ucSboxValueList, &ulTmp); ulTmp = ulTmp ^ move(ulTmp, 2) ^ move(ulTmp, 10) ^ move(ulTmp, 18) ^ move(ulTmp, 24);
return ulTmp; } void encode_fun(u8 len, u8* key, u8* input, u8* output) { int i = 0, j = 0; u8* p = (u8*)malloc(50); //定义一个50字节缓存区 u32 ulKeyTmpList[4] = { 0 }; //存储密钥的u32数据 u32 ulKeyList[36] = { 0 }; //用于密钥扩展算法与系统参数FK运算后的结果存储 u32 ulDataList[36] = { 0 }; //用于存放加密数据 four_uCh2uLong(key, &(ulKeyTmpList[0])); four_uCh2uLong(key + 4, &(ulKeyTmpList[1])); four_uCh2uLong(key + 8, &(ulKeyTmpList[2])); four_uCh2uLong(key + 12, &(ulKeyTmpList[3]));
ulKeyList[0] = ulKeyTmpList[0] ^ TBL_SYS_PARAMS[0]; ulKeyList[1] = ulKeyTmpList[1] ^ TBL_SYS_PARAMS[1]; ulKeyList[2] = ulKeyTmpList[2] ^ TBL_SYS_PARAMS[2]; ulKeyList[3] = ulKeyTmpList[3] ^ TBL_SYS_PARAMS[3];
for (i = 0; i < 32; i++) //32次循环迭代运算 { ulKeyList[i + 4] = ulKeyList[i] ^ func_key(ulKeyList[i + 1] ^ ulKeyList[i + 2] ^ ulKeyList[i + 3] ^ TBL_FIX_PARAMS[i]); } for (i = 0; i < len; i++) //将输入数据存放在p缓存区 *(p + i) = *(input + i); for (i = 0; i < 16 - len % 16; i++)//将不足16位补0凑齐16的整数倍 *(p + len + i) = 0;
for (j = 0; j < len / 16 + ((len % 16) ? 1 : 0); j++) { four_uCh2uLong(p + 16 * j, &(ulDataList[0])); four_uCh2uLong(p + 16 * j + 4, &(ulDataList[1])); four_uCh2uLong(p + 16 * j + 8, &(ulDataList[2])); four_uCh2uLong(p + 16 * j + 12, &(ulDataList[3])); for (i = 0; i < 32; i++) { ulDataList[i + 4] = ulDataList[i] ^ func_data(ulDataList[i + 1] ^ ulDataList[i + 2] ^ ulDataList[i + 3] ^ ulKeyList[i + 4]); } uLong2four_uCh(ulDataList[35], output + 16 * j); uLong2four_uCh(ulDataList[34], output + 16 * j + 4); uLong2four_uCh(ulDataList[33], output + 16 * j + 8); uLong2four_uCh(ulDataList[32], output + 16 * j + 12); } free(p); }
void decode_fun(u8 len, u8* key, u8* input, u8* output) { int i = 0, j = 0; u32 ulKeyTmpList[4] = { 0 };//存储密钥的u32数据 u32 ulKeyList[36] = { 0 }; //用于密钥扩展算法与系统参数FK运算后的结果存储 u32 ulDataList[36] = { 0 }; //用于存放加密数据 four_uCh2uLong(key, &(ulKeyTmpList[0])); four_uCh2uLong(key + 4, &(ulKeyTmpList[1])); four_uCh2uLong(key + 8, &(ulKeyTmpList[2])); four_uCh2uLong(key + 12, &(ulKeyTmpList[3]));
ulKeyList[0] = ulKeyTmpList[0] ^ TBL_SYS_PARAMS[0]; ulKeyList[1] = ulKeyTmpList[1] ^ TBL_SYS_PARAMS[1]; ulKeyList[2] = ulKeyTmpList[2] ^ TBL_SYS_PARAMS[2]; ulKeyList[3] = ulKeyTmpList[3] ^ TBL_SYS_PARAMS[3];
for (i = 0; i < 32; i++) { ulKeyList[i + 4] = ulKeyList[i] ^ func_key(ulKeyList[i + 1] ^ ulKeyList[i + 2] ^ ulKeyList[i + 3] ^ TBL_FIX_PARAMS[i]); } for (j = 0; j < len / 16; j++) { four_uCh2uLong(input + 16 * j, &(ulDataList[0])); four_uCh2uLong(input + 16 * j + 4, &(ulDataList[1])); four_uCh2uLong(input + 16 * j + 8, &(ulDataList[2])); four_uCh2uLong(input + 16 * j + 12, &(ulDataList[3])); for (i = 0; i < 32; i++) { ulDataList[i + 4] = ulDataList[i] ^ func_data(ulDataList[i + 1] ^ ulDataList[i + 2] ^ ulDataList[i + 3] ^ ulKeyList[35 - i]); } uLong2four_uCh(ulDataList[35], output + 16 * j); uLong2four_uCh(ulDataList[34], output + 16 * j + 4); uLong2four_uCh(ulDataList[33], output + 16 * j + 8); uLong2four_uCh(ulDataList[32], output + 16 * j + 12); } }
void print_hex(u8* data, int len) { int i = 0; char alTmp[16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' }; for (i = 0; i < len; i++) { printf("%c", alTmp[data[i] / 16]); printf("%c", alTmp[data[i] % 16]); putchar(' '); } putchar('\n'); } int main(void) {
unsigned char a91tNhn90uTlt1l[] = { 0x5B, 0x40, 0x39, 0x31, 0x54, 0x25, 0x4E, 0x68, 0x6E, 0x7B, 0x39, 0x30, 0x55, 0x40, 0x74, 0x6C, 0x54, 0x25, 0x31, 0x6C, 0x54, 0x24, 0x64, 0x70, 0x68, 0x50, 0x68, 0x66, 0x69, 0x40, 0x39, 0x31, 0x4F, 0x00 }; for (int i = 0; i < 33; i += 4) { a91tNhn90uTlt1l[i] ^= 0xC; a91tNhn90uTlt1l[i + 1] ^= 0x17; }//You_can_take_me_with_you //CAT_IN_X_19_Y_39 //_CATLOVE_OR_LIKE // LIKE_OR_LOVE_CAT //EKIL_RO_EVOLTAC_ //CatCTF{You_can_take_me_with_you_CAT_IN_X_19_Y_39_} //CAT_IN_X_19_Y_39_LIKE_OR_LOVE_CAT u8 i, len; u8 encode_Result[50] = { 0 }; //定义加密输出缓存区 u8 decode_Result[50] = { 0 }; //定义解密输出缓存区 unsigned char key[] = "wuwuwuyoucatchme"; u8 Data_plain[16] = { 0xB6,0x75,0xE1,0x79,0x70,0xC1,0x27,0x48,9,0xB,0xB6,0x4D,2,0xBC,6,0x19 }; len = 16 * (sizeof(Data_plain) / 16) + 16 * ((sizeof(Data_plain) % 16) ? 1 : 0);
decode_fun(len, key, Data_plain, decode_Result); printf("解密后数据是:\n"); for (i = 0; i < len; i++) printf("%x ", *(decode_Result + i));
system("pause"); return 0; }
|