Created
October 27, 2014 14:40
-
-
Save quapka/01b3112687336f660468 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import gmpy | |
# see http://stackoverflow.com/a/4801358/2377489 | |
p = 43 | |
q = 59 | |
d = 937 | |
plaintext = 134879475204 | |
if __name__ == '__main__': | |
n = p * q | |
phi_n = (p - 1)*(q - 1) | |
e = gmpy.invert(d, phi_n) | |
i = 0 | |
# determine index i | |
while 10**i < n: | |
i += 1 | |
# set length of each block | |
block_len = i - 1 | |
# get the number of blocks | |
num_blocks = len(str(plaintext)) / block_len | |
# split plaintext | |
parts = [] | |
for j in xrange(num_blocks): | |
# starting index | |
start = block_len * j | |
# ending index | |
end = block_len * (j + 1) | |
parts.append(int(str(plaintext)[start:end])) | |
# encryption | |
codetext = [] | |
for part in parts: | |
# encrypt each part separately | |
codeword = (part ** e) % n | |
codetext.append(codeword) | |
# merge codewords together | |
codetext = ''.join([str(x) for x in codetext]) | |
# show codetext | |
print codetext |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment