From a551b11de6ce7c7a550b021bda2dd8f68d3e1e70 Mon Sep 17 00:00:00 2001 From: Sri Suma <41474384+srisumapasumarthi@users.noreply.github.com> Date: Wed, 16 Oct 2019 22:25:51 -0700 Subject: [PATCH 1/4] Simplified DES --- other/sdes.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 other/sdes.py diff --git a/other/sdes.py b/other/sdes.py new file mode 100644 index 000000000000..2a51021397ba --- /dev/null +++ b/other/sdes.py @@ -0,0 +1,86 @@ +def apply_table(inp, table): + res = '' + for i in table: + res += inp[i-1] + return res + +def left_shift(data): + li = [] + for i in range(len(data)-1): + li.append(data[i+1]) + li.append(data[0]) + return ''.join(li) + +def XOR(a, b): + res = '' + for i in range(len(a)): + if a[i]==b[i]: + res += '0' + else: + res += '1' + return res + +def apply_sbox(s, data): + row = int('0b'+data[0]+data[-1], 2) + col = int('0b'+data[1:3], 2) + return bin(s[row][col])[2:] + +def function(expansion, s0, s1, key, message): + left = message[:4] + right = message[4:] + temp = apply_table(right, expansion) + temp = XOR(temp, key) + l = apply_sbox(s0, temp[:4]) + r = apply_sbox(s1, temp[4:]) + l = '0'*(2-len(l)) + l + r = '0'*(2-len(r)) + r + temp = apply_table(l+r, p4_table) + temp = XOR(left, temp) + return temp+right + + +key = input('Enter 10 bit key: ') +message = input('Enter 8 bit message: ') + +p8_table = [6, 3, 7, 4, 8, 5, 10, 9] +p10_table = [3, 5, 2, 7, 4, 10, 1, 9, 8, 6] +p4_table = [2, 4, 3, 1] +IP = [2, 6, 3, 1, 4, 8, 5, 7] +IP_inv = [4, 1, 3, 5, 7, 2, 8, 6] +expansion = [4, 1, 2, 3, 2, 3, 4, 1] +s0 = [[1, 0, 3, 2], + [3, 2, 1, 0], + [0, 2, 1, 3], + [3, 1, 3, 2]] +s1 = [[0, 1, 2, 3], + [2, 0, 1, 3], + [3, 0, 1, 0], + [2, 1, 0, 3]] + +#encryption +temp = apply_table(key, p10_table) +left = temp[:5] +right = temp[5:] +left = left_shift(left) +right = left_shift(right) +key1 = apply_table(left+right, p8_table) +left = left_shift(left) +right = left_shift(right) +left = left_shift(left) +right = left_shift(right) +key2 = apply_table(left+right, p8_table) + +temp = apply_table(message, IP) +temp = function(expansion, s0, s1, key1, temp) +temp = temp[4:]+temp[:4] +temp = function(expansion, s0, s1, key2, temp) +CT = apply_table(temp, IP_inv) +print('Cipher text is: ', CT) + +#decryption +temp = apply_table(CT, IP) +temp = function(expansion, s0, s1, key2, temp) +temp = temp[4:]+temp[:4] +temp = function(expansion, s0, s1, key1, temp) +PT = apply_table(temp, IP_inv) +print('Plain text after decypting is: ', PT) From 06b2b20193aaa7209f76b9364b2da7bf2a9a2bf0 Mon Sep 17 00:00:00 2001 From: Sri Suma <41474384+srisumapasumarthi@users.noreply.github.com> Date: Wed, 30 Oct 2019 10:26:29 +0530 Subject: [PATCH 2/4] Add files via upload Diffie Hellman algorithm to generate a secret key. --- ciphers/diffie.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 ciphers/diffie.py diff --git a/ciphers/diffie.py b/ciphers/diffie.py new file mode 100644 index 000000000000..6b0cca1f45e6 --- /dev/null +++ b/ciphers/diffie.py @@ -0,0 +1,26 @@ +def find_primitive(n): + for r in range(1, n): + li = [] + for x in range(n-1): + val = pow(r,x,n) + if val in li: + break + li.append(val) + else: + return r + + +if __name__ == "__main__": + q = int(input('Enter a prime number q: ')) + a = find_primitive(q) + a_private = int(input('Enter private key of A: ')) + a_public = pow(a, a_private, q) + b_private = int(input('Enter private key of B: ')) + b_public = pow(a, b_private, q) + + a_secret = pow(b_public, a_private, q) + b_secret = pow(a_public, b_private, q) + + print('The key value generated by A is: ', a_secret) + print('The key value generated by B is: ', b_secret) + From d4b560da864cac20ba378ac10805f62f7f250069 Mon Sep 17 00:00:00 2001 From: Sri Suma <41474384+srisumapasumarthi@users.noreply.github.com> Date: Wed, 30 Oct 2019 16:31:55 +0530 Subject: [PATCH 3/4] Update sdes.py --- other/sdes.py | 90 +++++++++++++++++++++++++-------------------------- 1 file changed, 44 insertions(+), 46 deletions(-) diff --git a/other/sdes.py b/other/sdes.py index 2a51021397ba..678456bb0918 100644 --- a/other/sdes.py +++ b/other/sdes.py @@ -5,11 +5,7 @@ def apply_table(inp, table): return res def left_shift(data): - li = [] - for i in range(len(data)-1): - li.append(data[i+1]) - li.append(data[0]) - return ''.join(li) + return data[1:]+data[0] def XOR(a, b): res = '' @@ -38,49 +34,51 @@ def function(expansion, s0, s1, key, message): temp = XOR(left, temp) return temp+right +if __name__ == "__main__": -key = input('Enter 10 bit key: ') -message = input('Enter 8 bit message: ') + key = input('Enter 10 bit key: ') + message = input('Enter 8 bit message: ') -p8_table = [6, 3, 7, 4, 8, 5, 10, 9] -p10_table = [3, 5, 2, 7, 4, 10, 1, 9, 8, 6] -p4_table = [2, 4, 3, 1] -IP = [2, 6, 3, 1, 4, 8, 5, 7] -IP_inv = [4, 1, 3, 5, 7, 2, 8, 6] -expansion = [4, 1, 2, 3, 2, 3, 4, 1] -s0 = [[1, 0, 3, 2], - [3, 2, 1, 0], - [0, 2, 1, 3], - [3, 1, 3, 2]] -s1 = [[0, 1, 2, 3], - [2, 0, 1, 3], - [3, 0, 1, 0], - [2, 1, 0, 3]] + p8_table = [6, 3, 7, 4, 8, 5, 10, 9] + p10_table = [3, 5, 2, 7, 4, 10, 1, 9, 8, 6] + p4_table = [2, 4, 3, 1] + IP = [2, 6, 3, 1, 4, 8, 5, 7] + IP_inv = [4, 1, 3, 5, 7, 2, 8, 6] + expansion = [4, 1, 2, 3, 2, 3, 4, 1] + s0 = [[1, 0, 3, 2], + [3, 2, 1, 0], + [0, 2, 1, 3], + [3, 1, 3, 2]] + s1 = [[0, 1, 2, 3], + [2, 0, 1, 3], + [3, 0, 1, 0], + [2, 1, 0, 3]] -#encryption -temp = apply_table(key, p10_table) -left = temp[:5] -right = temp[5:] -left = left_shift(left) -right = left_shift(right) -key1 = apply_table(left+right, p8_table) -left = left_shift(left) -right = left_shift(right) -left = left_shift(left) -right = left_shift(right) -key2 = apply_table(left+right, p8_table) + #key generation + temp = apply_table(key, p10_table) + left = temp[:5] + right = temp[5:] + left = left_shift(left) + right = left_shift(right) + key1 = apply_table(left+right, p8_table) + left = left_shift(left) + right = left_shift(right) + left = left_shift(left) + right = left_shift(right) + key2 = apply_table(left+right, p8_table) -temp = apply_table(message, IP) -temp = function(expansion, s0, s1, key1, temp) -temp = temp[4:]+temp[:4] -temp = function(expansion, s0, s1, key2, temp) -CT = apply_table(temp, IP_inv) -print('Cipher text is: ', CT) + #encryption + temp = apply_table(message, IP) + temp = function(expansion, s0, s1, key1, temp) + temp = temp[4:]+temp[:4] + temp = function(expansion, s0, s1, key2, temp) + CT = apply_table(temp, IP_inv) + print('Cipher text is: ', CT) -#decryption -temp = apply_table(CT, IP) -temp = function(expansion, s0, s1, key2, temp) -temp = temp[4:]+temp[:4] -temp = function(expansion, s0, s1, key1, temp) -PT = apply_table(temp, IP_inv) -print('Plain text after decypting is: ', PT) + #decryption + temp = apply_table(CT, IP) + temp = function(expansion, s0, s1, key2, temp) + temp = temp[4:]+temp[:4] + temp = function(expansion, s0, s1, key1, temp) + PT = apply_table(temp, IP_inv) + print('Plain text after decypting is: ', PT) From 4a45abc556ac02c3ca8e548cc2f041cc5cb16c8e Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 30 Oct 2019 12:47:50 +0100 Subject: [PATCH 4/4] Format code with psf/black and add doctests --- other/sdes.py | 81 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 34 deletions(-) diff --git a/other/sdes.py b/other/sdes.py index 678456bb0918..3038ff193ae9 100644 --- a/other/sdes.py +++ b/other/sdes.py @@ -1,43 +1,62 @@ def apply_table(inp, table): - res = '' + """ + >>> apply_table("0123456789", list(range(10))) + '9012345678' + >>> apply_table("0123456789", list(range(9, -1, -1))) + '8765432109' + """ + res = "" for i in table: - res += inp[i-1] + res += inp[i - 1] return res + def left_shift(data): - return data[1:]+data[0] + """ + >>> left_shift("0123456789") + '1234567890' + """ + return data[1:] + data[0] + def XOR(a, b): - res = '' + """ + >>> XOR("01010101", "00001111") + '01011010' + """ + res = "" for i in range(len(a)): - if a[i]==b[i]: - res += '0' + if a[i] == b[i]: + res += "0" else: - res += '1' + res += "1" return res + def apply_sbox(s, data): - row = int('0b'+data[0]+data[-1], 2) - col = int('0b'+data[1:3], 2) + row = int("0b" + data[0] + data[-1], 2) + col = int("0b" + data[1:3], 2) return bin(s[row][col])[2:] + def function(expansion, s0, s1, key, message): left = message[:4] - right = message[4:] + right = message[4:] temp = apply_table(right, expansion) temp = XOR(temp, key) l = apply_sbox(s0, temp[:4]) r = apply_sbox(s1, temp[4:]) - l = '0'*(2-len(l)) + l - r = '0'*(2-len(r)) + r - temp = apply_table(l+r, p4_table) + l = "0" * (2 - len(l)) + l + r = "0" * (2 - len(r)) + r + temp = apply_table(l + r, p4_table) temp = XOR(left, temp) - return temp+right - + return temp + right + + if __name__ == "__main__": - key = input('Enter 10 bit key: ') - message = input('Enter 8 bit message: ') + key = input("Enter 10 bit key: ") + message = input("Enter 8 bit message: ") p8_table = [6, 3, 7, 4, 8, 5, 10, 9] p10_table = [3, 5, 2, 7, 4, 10, 1, 9, 8, 6] @@ -45,40 +64,34 @@ def function(expansion, s0, s1, key, message): IP = [2, 6, 3, 1, 4, 8, 5, 7] IP_inv = [4, 1, 3, 5, 7, 2, 8, 6] expansion = [4, 1, 2, 3, 2, 3, 4, 1] - s0 = [[1, 0, 3, 2], - [3, 2, 1, 0], - [0, 2, 1, 3], - [3, 1, 3, 2]] - s1 = [[0, 1, 2, 3], - [2, 0, 1, 3], - [3, 0, 1, 0], - [2, 1, 0, 3]] + s0 = [[1, 0, 3, 2], [3, 2, 1, 0], [0, 2, 1, 3], [3, 1, 3, 2]] + s1 = [[0, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 0], [2, 1, 0, 3]] - #key generation + # key generation temp = apply_table(key, p10_table) left = temp[:5] right = temp[5:] left = left_shift(left) right = left_shift(right) - key1 = apply_table(left+right, p8_table) + key1 = apply_table(left + right, p8_table) left = left_shift(left) right = left_shift(right) left = left_shift(left) right = left_shift(right) - key2 = apply_table(left+right, p8_table) + key2 = apply_table(left + right, p8_table) - #encryption + # encryption temp = apply_table(message, IP) temp = function(expansion, s0, s1, key1, temp) - temp = temp[4:]+temp[:4] + temp = temp[4:] + temp[:4] temp = function(expansion, s0, s1, key2, temp) CT = apply_table(temp, IP_inv) - print('Cipher text is: ', CT) + print("Cipher text is:", CT) - #decryption + # decryption temp = apply_table(CT, IP) temp = function(expansion, s0, s1, key2, temp) - temp = temp[4:]+temp[:4] + temp = temp[4:] + temp[:4] temp = function(expansion, s0, s1, key1, temp) PT = apply_table(temp, IP_inv) - print('Plain text after decypting is: ', PT) + print("Plain text after decypting is:", PT)