# -*- coding: UTF-8-*-
import random
from random import randrange
from math import sqrt
import tkinter
#font12 = tkFont.Font(family="Helvetica", size=12)
def gcd(a, b):
if a < b:
a, b = b, a
while b != 0:
temp = a % b
a = b
b = temp
return a
def exEuclidean(en, eb):
if (eb > en):
en, eb = eb, en
r1 = en
r2 = eb
t1 = 0
t2 = 1
while(r2 > 0):
q = r1 // r2
r = r1 - q * r2
r1 = r2
r2 = r
t = t1 - q * t2
t1 = t2
t2 = t
if(r1 == 1):
return t1 % en
def fast_Exponentiation(a, b, c):
a_1 = a % c
ans = 1
while(b):
if b % 2 == 1:
ans = (ans * a_1) % c
a_1 = (a_1 * a_1) % c
b = b>>1
return ans
def mrange(start, stop, step):
while start < stop:
yield start
start += step
def isprime(num):
if num == 2:
return True
if (num < 2) or (num % 2 == 0):
return False
return all(num % i for i in mrange(3, int(sqrt(num)) + 1, 2))
def is_prime(n, k=10):
if n == 2:
return True
if not n & 1:
return False
def check(a, s, d, n):
x = pow(a, d, n)
if x == 1:
return True
for i in range(s - 1):
if x == n - 1:
return True
x = pow(x, 2, n)
return x == n - 1
s = 0
d = n - 1
while d % 2 == 0:
d >>= 1
s += 1
for i in range(k):
a = randrange(2, n - 1)
if not check(a, s, d, n):
return False
return True
def next_prime(num):
if (not num & 1) and (num != 2):
num += 1
if is_prime(num):
num += 1
while True:
if is_prime(num):
break
num += 2
return num
def encrypt(plain, e, n):
cipher = fast_Exponentiation(plain, e, n)
return cipher
def decrypt(cipher, d, n):
plain = fast_Exponentiation(cipher, d, n)
return plain
def RSA():
global p,q,e,d,n
inn = random.uniform(2 ** 511, 2 ** 512 - 1)
p = next_prime(int(inn))
inn = random.uniform(2 ** 511, 2 ** 512 - 1)
q = next_prime(int(inn))
n = p * q
print (p)
print (q)
t_n = (p - 1) * (q - 1)
e = 65537
for i in range(65537, 100000):
if(gcd(t_n, i) == 1):
e = i
break
d = exEuclidean(t_n, e)
print('e =', e)
print('d =', d)
plaintext = plaintextInput.get("1.0", "end-1c")
#print (plaintext)
#print (type(plaintext))
#plaintext = plaintext.encode('utf-8')
#print (type(plaintext))
plainnum = ''
ciphertextOutput.delete(1.0, tkinter.END)
i = 0
while i < len(plaintext):
temp_num = 0
num = ord(plaintext[i])
temp = str(num)
#print (temp)
if (len(temp) == 5):
result_temp = '9' + temp
else:
while (len(temp) < 3):
temp += ' '
temp_num += 1
result_temp = ''
if (temp_num > 0):
result_temp = '8'
for r in range(0, temp_num - 1):
result_temp += '0'
result_temp += str(num)
#print (result_temp)
plainnum += result_temp
i += 1
print(plainnum)
while(len(plainnum) % 100 != 0):
plainnum += '0'
print ('plainnum =', plainnum)
i = 1
temp_plain = ''
while i < len(plainnum) + 1:
temp_plain += plainnum[i-1]
if (i % 100 == 0 and i != 0):
int_plainnum = int(temp_plain)
print('plainnum =', temp_plain)
ciphernum = encrypt(int_plainnum, e, n)
print('ciphernun =', ciphernum)
ciphertextOutput.insert(tkinter.END, str(ciphernum))
ciphertextOutput.insert(tkinter.END, ',')
temp_plain = ''
i += 1
#int_plainnum = int(temp_plain)
#ciphernum = encrypt(int_plainnum, e, n)
#ciphertextOutput.delete(1.0, tkinter.END)
#ciphertextOutput.insert(tkinter.END,str(ciphernum))
def deRSA():
global p,q,e,d,n
print('e =', e)
print('d =', d)
ciphernum = ciphertextOutput.get("1.0", "end-1c")
i = 0
cipher_temp = ''
result_cipher = ''
while(i < len(ciphernum)):
if(ciphernum[i] == ','):
cipher_temp = int(cipher_temp)
print('ciphertext1 =', cipher_temp)
plaintext1 = decrypt(cipher_temp, d, n)
print('plaintext1 =', plaintext1)
temp_add = 0
plaintext2 = str(plaintext1)
plaintext3 = ''
while(len(plaintext2) % 100 != 0):
temp_add += 1
plaintext2 += ' '
for x in range(0, temp_add):
plaintext3 += '0'
plaintext3 += plaintext2
result_cipher += str(plaintext3)
cipher_temp = ''
i += 1
continue
cipher_temp += ciphernum[i]
i += 1
#print(cipher_temp)
#cipher_temp = int(cipher_temp)
#plaintext1 = decrypt(cipher_temp, d, n)
#print(plaintext1)
#result_cipher += str(plaintext1)
print (result_cipher)
i = 0
final_plain = ''
while i < len(result_cipher):
temp_result_plain = ''
if(result_cipher[i] == '8'):
i += 1
for x in range(0, 2):
if(i >= len(result_cipher)):
break
temp_result_plain += result_cipher[i]
i += 1
elif(result_cipher[i] == '9'):
i += 1
for x in range(0, 5):
if (i >= len(result_cipher)):
break
temp_result_plain += result_cipher[i]
i += 1
else:
for x in range(0, 3):
if (i >= len(result_cipher)):
break
temp_result_plain += result_cipher[i]
i += 1
print('temp_result_plain =', temp_result_plain)
print(chr(int(temp_result_plain)))
if(int(temp_result_plain) == 0):
break
final_plain += chr(int(temp_result_plain))
#print (result_plain)
#result_plain = result_plain.decode('utf-8')
plaintextOutput.delete(1.0, tkinter.END)
plaintextOutput.insert(1.0, final_plain)
if __name__ == '__main__':
'''''
inn = random.uniform(10**150, 9 * 10**150)
p = next_prime(int(inn))
inn = random.uniform(10**150, 9 * 10**150)
q = next_prime(int(inn))
print p
print q
n = p * q
t_n = (p - 1) * (q - 1)
print gcd(65537, t_n)
e = 65537
d = exEuclidean(t_n, e)
print d
print e * d % t_n
#p = 9613034531358350457419158128061542790930984559499621582258315087964794045505647063849125716018034750312098666606492420191808780667421096063354219926661209
#q = 12060191957231446918276794204450896001555925054637033936061798321731482148483764659215389453209175225273226830107120695604602513887145524969000359660045617
inn = random.uniform(2 ** 511, 2 ** 512 - 1)
p = next_prime(int(inn))
inn = random.uniform(2 ** 511, 2 ** 512 - 1)
q = next_prime(int(inn))
n = p * q
t_n = (p - 1) * (q - 1)
e = 65537
d = exEuclidean(t_n, e)
#plaintext = raw_input("Plaintext:")
plaintext = "abcdds\nskdjhd"
plainnum = ''
for x in plaintext:
temp_num = 0
num = ord(x)
temp = str(num)
if (len(temp) != 3):
temp += ' '
temp_num += 1
result_temp = ''
for r in range(0, temp_num):
result_temp += '0'
result_temp += str(num)
plainnum += result_temp
plainnum = int(plainnum)
ciphernum = encrypt(plainnum, e, n)
print ciphernum
plaintext1 = decrypt(ciphernum, d, n)
print plaintext1
add = (3 - len(str(plaintext1)) % 3) % 3
print add
add_letter = ''
for x in range(0, add):
add_letter += '0'
plaintext1 = add_letter + str(p
评论4