summaryrefslogtreecommitdiff
path: root/3544/CH2/EX2.34/Ex2_34.sce
blob: 877595ea6c7fcb01a6afbba50e1aa0abc3e4359f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//////////////////////////////////////////
//										//
//(a) Encrytion scheme of hill cipher   //
//										//
//////////////////////////////////////////


//PLaintext 
pt = "CAT"

disp("Plaintext: ")
disp(pt)

l = length(pt)
pt = strsplit(pt)

a = ascii("A")
pt_mat = []

//Taking A=0,B=1,C=2,etc.
for i=1:l
    pt_mat(i,1)=ascii(pt(i,1))-a
end

disp("Plaintext matrix:")
disp(pt_mat)

//Key matrix 
key_mat = [6 24 1; 13 16 10;20 17 15]
disp("Encryption Key matrix:")
disp(key_mat)

//ciphertext matrix
ct_mat = key_mat * pt_mat

disp("Product: ")
disp(ct_mat)
[r,c]=size(ct_mat)

//Taking mod for correct conversion
for i=1:r
    ct_mat(i,1) = modulo(ct_mat(i,1),26)
end

disp("Ciphertext matrix: ")
disp(ct_mat)

disp("Ciphertext: ")

//Conversion of code to letters
ct=[]
for i=1:r
    ct(i,1) = ascii(ct_mat(i,1)+a)
end
ct = strcat(ct)
disp(ct)



//////////////////////////////////////////
//										//
//(b) Decrytion scheme of hill cipher   //
//										//
//////////////////////////////////////////

//Ciphertext 
disp("Ciphertext: ")
disp(ct)

l = length(ct)
ct = strsplit(ct)

a = ascii("A")
ct_mat = []

//Taking A=0,B=1,C=2,etc.
for i=1:l
    ct_mat(i,1)=ascii(ct(i,1))-a
end

disp("Ciphertext matrix:")
disp(ct_mat)

//Key matrix for decryption (inverse of encryption key matrix)
key_mat = [8 5 10; 21 8 21;21 12 8]
disp("Decryption Key matrix:")
disp(key_mat)

//ciphertext matrix
pt_mat = key_mat * ct_mat

disp("Product: ")
disp(pt_mat)
[r,c]=size(pt_mat)

//Taking mod for correct conversion
for i=1:r
    pt_mat(i,1) = modulo(pt_mat(i,1),26)
end

disp("Plaintext matrix: ")
disp(pt_mat)

disp("Plaintext: ")

//Conversion of code to letters
pt=[]
for i=1:r
    pt(i,1) = ascii(pt_mat(i,1)+a)
end
pt = strcat(pt)
disp(pt)