summaryrefslogtreecommitdiff
path: root/3544/DEPENDENCIES/Chapter_2.sci
blob: 3ec8f93e0b0aea2594877d10840a447d36a8c5fd (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274

//////////////////////////////////////
//          Caesar cipher           //
//////////////////////////////////////


//Generalised Caesar cipher encryption
function [ct] = encrypt_caesar_general(pt,key)
    a = ascii('A')
    l = length(pt)
    ct = zeros(l)
    
    for i =1:l
        if isletter(part(pt,i:i)) then
            ct(i) = a + modulo( ascii(part(pt,i:i))+key-a, 26 ) 
        else
            ct(i) = ascii( part(pt,i:i) )
        end
    end
    ct = char(ct)
    ct = strcat(ct)
endfunction

//Caesar cipher encryption (key = 3  always)
function [ct] = encrypt_caesar(pt)
    ct = encrypt_caesar_general(pt,3)
endfunction


//Generalised Caesar cipher decryption
function [pt] = decrypt_caesar_general(ct,key)
    a = ascii('A')
    key = 26-key
    l = length(ct)
    pt = zeros(l)
    
    for i =1:l
        if isletter(part(ct,i:i)) then
            pt(i) = a + modulo( ascii(part(ct,i:i))+key-a, 26 )  
        else
            pt(i) = ascii(part(ct,i:i));        
        end
    end
    pt = char(pt)
    pt = strcat(pt)
endfunction


//Caesar cipher decryption (key = 3  always)
function [pt] = decrypt_caesar(ct)
    pt = decrypt_caesar_general(ct,3)
endfunction



//////////////////////////////////////
//         Playfair cipher          //
//////////////////////////////////////



//func to remove spaces from a string
function [mat]=remove_spaces(str)
    mat=[]
    k=1
    for i=1:length(str)
        if ~isletter(part(str,i:i)) then
            continue
        end
        mat(k,1) = part(str,i:i)
        k=k+1
    end
    mat = strcat(mat)
endfunction


//func to substitute I for J
function [mat]=i_to_j(str)
    str = remove_spaces(str)
    mat=[]
    k=1
    for i=1:length(str)
        mat(k,1) = part(str,i:i)
        if mat(k,1)=='J' then
            mat(k,1) = 'I'
        end
        k = k+1
    end
    mat = strcat(mat)
endfunction

//func to insert X between repeating characters
function [mat]=handle_duplicates(str)
    mat = [] 
    l = length(str)
    k = 1
    
    for i=1:l
        if i>1 & part(str,i:i)==part(str,i-1:i-1) then
            mat(k,1)='X'
            k=k+1
        end
        mat(k,1) = part(str,i:i) 
        k = k+1
    end    
    mat = strcat(mat)
endfunction
 
//Matrix creation and population for Playfair cipher
//func to populate playfair matrix
function [mat]=playfair_matrix(key)
    
    key = i_to_j(key)
    a = ascii('A')
    i = ascii('I')
    j = ascii('J')
    row = 5
    col = 5
    visited = zeros(26,1);
    mat = ones(row,col);
    
    len = length(key)
    
    li=1
    k=1
    
    for m=1:row
        for n=1:col
            while li<=len & visited(ascii(part(key,li:li)) - ascii('A')+1,1)~=0,
                li=li+1
                if part(key,li:li)=='I' & visited(j-a+1)==1 | part(key,li:li)=='J' & visited(i-a+1)==1 then
                    li = li+1
                end
            end
            while k<=26 & visited(k,1)~=0
                k=k+1
                if k==i-a+1 & visited(j-a+1)==1 | k==j-a+1 & visited(i-a+1)==1 then
                    k = k+1
                end
            end
            if li<=len then
                mat(m,n) = ascii(part(key,li:li))
                visited(ascii(part(key,li:li))-a+1,1) = 1
            else
                mat(m,n) = k+ascii('A')-1    
                visited(k,1) = 1
            end
                    
        end
    end

endfunction

//func to check and convert plaintext to suitable format for encipherment using playfair cipher
function [mat]=playfair_pt(pt)
    mat = i_to_j(pt)
    mat = handle_duplicates(mat)
endfunction

function [mat]=digram_array(pt)
    k = 1
    l = length(pt)
    for i=1:l
        if modulo(i,2)==0 then
            continue
        end
        mat(k,1) = part(pt,i:i)
        i=i+1
        if i>l then
            mat(k,2) = 'X'
        else
            mat(k,2) = part(pt,i:i) 
        end
        k=k+1
    end
endfunction

function []=print_matrix(mat,new_line)
    [r,c] = size(mat)
    t = type(mat)

    for i=1:r
        for j=1:c
            if  t==[1] then         // real numbers return 1, characters return 10
                printf("%c ",ascii(mat(i,j)))
            else
                printf("%c ",mat(i,j))
            end
        end
        printf("   ")
        if new_line~=0 then
            printf("\n")
        end
    end
endfunction

function [r,c]=find_letter(key_mat,a)
    [row,col] = size(key_mat)
    r = 0
    c = 0
    for i=1:row
        for j=1:col
            if ascii(key_mat(i,j))==a then
                r=i
                c=j
                break
            end
        end
    end
endfunction


function [mat]=encrypt_playfair(pt_mat,key_mat)

    [row,col] = size(pt_mat)
    mat = []

    for i=1:row
        a = pt_mat(i,1)
        b = pt_mat(i,2)
        [r_a,c_a] = find_letter(key_mat,a)
        [r_b,c_b] = find_letter(key_mat,b)
        
        if r_a==r_b then
            c_a = modulo(c_a,5)+1
            c_b = modulo(c_b,5)+1
        elseif c_a==c_b then
            r_a = modulo(r_a,5)+1
            r_b = modulo(r_b,5)+1
        else
            temp = c_a
            c_a = c_b
            c_b = temp
        end
        mat(i,1) = ascii(key_mat(r_a,c_a))
        mat(i,2) = ascii(key_mat(r_b,c_b))        
        
    end
endfunction


//////////////////////////////////////
//      Transposition cipher        //
//////////////////////////////////////

function [mat]=message_rectangle(str,col)
    l = length(str)
    row = l/6
    if modulo(l,6)>0 then
        row=row+1
    end
    //remove whitespace and non-alphabets from string
    str = remove_spaces(str)
    //Conversion of plaintext into a message table
    mat = []
    k=1
    for i=1:row
        for j=1:col
            if k>l then
                break
            end
            mat(i,j) = part(str,k:k)
            k=k+1
        end
    end
endfunction

//////////////////////////////////////
//   Diffie-Hellman Key Exchange    //
//////////////////////////////////////

function [key]=diffie_key(g,p,n)
    key = modulo(g^p,n)
endfunction