Алгоритм шифрования AES

Автор: Пользователь скрыл имя, 26 Ноября 2011 в 09:27, курсовая работа

Краткое описание

Шифрование — способ преобразования открытой информации в закрытую и обратно. Применяется для хранения важной информации в ненадёжных источниках или передачи её по незащищённым каналам связи. Согласно ГОСТ 28147-89, шифрование подразделяется на процесс зашифровывания и расшифровывания.
В зависимости от алгоритма преобразования данных, методы шифрования подразделяются на гарантированной или временной крипто-стойкости.

Оглавление

Введение 2
История AES 3
Описание AES 4
Программная реализация алгоритма AES 11
Листинг программы 13
Заключение 32
Список использованной литературы 33

Файлы: 1 файл

Курсач.docx

— 365.25 Кб (Скачать)

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[37];

  W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];

  W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];

  T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[38];

  W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];

  W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];

  T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[39];

  // раунд 2

  W0 := InverseTable[Byte(T1[0])]; W1 := InverseTable[Byte(T1[3] shr 8)];

  W2 := InverseTable[Byte(T1[2] shr 16)]; W3 := InverseTable[Byte(T1[1] shr 24)];

  T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[32];

  W0 := InverseTable[Byte(T1[1])]; W1 := InverseTable[Byte(T1[0] shr 8)];

  W2 := InverseTable[Byte(T1[3] shr 16)]; W3 := InverseTable[Byte(T1[2] shr 24)];

  T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[33];

  W0 := InverseTable[Byte(T1[2])]; W1 := InverseTable[Byte(T1[1] shr 8)];

  W2 := InverseTable[Byte(T1[0] shr 16)]; W3 := InverseTable[Byte(T1[3] shr 24)];

  T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[34];

  W0 := InverseTable[Byte(T1[3])]; W1 := InverseTable[Byte(T1[2] shr 8)];

  W2 := InverseTable[Byte(T1[1] shr 16)]; W3 := InverseTable[Byte(T1[0] shr 24)];

  T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[35];

  // раунд 3

  W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];

  W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];

  T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[28];

  W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];

  W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];

  T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[29];

  W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];

  W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];

  T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[30];

  W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];

  W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];

  T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[31];

  // раунд 4

  W0 := InverseTable[Byte(T1[0])]; W1 := InverseTable[Byte(T1[3] shr 8)];

  W2 := InverseTable[Byte(T1[2] shr 16)]; W3 := InverseTable[Byte(T1[1] shr 24)];

  T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[24];

  W0 := InverseTable[Byte(T1[1])]; W1 := InverseTable[Byte(T1[0] shr 8)];

  W2 := InverseTable[Byte(T1[3] shr 16)]; W3 := InverseTable[Byte(T1[2] shr 24)];

  T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[25];

  W0 := InverseTable[Byte(T1[2])]; W1 := InverseTable[Byte(T1[1] shr 8)];

  W2 := InverseTable[Byte(T1[0] shr 16)]; W3 := InverseTable[Byte(T1[3] shr 24)];

  T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[26];

  W0 := InverseTable[Byte(T1[3])]; W1 := InverseTable[Byte(T1[2] shr 8)];

  W2 := InverseTable[Byte(T1[1] shr 16)]; W3 := InverseTable[Byte(T1[0] shr 24)];

  T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[27];

  // раунд 5

  W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];

  W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];

  T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[20];

  W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];

  W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];

  T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[21];

  W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];

  W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];

  T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[22];

  W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];

  W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];

  T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[23];

  // раунд 6

  W0 := InverseTable[Byte(T1[0])]; W1 := InverseTable[Byte(T1[3] shr 8)];

  W2 := InverseTable[Byte(T1[2] shr 16)]; W3 := InverseTable[Byte(T1[1] shr 24)];

  T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[16];

  W0 := InverseTable[Byte(T1[1])]; W1 := InverseTable[Byte(T1[0] shr 8)];

  W2 := InverseTable[Byte(T1[3] shr 16)]; W3 := InverseTable[Byte(T1[2] shr 24)];

  T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[17];

  W0 := InverseTable[Byte(T1[2])]; W1 := InverseTable[Byte(T1[1] shr 8)];

  W2 := InverseTable[Byte(T1[0] shr 16)]; W3 := InverseTable[Byte(T1[3] shr 24)];

  T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[18];

  W0 := InverseTable[Byte(T1[3])]; W1 := InverseTable[Byte(T1[2] shr 8)];

  W2 := InverseTable[Byte(T1[1] shr 16)]; W3 := InverseTable[Byte(T1[0] shr 24)];

  T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[19];

  // раунд 7

  W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];

  W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];

  T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[12];

  W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];

  W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];

  T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[13];

  W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];

  W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];

  T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[14];

  W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];

  W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];

  T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[15];

  // раунд 8

  W0 := InverseTable[Byte(T1[0])]; W1 := InverseTable[Byte(T1[3] shr 8)];

  W2 := InverseTable[Byte(T1[2] shr 16)]; W3 := InverseTable[Byte(T1[1] shr 24)];

  T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[8];

  W0 := InverseTable[Byte(T1[1])]; W1 := InverseTable[Byte(T1[0] shr 8)];

  W2 := InverseTable[Byte(T1[3] shr 16)]; W3 := InverseTable[Byte(T1[2] shr 24)];

  T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[9];

  W0 := InverseTable[Byte(T1[2])]; W1 := InverseTable[Byte(T1[1] shr 8)];

  W2 := InverseTable[Byte(T1[0] shr 16)]; W3 := InverseTable[Byte(T1[3] shr 24)];

  T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[10];

  W0 := InverseTable[Byte(T1[3])]; W1 := InverseTable[Byte(T1[2] shr 8)];

  W2 := InverseTable[Byte(T1[1] shr 16)]; W3 := InverseTable[Byte(T1[0] shr 24)];

  T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[11];

  // раунд 9

  W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];

  W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];

  T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[4];

  W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];

  W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];

  T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[5];

  W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];

  W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];

  T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[6];

  W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];

  W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];

  T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[7];

  // Последний раунд преобразований

  W0 := LastInverseTable[Byte(T1[0])]; W1 := LastInverseTable[Byte(T1[3] shr 8)];

  W2 := LastInverseTable[Byte(T1[2] shr 16)]; W3 := LastInverseTable[Byte(T1[1] shr 24)];

  T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[0];

  W0 := LastInverseTable[Byte(T1[1])]; W1 := LastInverseTable[Byte(T1[0] shr 8)];

  W2 := LastInverseTable[Byte(T1[3] shr 16)]; W3 := LastInverseTable[Byte(T1[2] shr 24)];

  T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[1];

  W0 := LastInverseTable[Byte(T1[2])]; W1 := LastInverseTable[Byte(T1[1] shr 8)];

  W2 := LastInverseTable[Byte(T1[0] shr 16)]; W3 := LastInverseTable[Byte(T1[3] shr 24)];

  T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[2];

  W0 := LastInverseTable[Byte(T1[3])]; W1 := LastInverseTable[Byte(T1[2] shr 8)];

  W2 := LastInverseTable[Byte(T1[1] shr 16)]; W3 := LastInverseTable[Byte(T1[0] shr 24)];

  T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))

    xor ((W3 shl 24) or (W3 shr 8))) xor Key[3];

  // Конечное преобразование

  PLongWord(@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;

  const Key: TAESKey128; Dest: TStream);

var

  ExpandedKey: TAESExpandedKey128;

begin

  ExpandAESKeyForEncryption(Key, ExpandedKey);

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

  EncryptAESStreamECB(Source, Count, ExpandedKey, Dest);

end;

//Шифрование потока

procedure EncryptAESStreamECB(Source: TStream; Count: cardinal;

  const ExpandedKey: TAESExpandedKey128; Dest: TStream);

var

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

  Done: cardinal;

  st : TStringStream;

  tick : integer;

begin

  st := TStringStream.Create('');

  if Count = 0 then

  begin

    Source.Position := 0; //Указатель в начало потока

    Count := Source.Size; //Определение текущего размера потока

  end

  else Count := Min(Count, Source.Size - Source.Position);

  if Count = 0 then exit;

  while Count >= SizeOf(TAESBuffer) do

  begin

    Done := Source.Read(TempIn, SizeOf(TempIn)); //Считываем данные из потока

    if Done < SizeOf(TempIn) then

      raise EStreamError.Create(SReadError);

    EncryptAES(TempIn, ExpandedKey, TempOut);

    if flag = false then

    begin

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

      FmStop.EditIn.Text := IntToHex(TempIn[0],2)+IntToHex(TempIn[1],2)+

      IntToHex(TempIn[2],2)+IntToHex(TempIn[3],2)+IntToHex(TempIn[4],2)+

      IntToHex(TempIn[5],2)+IntToHex(TempIn[6],2)+IntToHex(TempIn[7],2)+

      IntToHex(TempIn[8],2)+IntToHex(TempIn[9],2)+IntToHex(TempIn[10],2)+

      IntToHex(TempIn[11],2)+IntToHex(TempIn[12],2)+IntToHex(TempIn[13],2)+

      IntToHex(TempIn[14],2)+IntToHex(TempIn[15],2); 

      FmStop.EditOut.Text := IntToHex(TempOut[0],2)+IntToHex(TempOut[1],2)+

      IntToHex(TempOut[2],2)+IntToHex(TempOut[3],2)+IntToHex(TempOut[4],2)+

      IntToHex(TempOut[5],2)+IntToHex(TempOut[6],2)+IntToHex(TempOut[7],2)+

      IntToHex(TempOut[8],2)+IntToHex(TempOut[9],2)+IntToHex(TempOut[10],2)+

      IntToHex(TempOut[11],2)+IntToHex(TempOut[12],2)+IntToHex(TempOut[13],2)+

      IntToHex(TempOut[14],2)+IntToHex(TempOut[15],2);

      //FmStop

    end;

    Done := Dest.Write(TempOut, SizeOf(TempOut));

    st.Write(TempOut, SizeOf(TempOut));

 

    Form1.MemoOut.Text := st.DataString;

    if Form1.RadioGroup1.ItemIndex  = 1 then

    begin

      tick := GetTickCount;

      repeat

        Application.ProcessMessages;

      until (GetTickCount-tick)>StrToInt(Form1.editDelay.Text);

Информация о работе Алгоритм шифрования AES