0% found this document useful (0 votes)
33 views4 pages

Sprawozdanie 4

The document describes an implementation of a stream cipher using linear feedback shift registers (LFSRs) in Object Pascal. It presents code for encrypting and decrypting data using different LFSR configurations defined by their feedback polynomials. The code takes in an N value to select the specific polynomial, initializes an LFSR with a key, XORs the LFSR output with plaintext to encrypt, and decrypts by re-initializing the LFSR and XORing again. Test runs are shown encrypting and decrypting sample text for N=5 and N=15.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views4 pages

Sprawozdanie 4

The document describes an implementation of a stream cipher using linear feedback shift registers (LFSRs) in Object Pascal. It presents code for encrypting and decrypting data using different LFSR configurations defined by their feedback polynomials. The code takes in an N value to select the specific polynomial, initializes an LFSR with a key, XORs the LFSR output with plaintext to encrypt, and decrypts by re-initializing the LFSR and XORing again. Test runs are shown encrypting and decrypting sample text for N=5 and N=15.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

12.06.

2014
Piotr Karwowski
Informatyka, Semestr VI
Politechnika Białostocka

Bezpieczeństwo Sieci Komputerowych


Prof. Dr. V. N. Yarmolik

SPRAWOZDANIE nr. 4

SZYFRY STRUMIENIOWE
Zadanie 1

Wykonaj program realizujący szyfrowanie i deszyfrowanie z wykorzystaniem n-bitowego LFSR –


liniowego rejestru przesuwnego ze sprzężeniem zwrotnym według wielomianu fi(x);

Algorytm zakodowałem w języku Object Pascal. Środowisko testowe: Windows 7 x64bit, Delphi 7.

Kod źródłowy:

program Project1;

{$APPTYPE CONSOLE}

uses
SysUtils, Windows;

const
wartosc_poczatkowa = $a9fed863;

var
lfsr: dword = wartosc_poczatkowa;
bit: byte;
n: byte;
i: dword;
testowe: array[0..7] of byte = ($aa, $bb, $cc, $dd, $ee, $ff, 1,
2);

procedure zaktualizuj_lfsr;
var
z: byte;
begin
//
for z := 1 to 32 do
begin
case n of
1: bit:= (lfsr);
2: bit:= (lfsr) xor (lfsr shr 2);
3: bit:= (lfsr) xor (lfsr shr 3);
4: bit:= (lfsr) xor (lfsr shr 4);
5: bit:= (lfsr shr 2) xor (lfsr shr 5);
6: bit:= (lfsr) xor (lfsr shr 6);
7: bit:= (lfsr) xor (lfsr shr 7);
8: bit:= (lfsr) xor (lfsr shr 5) xor (lfsr shr 6) xor (lfsr
shr 8);
9: bit:= (lfsr shr 4) xor (lfsr shr 9);
10: bit:= (lfsr shr 3) xor (lfsr shr 10);
11: bit:= (lfsr shr 2) xor (lfsr shr 11);
12: bit:= (lfsr shr 3) xor (lfsr shr 4) xor (lfsr shr 7) xor
(lfsr shr 12);
13: bit:= lfsr xor (lfsr shr 3) xor (lfsr shr 4) xor (lfsr shr
13);
14: bit:= lfsr xor (lfsr shr 11) xor (lfsr shr 12) xor (lfsr
shr 14);
15: bit:=lfsr xor (lfsr shr 15);
16: bit:=(lfsr shr 2) xor (lfsr shr 3) xor (lfsr shr 5) xor
(lfsr shr 16);
17: bit:=(lfsr shr 3) xor (lfsr shr 17);
18: bit:=(lfsr shr 7) xor (lfsr shr 18);
19: bit:=lfsr xor (lfsr shr 2) xor (lfsr shr 5) xor (lfsr shr
19);
20: bit:=(lfsr shr 3) xor (lfsr shr 20);
21: bit:=(lfsr shr 2) xor (lfsr shr 21);
22: bit:=lfsr xor (lfsr shr 22);
23: bit:=(lfsr shr 5) xor (lfsr shr 23);
24: bit:=(lfsr shr 3) xor (lfsr shr 4) xor (lfsr shr 9) xor
(lfsr shr 24);
25: bit:=(lfsr shr 3) xor (lfsr shr 25);
26: bit:=lfsr xor (lfsr shr 2) xor (lfsr shr 6) xor (lfsr shr
26);
27: bit:=lfsr xor (lfsr shr 2) xor (lfsr shr 5) xor (lfsr shr
27);
28: bit:=(lfsr shr 3) xor (lfsr shr 28);
end;
bit := bit and 1;
// przesuwa status lfsr w prawo o 1, oraz "wrzuca" do rejestru
przesuwnego
// wartosc bitu z poprzednich wyliczen
lfsr := (lfsr shr 1) or (bit shl 31);
end;
end;

begin
try
{ TODO -oUser -cConsole Main : Insert code here }
write('Podaj N: ');
readln(n);
writeln('Startowy LFSR (klucz): 0x'+IntToHex(lfsr,8));
write('Tekst JAWNY: ');
for i := Low(testowe) to High(Testowe) do
begin
write(inttohex(testowe[i], 2));
end;
writeln('');
write('Tekst ZASZYFROWANY: ');
// Generuj LFSR oraz xoruj tekst jawny
for i := Low(testowe) to High(Testowe) do
begin
zaktualizuj_lfsr();
testowe[i] := testowe[i] xor (lfsr and $ff);
write(inttohex(testowe[i], 2));
end;
// Przywroc wartosc poczatkowa - w celu ODszyfrowania
writeln('');
write('Teskt ODSZYFROWANY: ');
lfsr := wartosc_poczatkowa;
for i := Low(testowe) to High(Testowe) do
begin
zaktualizuj_lfsr();
testowe[i] := testowe[i] xor (lfsr and $ff);
write(inttohex(testowe[i], 2));
end;
readln(n);
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
end.

Test algorytmu:

Podaj N: 5
Startowy LFSR (klucz): 0xA9FED863
Tekst JAWNY: AABBCCDDEEFF0102
Tekst ZASZYFROWANY: 718BD19ADDAA3EBC
Teskt ODSZYFROWANY: AABBCCDDEEFF0102

Podaj N: 15
Startowy LFSR (klucz): 0xA9FED863
Tekst JAWNY: AABBCCDDEEFF0102
Tekst ZASZYFROWANY: 34A23425F5694194
Teskt ODSZYFROWANY: AABBCCDDEEFF0102

Program, po podaniu wartosci N, wybiera odpowiedni wielomian funkcji LFSR, zgodny z


wymaganiami zadania. Procedura zaktualizuj_lfsr generuje BIT na podstawie wybranej funkcji, a
następnie przesuwa zawartość rejestru przesuwnego LFSR w prawo o 1 bit, oraz wykonuje
logiczną operację OR na bicie który „wyleciał” spoza zakresu. Operacja ta jest wykonywana 32
razy, tak aby cała zawartość rejestru została zmodyfikowana. W ten sposób rejestr posłuży jako
klucz xorujący na wejściowym strumieniu danych. Po zaszyfrowaniu pewnego stałego tesktu
jawnego, jest on odszyfrowywany w ten sam sposób – po ówcześniejszym przwyróceniu stanu
początkowego rejestru LFSR (czyli naszego klucza).

You might also like