Skip to content

Commit

Permalink
adds rsa and diffie helman
Browse files Browse the repository at this point in the history
  • Loading branch information
anishLearnsToCode committed Oct 9, 2020
1 parent ad41ac7 commit b9c38e4
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 10 deletions.
22 changes: 21 additions & 1 deletion ciphers/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
import numpy as np


def int_to_bin(number: int, block_size=8) -> str:
binary = bin(number)[2:]
return '0' * (block_size - len(binary)) + binary


def char_2_num(letter: str) -> int:
return ord(letter) - ord('a')
return ord(letter.lower()) - ord('a')


def token_2_num(word: str) -> list:
return [char_2_num(letter) for letter in word]


def to_val(word: str, base: int = 26) -> int:
value = 0
for index, letter in enumerate(word[::-1]):
value += char_2_num(letter) * pow(base, index)
return value


def incidence_of_coincidence(frequency: list) -> float:
f = np.array(frequency)
N = f.sum()
return (f * (f - 1)).sum() / (N * (N - 1))


def num_2_char(number: int) -> str:
Expand Down
Empty file added diffie-hellman/__init__.py
Empty file.
17 changes: 17 additions & 0 deletions diffie-hellman/diffie_hellman.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from mathematics import *


a = 2
p = 11

# eve
X_1 = 6
Y_1 = 9

# bob
Y_2 = 3
X_2 = discrete_log(3, 2, 11)

K = a ** (X_1 * X_2) % 11
print(K)

12 changes: 12 additions & 0 deletions diffie-hellman/discrete-log-brute-force.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from mathematics import *
import pprint
import pandas

a = 2
b = 7
m = 9

# table = pandas.DataFrame(discrete_log_table(7))
# print(table)

print(discrete_log(9, 2, 11))
24 changes: 24 additions & 0 deletions mathematics/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ def totient(number: int) -> int:
return count


def relatively_prime_numbers(n):
numbers = []
for i in range(1, n):
if relatively_prime(i, n):
numbers.append(i)
return numbers


def crt_representation(number: int, crt: tuple) -> list:
result = []
for mod in crt:
Expand Down Expand Up @@ -177,3 +185,19 @@ def is_product_of_2_primes(number: int) -> bool:
if is_prime(i) and is_prime(quotient):
return True
return False


def discrete_log_table(number: int) -> list:
table = []
for i in range(1, number):
row = []
for j in range(1, number):
row.append(pow(i, j, number))
table.append(row)
return table


def discrete_log(n, b, m):
for i in range(m):
if pow(b, i, m) == n:
return i
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
numpy
pandas
Empty file added rsa/__init__.py
Empty file.
25 changes: 25 additions & 0 deletions rsa/rsa_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from mathematics import *
import random


p, q = 5, 11
n = p * q
print('n:', n)
phi_n = totient(n)
print('totient:', phi_n)
phi_n_list = relatively_prime_numbers(phi_n)
print(phi_n_list)
# e = phi_n_list[random.randint(0, len(phi_n_list))]
e = 17
print('e:', e)
d = multiplicative_inverse(e, phi_n)
print('d:', d)

# m = random.randint(0, n)
m = 8
c = pow(m, e, n)
print('plaintext:', m)
print('ciphertext:', c)

p = pow(c, d, n)
print('decrypted:', p)
13 changes: 4 additions & 9 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
from mathematics import *
from ciphers import *
from ciphers.utils import *
import numpy as np
import math
import tensorflow

plaintext = 'meetmelater'

caesar_cipher = CaesarShiftCipher(shift=23)
permutation_cipher = PermutationCipher(key=[3, 5, 2, 4, 1])

ciphertext = caesar_cipher.encrypt(plaintext)

print(ciphertext)
print(permutation_cipher.encrypt(ciphertext))
print(tensorflow.version.VERSION)

0 comments on commit b9c38e4

Please sign in to comment.