1 #include <stdio.h> 2 3 #define w 32 /* word为32bits */ 4 #define r 20 //加密轮数 5 6 #define P32 0xB7E15163 /* 定义两个常量,用于子密钥生成 */ 7 #define Q32 0x9E3779B9 8 12 #define bytes (w / 8) /* 定义字节*/ 13 #define c ((b + bytes - 1) / bytes) /* 密钥字数 */ 14 #define R24 (2 * r + 4) 15 #define lgw 5 /* log2(w) */ 16 17 /* 定义逻辑移位操作运算 */ 18 #define ROTL(x,y) (((x)<<(y&(w-1))) | ((x)>>(w-(y&(w-1))))) 19 #define ROTR(x,y) (((x)>>(y&(w-1))) | ((x)<<(w-(y&(w-1))))) 20 21 unsigned int S[R24 - 1]; /* 子密钥组 */ 22 23 void rc6_key_setup(unsigned char *K, int b) 24 { 25 int i, j, s, v; 26 unsigned int L[(32 + bytes - 1) / bytes]; /* Big enough for max b */ 27 unsigned int A, B; 28 29 L[c - 1] = 0; 30 for (i = b - 1; i >= 0; i--) 31 L[i / bytes] = (L[i / bytes] << 8) + K[i]; 32 33 S[0] = P32; 34 for (i = 1; i <= 2 * r + 3; i++) 35 S[i] = S[i - 1] + Q32; 36 37 A = B = i = j = 0; 38 v = R24; 39 if (c > v) v = c; 40 v *= 3; 41 42 for (s = 1; s <= v; s++) 43 { 44 A = S[i] = ROTL(S[i] + A + B, 3); 45 B = L[j] = ROTL(L[j] + A + B, A + B); 46 i = (i + 1) % R24; 47 j = (j + 1) % c; 48 } 49 } 50 51 void rc6_block_encrypt(unsigned int *pt, unsigned int *ct) 52 { 53 unsigned int A, B, C, D, t, u, x; 54 int i; 55 56 A = pt[0]; 57 B = pt[1]; 58 C = pt[2]; 59 D = pt[3]; 60 B += S[0]; 61 D += S[1]; 62 for (i = 2; i <= 2 * r; i += 2) 63 { 64 t = ROTL(B * (2 * B + 1), lgw); 65 u = ROTL(D * (2 * D + 1), lgw); 66 A = ROTL(A ^ t, u) + S[i]; 67 C = ROTL(C ^ u, t) + S[i + 1]; 68 x = A; 69 A = B; 70 B = C; 71 C = D; 72 D = x; 73 } 74 A += S[2 * r + 2]; 75 C += S[2 * r + 3]; 76 ct[0] = A; 77 ct[1] = B; 78 ct[2] = C; 79 ct[3] = D; 80 } 81 82 void rc6_block_decrypt(unsigned int *ct, unsigned int *pt) 83 { 84 unsigned int A, B, C, D, t, u, x; 85 int i; 86 87 A = ct[0]; 88 B = ct[1]; 89 C = ct[2]; 90 D = ct[3]; 91 C -= S[2 * r + 3]; 92 A -= S[2 * r + 2]; 93 for (i = 2 * r; i >= 2; i -= 2) 94 { 95 x = D; 96 D = C; 97 C = B; 98 B = A; 99 A = x; 100 u = ROTL(D * (2 * D + 1), lgw); 101 t = ROTL(B * (2 * B + 1), lgw); 102 C = ROTR(C - S[i + 1], t) ^ u; 103 A = ROTR(A - S[i], u) ^ t; 104 } 105 D -= S[1]; 106 B -= S[0]; 107 pt[0] = A; 108 pt[1] = B; 109 pt[2] = C; 110 pt[3] = D; 111 } 112 113 struct test_struct 114 { 115 int keylen; 116 unsigned char key[32]; 117 unsigned int pt[4]; 118 unsigned int ct[4]; 119 } tests[] = 120 { 121 { 16, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 122 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 123 {0x00000000, 0x00000000, 0x00000000, 0x00000000}, 124 {0x36a5c38f, 0x78f7b156, 0x4edf29c1, 0x1ea44898}, 125 }, 126 127 { 16, {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 128 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78}, 129 {0x35241302, 0x79685746, 0xbdac9b8a, 0xf1e0dfce}, 130 {0x2f194e52, 0x23c61547, 0x36f6511f, 0x183fa47e}, 131 }, 132 133 { 24, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 134 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 135 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 136 {0x00000000, 0x00000000, 0x00000000, 0x00000000}, 137 {0xcb1bd66c, 0x38300b19, 0x163f8a4e, 0x82ae9086}, 138 }, 139 140 { 24, {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 141 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, 142 0x89, 0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0}, 143 {0x35241302, 0x79685746, 0xbdac9b8a, 0xf1e0dfce}, 144 {0xd0298368, 0x0405e519, 0x2ae9521e, 0xd49152f9}, 145 }, 146 147 { 32, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 148 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 149 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 150 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 151 {0x00000000, 0x00000000, 0x00000000, 0x00000000}, 152 {0x05bd5f8f, 0xa85fd110, 0xda3ffa93, 0xc27e856e}, 153 }, 154 155 { 32, {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 156 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, 157 0x89, 0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0, 158 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe}, 159 {0x35241302, 0x79685746, 0xbdac9b8a, 0xf1e0dfce}, 160 {0x161824c8, 0x89e4d7f0, 0xa116ad20, 0x485d4e67}, 161 }, 162 163 { 0, 164 } 165 }; 166 167 int 168 main() 169 { 170 unsigned int ct[4], pt[4]; 171 int i,j; 172 struct test_struct *p; 173 174 for (p = tests, i = 1; p->keylen; p++, i++) 175 { 176 rc6_key_setup(p->key, p->keylen); 177 rc6_block_encrypt(p->pt, ct); 178 printf("Test %d\n",i); 179 180 printf("密钥是%d字:\n密钥为:",p->keylen); 181 for(j=0;j<p->keylen;j++) 182 printf("%.8lx ",p->key[j]); 183 printf("\n"); 184 printf("加密:\n明文:%08x %08x %08x %08x\n", 185 p->pt[0], p->pt[1], p->pt[2], p->pt[3]); 186 printf("密文: %08x %08x %08x %08x\n", 187 ct[0], ct[1], ct[2], ct[3]); 188 rc6_block_decrypt(ct, pt); 189 printf("解密:\n密文:%08x %08x %08x %08x\n", 190 ct[0], ct[1], ct[2], ct[3]); 191 printf("明文: %08x %08x %08x %08x\n\n", 192 pt[0], pt[1], pt[2], pt[3]); 193 } 194 system("pause"); 195 return 0; 196 197 }