1、/* This is an implementation of the RIJNDAEL cryptosystemfor 128 bit plaintext block.This programme only gives the instance of 128 bit key.You can modify a little details to meet your need for 192or 256 bit key input */#include “stdio.h“typedef unsigned char u1byte; /* an 8 bit unsigned character ty
2、pe */typedef unsigned long u4byte; /* a 32 bit unsigned integer type */#define LARGE_TABLESu1byte pow_tab256;u1byte log_tab256;u1byte sbx_tab256;u1byte isb_tab256;u4byte rco_tab 10;u4byte ft_tab4256;u4byte it_tab4256;#ifdef LARGE_TABLESu4byte fl_tab4256;u4byte il_tab4256;#endifu4byte tab_gen = 0;u4b
3、yte k_len;u4byte e_key60;u4byte d_key60;#define ff_mult(a,b) (a v = star_x(u); w = star_x(v); t = w (x); (y) = u v w; (y) = rotr(u t, 8) rotr(v t, 16) rotr(t,24)/* initialise the key schedule from the user supplied key */#define loop4(i) t = ls_box(rotr(t, 8) rco_tabi; t = e_key4 * i; e_key4 * i + 4
4、 = t; t = e_key4 * i + 1; e_key4 * i + 5 = t; t = e_key4 * i + 2; e_key4 * i + 6 = t; t = e_key4 * i + 3; e_key4 * i + 7 = t; #define loop6(i) t = ls_box(rotr(t, 8) rco_tabi; t = e_key6 * i; e_key6 * i + 6 = t; t = e_key6 * i + 1; e_key6 * i + 7 = t; t = e_key6 * i + 2; e_key6 * i + 8 = t; t = e_key
5、6 * i + 3; e_key6 * i + 9 = t; t = e_key6 * i + 4; e_key6 * i + 10 = t; t = e_key6 * i + 5; e_key6 * i + 11 = t; #define loop8(i) t = ls_box(rotr(t, 8) rco_tabi; t = e_key8 * i; e_key8 * i + 8 = t; t = e_key8 * i + 1; e_key8 * i + 9 = t; t = e_key8 * i + 2; e_key8 * i + 10 = t; t = e_key8 * i + 3; e
6、_key8 * i + 11 = t; t = e_key8 * i + 4 ls_box(t); e_key8 * i + 12 = t; t = e_key8 * i + 5; e_key8 * i + 13 = t; t = e_key8 * i + 6; e_key8 * i + 14 = t; t = e_key8 * i + 7; e_key8 * i + 15 = t; void set_key(const u4byte in_key, const u4byte key_len) u4byte i, t, u, v, w;if(!tab_gen)gen_tabs();k_len
7、= (key_len + 31) / 32;e_key0 = in_key0; e_key1 = in_key1;e_key2 = in_key2; e_key3 = in_key3;switch(k_len)case 4: t = e_key3;for(i = 0; i 6)f_nround(b1, b0, kp); f_nround(b0, b1, kp);if(k_len 4)f_nround(b1, b0, kp); f_nround(b0, b1, kp);f_nround(b1, b0, kp); f_nround(b0, b1, kp);f_nround(b1, b0, kp);
8、 f_nround(b0, b1, kp);f_nround(b1, b0, kp); f_nround(b0, b1, kp);f_nround(b1, b0, kp); f_nround(b0, b1, kp);f_nround(b1, b0, kp); f_lround(b0, b1, kp);out_blk0 = b00; out_blk1 = b01;out_blk2 = b02; out_blk3 = b03;/* decrypt a block of text */#define i_nround(bo, bi, k) i_rn(bo, bi, 0, k); i_rn(bo, b
9、i, 1, k); i_rn(bo, bi, 2, k); i_rn(bo, bi, 3, k); k -= 4#define i_lround(bo, bi, k) i_rl(bo, bi, 0, k); i_rl(bo, bi, 1, k); i_rl(bo, bi, 2, k); i_rl(bo, bi, 3, k)void decrypt(const u4byte in_blk4, u4byte out_blk4) u4byte b04, b14, *kp;b00 = in_blk0 e_key4 * k_len + 24; b01 = in_blk1 e_key4 * k_len +
10、 25;b02 = in_blk2 e_key4 * k_len + 26; b03 = in_blk3 e_key4 * k_len + 27;kp = d_key + 4 * (k_len + 5);if(k_len 6)i_nround(b1, b0, kp); i_nround(b0, b1, kp);if(k_len 4)i_nround(b1, b0, kp); i_nround(b0, b1, kp);i_nround(b1, b0, kp); i_nround(b0, b1, kp);i_nround(b1, b0, kp); i_nround(b0, b1, kp);i_nr
11、ound(b1, b0, kp); i_nround(b0, b1, kp);i_nround(b1, b0, kp); i_nround(b0, b1, kp);i_nround(b1, b0, kp); i_lround(b0, b1, kp);out_blk0 = b00; out_blk1 = b01;out_blk2 = b02; out_blk3 = b03;/* use data to test programme */void main( ) int i;u4byte out_block4;const u4byte in_key4=0x11111111,0x22222222,0
12、x33333333,0x44444444;const u4byte plain_block4=0x12121212,0x34343434,0x45454545,0x56565656;const u4byte cipher_block4=0x844050f7,0xe346a4ff,0x375104da,0xabb97f8b;printf(“-n“);set_key(in_key, 128);encrypt(plain_block, out_block);printf(“Encryptions:n“);for(i=0;i4;i+)printf(“%x “,out_blocki);decrypt(cipher_block, out_block);printf(“nDecryptions:n“);for(i=0;i4;i+)printf(“%x “,out_blocki);