Реализация криптографического алгоритма (AES) Rijndael

Курсовой проект - Компьютеры, программирование

Другие курсовые по предмету Компьютеры, программирование

; W3:= ForwardTable [Byte(T0 [3] shr 24)];[0]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[28];:= ForwardTable [Byte(T0 [1])]; W1:= ForwardTable [Byte(T0 [2] shr 8)];:= ForwardTable [Byte(T0 [3] shr 16)]; W3:= ForwardTable [Byte(T0 [0] shr 24)];[1]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[29];:= ForwardTable [Byte(T0 [2])]; W1:= ForwardTable [Byte(T0 [3] shr 8)];:= ForwardTable [Byte(T0 [0] shr 16)]; W3:= ForwardTable [Byte(T0 [1] shr 24)];[2]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[30];:= ForwardTable [Byte(T0 [3])]; W1:= ForwardTable [Byte(T0 [0] shr 8)];:= ForwardTable [Byte(T0 [1] shr 16)]; W3:= ForwardTable [Byte(T0 [2] shr 24)];[3]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[31];

// раунд 8:= ForwardTable [Byte(T1 [0])]; W1:= ForwardTable [Byte(T1 [1] shr 8)];:= ForwardTable [Byte(T1 [2] shr 16)]; W3:= ForwardTable [Byte(T1 [3] shr 24)];[0]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[32];:= ForwardTable [Byte(T1 [1])]; W1:= ForwardTable [Byte(T1 [2] shr 8)];:= ForwardTable [Byte(T1 [3] shr 16)]; W3:= ForwardTable [Byte(T1 [0] shr 24)];[1]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[33];:= ForwardTable [Byte(T1 [2])]; W1:= ForwardTable [Byte(T1 [3] shr 8)];:= ForwardTable [Byte(T1 [0] shr 16)]; W3:= ForwardTable [Byte(T1 [1] shr 24)];[2]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[34];:= ForwardTable [Byte(T1 [3])]; W1:= ForwardTable [Byte(T1 [0] shr 8)];:= ForwardTable [Byte(T1 [1] shr 16)]; W3:= ForwardTable [Byte(T1 [2] shr 24)];[3]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[35];

// раунд 9:= ForwardTable [Byte(T0 [0])]; W1:= ForwardTable [Byte(T0 [1] shr 8)];:= ForwardTable [Byte(T0 [2] shr 16)]; W3:= ForwardTable [Byte(T0 [3] shr 24)];[0]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[36];:= ForwardTable [Byte(T0 [1])]; W1:= ForwardTable [Byte(T0 [2] shr 8)];:= ForwardTable [Byte(T0 [3] shr 16)]; W3:= ForwardTable [Byte(T0 [0] shr 24)];[1]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[37];:= ForwardTable [Byte(T0 [2])]; W1:= ForwardTable [Byte(T0 [3] shr 8)];:= ForwardTable [Byte(T0 [0] shr 16)]; W3:= ForwardTable [Byte(T0 [1] shr 24)];[2]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[38];:= ForwardTable [Byte(T0 [3])]; W1:= ForwardTable [Byte(T0 [0] shr 8)];:= ForwardTable [Byte(T0 [1] shr 16)]; W3:= ForwardTable [Byte(T0 [2] shr 24)];[3]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[39];

// Последнийраундпреобраобразований:= LastForwardTable [Byte(T1 [0])]; W1:= LastForwardTable [Byte(T1 [1] shr 8)];:= LastForwardTable [Byte(T1 [2] shr 16)]; W3:= LastForwardTable [Byte(T1 [3] shr 24)];[0]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[40];:= LastForwardTable [Byte(T1 [1])]; W1:= LastForwardTable [Byte(T1 [2] shr 8)];:= LastForwardTable [Byte(T1 [3] shr 16)]; W3:= LastForwardTable [Byte(T1 [0] shr 24)];[1]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[41];:= LastForwardTable [Byte(T1 [2])]; W1:= LastForwardTable [Byte(T1 [3] shr 8)];:= LastForwardTable [Byte(T1 [0] shr 16)]; W3:= LastForwardTable [Byte(T1 [1] shr 24)];[2]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[42];:= LastForwardTable [Byte(T1 [3])]; W1:= LastForwardTable [Byte(T1 [0] shr 8)];:= LastForwardTable [Byte(T1 [1] shr 16)]; W3:= LastForwardTable [Byte(T1 [2] shr 24)];[3]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[43];

// Завершение(@OutBuf[0])^:= T0 [0]; PLongWord (@OutBuf[4])^:= T0 [1];(@OutBuf[8])^:= T0 [2]; PLongWord (@OutBuf[12])^:= T0 [3];;ExpandAESKeyForDecryption (var ExpandedKey: TAESExpandedKey128);: integer;, F2, F4, F8, F9: longword;I:= 1 to 9 do

// Процедура расширения ключа:= ExpandedKey [I * 4];

U:= F9 and $80808080;:= ((F9 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);:= F2 and $80808080;:= ((F2 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);:= F4 and $80808080;:= ((F4 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);:= F9 xor F8;[I * 4]:= F2 xor F4 xor F8 xor

(((F2 xor F9) shl 24) or ((F2 xor F9) shr 8)) xor

(((F4 xor F9) shl 16) or ((F4 xor F9) shr 16)) xor ((F9 shl 8) or (F9 shr 24));:= ExpandedKey [I * 4 + 1];:= F9 and $80808080;:= ((F9 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);:= F2 and $80808080;:= ((F2 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);:= F4 and $80808080;:= ((F4 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);:= F9 xor F8;[I * 4 + 1]:= F2 xor F4 xor F8 xor

(((F2 xor F9) shl 24) or ((F2 xor F9) shr 8)) xor

(((F4 xor F9) shl 16) or ((F4 xor F9) shr 16)) xor ((F9 shl 8) or (F9 shr 24));:= ExpandedKey [I * 4 + 2];:= F9 and $80808080;:= ((F9 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);:= F2 and $80808080;:= ((F2 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);:= F4 and $80808080;:= ((F4 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);:= F9 xor F8;[I * 4 + 2]:= F2 xor F4 xor F8 xor

(((F2 xor F9) shl 24) or ((F2 xor F9) shr 8)) xor

(((F4 xor F9) shl 16) or ((F4 xor F9) shr 16)) xor ((F9 shl 8) or (F9 shr 24));:= ExpandedKey [I * 4 + 3];:= F9 and $80808080;:= ((F9 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);:= F2 and $80808080;:= ((F2 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);:= F4 and $80808080;:= ((F4 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);:= F9 xor F8;[I * 4 + 3]:= F2 xor F4 xor F8 xor

(((F2 xor F9) shl 24) or ((F2 xor F9) shr 8)) xor

(((F4 xor F9) shl 16) or ((F4 xor F9) shr 16)) xor ((F9 shl 8) or (F9 shr 24));;;ExpandAESKeyForDecryption (const Key: TAESKey128; var ExpandedKey: TAESExpandedKey128);(Key, ExpandedKey);

// Подготовка ключа (расширение)(ExpandedKey);

// Расширение ключа для дешифрования;DecryptAES (const InBuf: TAESBuffer; const Key: TAESExpandedKey128;OutBuf: TAESBuffer);, T1: array [0..3] of longword;, W1, W2, W3: longword;

// Инициализация[0]:= PLongWord (@InBuf[0])^ xor Key[40];[1]:= PLongWord (@InBuf[4])^ xor Key[41];[2]:= PLongWord (@InBuf[8])^ xor Key[42];[3]:= PLongWord (@InBuf[12])^ xor Key[43];

// Преобразованиявыполняются 10 раз

// раунд 1:= InverseTable [Byte(T0 [0])]; W1:= InverseTable [Byte(T0 [3] shr 8)];:= InverseTable [Byte(T0 [2] shr 16)]; W3:= InverseTable [Byte(T0 [1] shr 24)];[0]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[36];:= InverseTable [Byte(T0 [1])]; W1:= InverseTable [Byte(T0 [0] shr 8)];:= InverseTable [Byte(T0 [3] shr 16)]; W3:= InverseTable [Byte(T0 [2] shr 24)];[1]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[37];:= InverseTable [Byte(T0 [2])]; W1:= InverseTable [Byte(T0 [1] shr 8)];:= InverseTable [Byte(T0 [0] shr 16)]; W3:= InverseTable [Byte(T0 [3] shr 24)];[2]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[38];:= InverseTable [Byte(T0 [3])]; W1:= InverseTable [Byte(T0 [2] shr 8)];:= InverseTable [Byte(T0 [1] shr 16)]; W3:= InverseTable [Byte(T0 [0] shr 24)];[3]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[39];

// раунд 2:= InverseTable [Byte(T1 [0])]; W1:= InverseTable [Byte(T1 [3] shr 8)];:= InverseTable [Byte(T1 [2] shr 16)]; W3:= InverseTable [Byte(T1 [1] shr 24)];[0]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[32];:= InverseTable [Byte(T1 [1])]; W1:= InverseTable [Byte(T1 [0] shr 8)];:= InverseTable [Byte(T1 [3] shr 16)]; W3:= InverseTable [Byte(T1 [2] shr 24)];[1]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[33];:= InverseTable [Byte(T1 [2])]; W1:= InverseTable [Byte(T1 [1] shr 8)];:= InverseTable [Byte(T1 [0] shr 16)]; W3:= InverseTable [Byte(T1 [3] shr 24)];[2]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[34];:= InverseTable [Byte(T1 [3])]; W1:= InverseTable [Byte(T1 [2] shr 8)];:= InverseTable [Byte(T1 [1] shr 16)]; W3:= InverseTable [Byte(T1 [0] shr 24)];[3]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[35];

// раунд 3:= InverseTable [Byte(T0 [0])]; W1:= InverseTable [Byte(T0 [3] shr 8)];:= InverseTable [Byte(T0 [2] shr 16)]; W3:= InverseTable [Byte(T0 [1] shr 24)];[0]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[28];:= InverseTable [Byte(T0 [1])]; W1:= InverseTable [Byte(T0 [0] shr 8)];:= InverseTable [Byte(T0 [3] shr 16)]; W3:= InverseTable [Byte(T0 [2] shr 24)];[1]:= (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))((W3 shl 24) or (W3 shr 8))) xor Key[29];:= InverseTable [Byte(T0 [2])]; W1:= Inv