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

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

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

erseTable [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[30];:= 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[31];

// раунд 4:= 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[24];:= 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[25];:= 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[26];:= 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[27];

// раунд 5:= 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[20];:= 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[21];:= 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[22];:= 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[23];

// раунд 6:= 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[16];:= 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[17];:= 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[18];:= 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[19];

// раунд 7:= 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[12];:= 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[13];:= 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[14];:= 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[15];

// раунд 8:= 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[8];:= 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[9];:= 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[10];:= 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[11];

// раунд 9:= 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[4];:= 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[5];:= 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[6];:= 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[7];

// Последнийраундпреобразований:= LastInverseTable [Byte(T1 [0])]; W1:= LastInverseTable [Byte(T1 [3] shr 8)];:= LastInverseTable [Byte(T1 [2] shr 16)]; W3:= LastInverseTable [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[0];:= LastInverseTable [Byte(T1 [1])]; W1:= LastInverseTable [Byte(T1 [0] shr 8)];:= LastInverseTable [Byte(T1 [3] shr 16)]; W3:= LastInverseTable [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[1];:= LastInverseTable [Byte(T1 [2])]; W1:= LastInverseTable [Byte(T1 [1] shr 8)];:= LastInverseTable [Byte(T1 [0] shr 16)]; W3:= LastInverseTable [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[2];:= LastInverseTable [Byte(T1 [3])]; W1:= LastInverseTable [Byte(T1 [2] shr 8)];:= LastInverseTable [Byte(T1 [1] shr 16)]; W3:= LastInverseTable [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[3];

// Конечное преобразование(@OutBuf[0])^:= T0 [0]; PLongWord (@OutBuf[4])^:= T0 [1];

PLongWord (@OutBuf[8])^:= T0 [2]; PLongWord (@OutBuf[12])^:= T0 [3];

end;

// Сначала ключ переводится к расширенному

procedure EncryptAESStreamECB (Source: TStream; Count: cardinal;Key: TAESKey128; Dest: TStream);: TAESExpandedKey128;(Key, ExpandedKey);

// Вызов из этой процедуры процедуры шифорования с расширенным ключом

EncryptAESStreamECB (Source, Count, ExpandedKey, Dest);;

// ШифрованиепотокаEncryptAESStreamECB (Source: TStream; Count: cardinal;ExpandedKey: TAESExpandedKey128; Dest: TStream);

var, TempOut: TAESBuffer; // Размер буферов считывания и записи

Done: cardinal;: TStringStream;: integer;:= TStringStream. Create();Count = 0 then. Position:= 0; // Указательвначалопотока:= Source. Size; // Определение текущего размера потока

endCount:= Min (Count, Source. Size - Source. Position);Count = 0 then exit;Count >= SizeOf(TAESBuffer) do:= Source. Read (TempIn, SizeOf(TempIn)); // СчитываемданныеизпотокаDone < SizeOf(TempIn) thenEStreamError. Create(SReadError);(TempIn, ExpandedKey, TempOut); flag = false then

// Вывод входного и выходного блоков

FmStop. EditIn. Text:= IntToHex (TempIn[0], 2)+IntToHex (TempIn[1], 2)+(TempIn[2], 2)+IntToHex (TempIn[3], 2)+IntToHex (TempIn[4], 2)+(TempIn[5], 2)+IntToHex (TempIn[6], 2)+IntToHex (TempIn[7], 2)+(TempIn[8], 2)+IntToHex (TempIn[9], 2)+IntToHex (TempIn[10], 2)+(TempIn[11], 2)+IntToHex