coding utf-8 from random import import qwerty from math import import

  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
# coding=utf-8
from random import *
import qwerty
from math import *
import time
def gcd(a, b):
"""returns the Greatest Common Divisor of a and b"""
a = abs(a)
b = abs(b)
if a < b:
a, b = b, a
while b != 0:
a, b = b, a % b
return a
def int_base_two(x):
"""x is a positive integer. Convert it to base two as a list of integers
in reverse order as a list."""
# repeating x >>= 1 and x & 1 will do the trick
bit_inverse = []
while x != 0:
bit_inverse.append(x & 1)
x >>= 1
return bit_inverse
def mod_exp(a, d, n):
"""returns a ** d (mod n)"""
base_two_d = int_base_two(d)
base_two_d__len = len(base_two_d)
mod_array = []
result = 1
for i in range(1, base_two_d__len + 1):
if i == 1:
mod_array.append(a % n)
else:
mod_array.append((mod_array[i - 2] ** 2) % n)
for i in range(0, base_two_d__len):
if base_two_d[i] == 1:
result *= base_two_d[i] * mod_array[i]
return result % n
def to_bin(x):
return bin(x)[2:]
def to_bin_8(x):
return bin(x)[2:].zfill(8)
def data_to_bin_str(data_file):
bin_str = ''
for i in data_file:
bin_str += (to_bin_8(ord(i)))
return bin_str
def encrypt(file, e_file, open_key, N):
f = open(file, 'rb')
f.seek(0)
data_file = f.read()
fi = open(e_file, 'wb')
#print data_file
for i in range(0, len(data_file), 64):
part_of_data_file = data_file[i:i+64]
bin_str = data_to_bin_str(part_of_data_file)
num_str = int(bin_str, 2)
#print num_str
secret_num = str(mod_exp(num_str, int(open_key), int(N)))
fi.write(str(secret_num + '\n'))
fi.close()
f.close()
def decrypt(d_file_en, d_file_dec, secret_key, N):
f = open(d_file_en, 'rb')
fi = open(d_file_dec, 'wb')
for line in f:
l = line.strip()
secret_mes = mod_exp(int(l), secret_key, N)
tmp = secret_mes
string = ''
while tmp:
string += chr(tmp & 0b11111111)
tmp >>= 8
#print '\n', string[::-1]
fi.write(string[::-1])
f.close()
fi.close()
def multiple_inv(e, F):
u = (e, 1)
v = (F, 0)
while v[0] != 0:
q = u[0] // v[0]
t = (u[0] % v[0], u[1] - q * v[1])
u = v
v = t
if u[0] != 1:
return 0
return u[1] % F
time.clock()
p = qwerty.findAPrime(10**154, 2**512-1, 50)
q = qwerty.findAPrime(10**154, 2**512-1, 50)
#print p, q
print time.clock()
#p = 3484012459357535389257764733341658573348123528084503236095329120219032291822016457428603267715402056283756173357918283509919929164000482319465122237816683
#q = 650596054740854964979315968687627705877635108044267532891060878406152879074939666218121009645274892558211324045903927901263695872711186043763777008229087
F = (p-1)*(q-1)
N = p*q
secret_key = multiple_inv(123426017006182806728593424683999798008235734137469123231828679 , F)
encrypt("data_file.txt", "data_file_encrypt", 123426017006182806728593424683999798008235734137469123231828679 , N)
decrypt("data_file_encrypt", "data_file_decrypt.txt", secret_key, N)