Sei sulla pagina 1di 35

DESCRIPTION OF RAIL FENCE

The plaintext is written


downwards and diagonally on
successive "rails" of an
imaginary fence, then moving up
when we reach the bottom rail.
When we reach the top rail, the
message is written downwards
again until the whole plaintext
is written out. The message is
then read off in rows.
ZIG ZAG CRYPTOGRAPHY
3 D H

2 N Y A

1 A S
1 2 3 4 5 6 7

Created By: Andysah, Zara,


Sany, Sri, Zuhri
LINKED LIST

A B C

Information Pointer
ADDING THE NODE
procedure AddNode(var First, Last : pNode; ch : char; Column, Row : byte);
var
Temp : pNode;
begin
new(Temp);
if ord(ch) = 32 then
Temp^.Space := true
else
Temp^.Space := false;
Temp^.Content := ch;
Temp^.Column := Column;
Temp^.Row := Row;

if First = nil then


First := Temp
else
Last^.Next := Temp;

Last := Temp;
end;
ENCRYPTION
Algorithm:
- Make a linear tree
- Record the column & row
- Get the data from the plain
text according to the row
position
- Return the cipher text

Here We Go !!!
LINEAR TREE
D H

N Y A

A S

PLAIN : ANDYSAH
CIPHER : DHNYAAS
EXPLANATION
3 D H

2 N Y A

1 A S
1 2 3 4 5 6 7

Tree contains of columns &


rows.
INFORMATION RECORD
type
pNode = ^tNode;
tNode = record
Content : char;
Column : byte;
Row : byte;
Next : pNode;
end;
BUILD THE TREE
procedure BuildTree(var First, Last : pNode; s : string);
var
i : byte;
Column, Row : byte;
isUp : boolean;
begin
isUp := true;
Column := 1; Row := 1;

for i := 1 to length(s) do
begin
AddNode(First, Last, s[i], Column, Row);
inc(Column);
if (isUp) and (Row < Step) then
inc(Row)
else if (Row <= Step) and (Row > 1) then
begin
isUp := false;
dec(Row);
end
else if (isUp = false) and (Row = 1) then
begin
isUp := true;
inc(Row);
end;
end;
end;
INFORMATION RECORD
Data 1 : A, 1, 1
Data 2 : N, 2, 2
Data 3 : D, 3, 3
Data 4 : Y, 4, 2
Data 5 : S, 5, 1
Data 6 : A, 6, 2
Data 7 : H, 7, 3

rd th
The 3 and 7 columns are
peak rows.
SORTED-LIST
Data 3 : D, 3, 3
Data 7 : H, 7, 3
Data 2 : N, 2, 2
Data 4 : Y, 4, 2
Data 6 : A, 6, 2
Data 1 : A, 1, 1
Data 5 : S, 5, 1

Sort the row in descending


order & the column at the
same rows in ascending.
ILLUSTRATION
3 D H

2 N Y A

1 A S
1 2 3 4 5 6 7
rd
The 3 Row
Cipher := DH
ILLUSTRATION
3 D H

2 N Y A

1 A S
1 2 3 4 5 6 7
nd
The 2 Row
Cipher := DH+NYA
ILLUSTRATION
3 D H

2 N Y A

1 A S
1 2 3 4 5 6 7
st
The 1 Row
Cipher := DH+NYA+AS
CIPHER TEXT FUNCTION
function ToCipherText(Node : pNode) : string;
var
Result : string;
Row : byte;
Point : pNode;
begin
Row := Step;
Point := Node;
k := 1;
Result := PlainText;

for i := Step downto 1 do


begin
for j := 1 to length(PlainText) do
begin
if Point^.Row = i then
begin
Result[k] := Point^.Content;
inc(k);
end;
Point := Point^.Next;
end;
Point := Node;
end;
ToCipherText := Result;
end;
DECRYPTION
Algorithm:
- Get the text length
- Make a temporary tree
- Replace the tree content with
the cipher text according to
its column and row position
- Return the plain text

Here We Go !!!
GET THE TEXT LENGTH
Cipher Text : DHNYAAS
Text Length : 7

D H N Y A A S
1 2 3 4 5 6 7
MAKE A TEMPORARY TREE
3 X X

2 X X X

1 X X
1 2 3 4 5 6 7

Now we make the same length


temporary tree.
INFORMATION RECORD
Data 1 : X, 1, 1
Data 2 : X, 2, 2
Data 3 : X, 3, 3
Data 4 : X, 4, 2
Data 5 : X, 5, 1
Data 6 : X, 6, 2
Data 7 : X, 7, 3

rd th
The 3 and 7 columns are
peak rows.
RE-SORTING
Data 3 : X, 3, 3 => Character 1
Data 7 : X, 7, 3 => Character 2
Data 2 : X, 2, 2 => Character 3
Data 4 : X, 4, 2 => Character 4
Data 6 : X, 6, 2 => Character 5
Data 1 : X, 1, 1 => Character 6
Data 5 : X, 5, 1 => Character 7

Sort the row in descending


order & the column at the
same rows in ascending.
WE'VE GOT THE RULE
Temporary Text : XXXXXXX
Text Length : 7

X X X X X X X
1 2 3 4 5 6 7
3 2 1
Cipher Text : DHNYAAS
DH := 3rd, NYA := 2nd AS := 1st
RE-PLACE
Temporary Text : XXXXXXX
Text Length : 7

D H N Y A A S
1 2 3 4 5 6 7
3 2 1
Cipher Text : DHNYAAS
DH := 3rd, NYA := 2nd AS := 1st
RE-TREE POSITION
3 D H

2 X X X

1 X X
1 2 3 4 5 6 7
rd
The 3 Row
Cipher := DH
RE-TREE POSITION
3 D H

2 N Y A

1 X X
1 2 3 4 5 6 7
nd
The 2 Row
Cipher := DH+NYA
RE-TREE POSITION
3 D H

2 N Y A

1 A S
1 2 3 4 5 6 7
st
The 1 Row
Cipher := DH+NYA+AS
TREE TO TEXT
Data 1 : A, 1 , 1
Data 2 : N, 2 , 2
Data 3 : D, 3 , 3
Data 4 : Y, 4 , 2
Data 5 : S, 5 , 1
Data 6 : A, 6 , 2
Data 7 : H, 7 , 3

Get the character for the


first to last column. Column
is always sorted.
ILLUSTRATION
3 D H

2 N Y A

1 A S
1 2 3 4 5 6 7
st
The 1 Column
Plain := A
ILLUSTRATION
3 D H

2 N Y A

1 A S
1 2 3 4 5 6 7
nd
The 2 Column
Plain := AN
ILLUSTRATION
3 D H

2 N Y A

1 A S
1 2 3 4 5 6 7
rd
The 3 Column
Plain := AND
ILLUSTRATION
3 D H

2 N Y A

1 A S
1 2 3 4 5 6 7
th
The 4 Column
Plain := ANDY
ILLUSTRATION
3 D H

2 N Y A

1 A S
1 2 3 4 5 6 7
th
The 5 Column
Plain := ANDYS
ILLUSTRATION
3 D H

2 N Y A

1 A S
1 2 3 4 5 6 7
th
The 6 Column
Plain := ANDYSA
ILLUSTRATION
3 D H

2 N Y A

1 A S
1 2 3 4 5 6 7
th
The 7 Column
Plain := ANDYSAH
PLAIN TEXT FUNCTION
function ToPlainText(Node : pNode) : string;
var
Result : string;
Temp : pNode;
begin
Temp := Node; k := 1;
for i := Step downto 1 do
begin
for j := 1 to length(PlainText) do
begin
if Temp^.Row = i then
begin
Temp^.Content := CipherText[k];
inc(k);
End; Temp := Temp^.Next;
End; Temp := Node;
end;

Result := PlainText;
for i := 1 to length(PlainText) do
begin
Result[Temp^.Column] := Temp^.Content;
Temp := Temp^.Next;
end;
ToPlainText := Result;
end;
THANK YOU
FOR
WATCHING, BYE !!!

Potrebbero piacerti anche