· 5 years ago · Nov 18, 2019, 04:52 PM
1sdes:-
2https://raw.githubusercontent.com/GeoffreyVDB/SDES-decryption/master/SDES.py
3
4__author__ = 'geoffrey'
5
6# parameters
7key = "0111111101"
8cipher = "10100010"
9
10
11P10 = (3, 5, 2, 7, 4, 10, 1, 9, 8, 6)
12P8 = (6, 3, 7, 4, 8, 5, 10, 9)
13P4 = (2, 4, 3, 1)
14
15IP = (2, 6, 3, 1, 4, 8, 5, 7)
16IPi = (4, 1, 3, 5, 7, 2, 8, 6)
17
18E = (4, 1, 2, 3, 2, 3, 4, 1)
19
20S0 = [
21 [1, 0, 3, 2],
22 [3, 2, 1, 0],
23 [0, 2, 1, 3],
24 [3, 1, 3, 2]
25 ]
26
27S1 = [
28 [0, 1, 2, 3],
29 [2, 0, 1, 3],
30 [3, 0, 1, 0],
31 [2, 1, 0, 3]
32 ]
33
34def permutation(perm, key):
35 permutated_key = ""
36 for i in perm:
37 permutated_key += key[i-1]
38
39 return permutated_key
40
41def generate_first_key(left_key, right_key):
42 left_key_rot = left_key[1:] + left_key[:1]
43 right_key_rot = right_key[1:] + right_key[:1]
44 key_rot = left_key_rot + right_key_rot
45 return permutation(P8, key_rot)
46
47def generate_second_key(left_key, right_key):
48 left_key_rot = left_key[3:] + left_key[:3]
49 right_key_rot = right_key[3:] + right_key[:3]
50 key_rot = left_key_rot + right_key_rot
51 return permutation(P8, key_rot)
52
53def F(right, subkey):
54 expanded_cipher = permutation(E, right)
55 xor_cipher = bin( int(expanded_cipher, 2) ^ int(subkey, 2) )[2:].zfill(8)
56 left_xor_cipher = xor_cipher[:4]
57 right_xor_cipher = xor_cipher[4:]
58 left_sbox_cipher = Sbox(left_xor_cipher, S0)
59 right_sbox_cipher = Sbox(right_xor_cipher, S1)
60 return permutation(P4, left_sbox_cipher + right_sbox_cipher)
61
62def Sbox(input, sbox):
63 row = int(input[0] + input[3], 2)
64 column = int(input[1] + input[2], 2)
65 return bin(sbox[row][column])[2:].zfill(4)
66
67def f(first_half, second_half, key):
68 left = int(first_half, 2) ^ int(F(second_half, key), 2)
69 print "Fk: " + bin(left)[2:].zfill(4) + second_half
70 return bin(left)[2:].zfill(4), second_half
71
72p10key = permutation(P10, key)
73left = p10key[:len(p10key)/2]
74right = p10key[len(p10key)/2:]
75
76first_key = generate_first_key(left, right)
77second_key = generate_second_key(left, right)
78print "[*] First key: " + first_key
79print "[*] Second key: " + second_key
80
81permutated_cipher = permutation(IP, cipher)
82print "IP: " + permutated_cipher
83first_half_cipher = permutated_cipher[:len(permutated_cipher)/2]
84second_half_cipher = permutated_cipher[len(permutated_cipher)/2:]
85
86left, right = f(first_half_cipher, second_half_cipher, second_key)
87print "SW: " + right + left
88left, right = f(right, left, first_key) # switch left and right!
89
90print "IP^-1: " + permutation(IPi, left + right)
91
92S-aes
93from simple_aes_cipher import AESCipher, generate_secret_key
94
95pass_phrase = "hogefuga"
96secret_key = generate_secret_key(pass_phrase)
97
98# generate cipher
99cipher = AESCipher(secret_key)
100
101raw_text = "arjunsihag"
102encrypt_text = cipher.encrypt(raw_text)
103assert raw_text != encrypt_text
104
105decrypt_text = cipher.decrypt(encrypt_text)
106assert encrypt_text != decrypt_text
107assert decrypt_text == raw_text
108
109print(encrypt_text)
110
111--------------------
112
113LLFSR:-
114def lfsr(seed, taps):
115 sr, xor = seed, 0
116 while 1:
117 for t in taps:
118 xor += int(sr[t-1])
119 if xor%2 == 0.0:
120 xor = 0
121 else:
122 xor = 1
123 print xor
124 sr, xor = str(xor) + sr[:-1], 0
125 print sr
126 if sr == seed:
127 break
128
129lfsr('11001001', (8,7,6,1)) #example
130
131-----------------
132LCG:-
133def bsd_rand(seed):
134 def rand():
135 rand.seed = (1103515245*rand.seed + 12345) & 0x7fffffff
136 return rand.seed
137 rand.seed = seed
138 return rand
139
140def msvcrt_rand(seed):
141 def rand():
142 rand.seed = (214013*rand.seed + 2531011) & 0x7fffffff
143 return rand.seed >> 16
144 rand.seed = seed
145 return rand
146
147 print(msvcrt_rand)
148
149-------------------
150
151AES
152from simple_aes_cipher import AESCipher, generate_secret_key
153
154pass_phrase = "hogefuga"
155secret_key = generate_secret_key(pass_phrase)
156
157# generate cipher
158cipher = AESCipher(secret_key)
159
160raw_text = "abcdefg"
161encrypt_text = cipher.encrypt(raw_text)
162assert raw_text != encrypt_text
163
164decrypt_text = cipher.decrypt(encrypt_text)
165assert encrypt_text != decrypt_text
166assert decrypt_text == raw_text
167
168---------------------------------------
169
170elgamal
171
172import random
173from math import pow
174
175a = random.randint(2, 10)
176
177def gcd(a, b):
178 if a < b:
179 return gcd(b, a)
180 elif a % b == 0:
181 return b;
182 else:
183 return gcd(b, a % b)
184
185# Generating large random numbers
186def gen_key(q):
187
188 key = random.randint(pow(10, 20), q)
189 while gcd(q, key) != 1:
190 key = random.randint(pow(10, 20), q)
191
192 return key
193
194# Modular exponentiation
195def power(a, b, c):
196 x = 1
197 y = a
198
199 while b > 0:
200 if b % 2 == 0:
201 x = (x * y) % c;
202 y = (y * y) % c
203 b = int(b / 2)
204
205 return x % c
206
207# Asymmetric encryption
208def encrypt(msg, q, h, g):
209
210 en_msg = []
211
212 k = gen_key(q)# Private key for sender
213 s = power(h, k, q)
214 p = power(g, k, q)
215
216 for i in range(0, len(msg)):
217 en_msg.append(msg[i])
218
219 print("g^k used : ", p)
220 print("g^ak used : ", s)
221 for i in range(0, len(en_msg)):
222 en_msg[i] = s * ord(en_msg[i])
223
224 return en_msg, p
225
226def decrypt(en_msg, p, key, q):
227
228 dr_msg = []
229 h = power(p, key, q)
230 for i in range(0, len(en_msg)):
231 dr_msg.append(chr(int(en_msg[i]/h)))
232
233 return dr_msg
234
235# Driver code
236def main():
237
238 msg = 'encryption'
239 print("Original Message :", msg)
240
241 q = random.randint(pow(10, 20), pow(10, 50))
242 g = random.randint(2, q)
243
244 key = gen_key(q)# Private key for receiver
245 h = power(g, key, q)
246 print("g used : ", g)
247 print("g^a used : ", h)
248
249 en_msg, p = encrypt(msg, q, h, g)
250 dr_msg = decrypt(en_msg, p, key, q)
251 dmsg = ''.join(dr_msg)
252 print("Decrypted Message :", dmsg);
253
254
255if __name__ == '__main__':
256 main()