Guest User

no-rsa

a guest
Mar 14th, 2017
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.99 KB | None | 0 0
  1. #Embedded file name: src/no-rsa.py
  2. import os
  3. import sys
  4. import json
  5. import random
  6. import string
  7. from Crypto.PublicKey import RSA
  8. N = None
  9. P = None
  10. Q = None
  11. PHI = None
  12. E = 65537
  13. D = None
  14. FILE_DIR = 'files'
  15.  
  16. def service():
  17.     print "Hi! Welcome to our note storage system. It's based on RSA!"
  18.     print 'What do you want to do?'
  19.     print '1. Write a Note -> Type W'
  20.     print '2. Reading a specific note? -> Type R'
  21.     print '3. Request for a signature of your integer? -> Type S'
  22.     sys.stdout.flush()
  23.     cmd = raw_input()
  24.     if cmd == 'W':
  25.         write_note()
  26.     elif cmd == 'R':
  27.         read_note()
  28.     elif cmd == 'S':
  29.         sign()
  30.  
  31.  
  32. def generate_random_note_id():
  33.     return '1111' + ''.join((random.choice(string.digits) for x in range(36)))
  34.  
  35.  
  36. def sign():
  37.     global E
  38.     global N
  39.     print 'Here is our public key (you need them in order to verify the signature): N E'
  40.     print N, E
  41.     print 'Please type: number!'
  42.     print "We don't sign integers starting with for consecutive ones!"
  43.     sys.stdout.flush()
  44.     number = raw_input()
  45.     if len(number) >= 3 and number[0:3] == '1111':
  46.         print 'We told you! we will not sign integers starting with for consecutive ones!'
  47.         sys.stdout.flush()
  48.     else:
  49.         if number.isdigit():
  50.             s = dec(int(number))
  51.             print 'The signature: '
  52.             print s
  53.             sys.stdout.flush()
  54.             return s
  55.         print 'Not a number!'
  56.         sys.stdout.flush()
  57.  
  58.  
  59. def read_note():
  60.     print 'Please type: note_id token'
  61.     sys.stdout.flush()
  62.     note_id, token = raw_input().split(' ', 2)
  63.     try:
  64.         with open('{}/{}'.format(FILE_DIR, note_id)) as f:
  65.             json_data = json.load(f)
  66.             real_token = json_data['token']
  67.             content = json_data['content']
  68.     except Exception as e:
  69.         print 'wrong note_id!'
  70.         sys.stdout.flush()
  71.         return
  72.  
  73.     if token != real_token:
  74.         print 'Wrong token!'
  75.         sys.stdout.flush()
  76.         return
  77.     print 'Note content: ', content
  78.     sys.stdout.flush()
  79.  
  80.  
  81. def write_note():
  82.     while True:
  83.         note_id = generate_random_note_id()
  84.         if not os.path.isfile(note_id):
  85.             break
  86.  
  87.     print 'Please type: content (in just one line)'
  88.     sys.stdout.flush()
  89.     content = raw_input()
  90.     token = str(dec(int(note_id)))
  91.     with open('{}/{}'.format(FILE_DIR, note_id), 'wx') as f:
  92.         json.dump({'token': token,
  93.          'content': content}, f)
  94.     print 'Your note is safe with us! You can retrieve it later by these information: note_id token'
  95.     print note_id, token
  96.     sys.stdout.flush()
  97.  
  98.  
  99. def enc(x):
  100.     return modpow(x, E, N)
  101.  
  102.  
  103. def dec(c):
  104.     global D
  105.     return modpow(c, D, N)
  106.  
  107.  
  108. def modpow(a, b, m):
  109.     result = 1
  110.     a = a % m
  111.     while b > 0:
  112.         if b % 2 == 0:
  113.             a = a * a % m
  114.             b = b / 2
  115.         else:
  116.             result = result * a % m
  117.             b = b - 1
  118.  
  119.     return result
  120.  
  121. def initialize_rsa_credentials():
  122.     global PHI
  123.     global E
  124.     global D
  125.     global N
  126.     global Q
  127.     global P
  128.     if os.path.isfile('rsa.txt'):
  129.         with open('rsa.txt') as rsa_data:
  130.             rsa_credentials = json.load(rsa_data)
  131.             N = rsa_credentials['N']
  132.             P = rsa_credentials['P']
  133.             Q = rsa_credentials['Q']
  134.             PHI = rsa_credentials['PHI']
  135.             E = rsa_credentials['E']
  136.             D = rsa_credentials['D']
  137.             return
  138.     key = RSA.generate(1024, e=E)
  139.     N = getattr(key.key, 'n')
  140.     P = getattr(key.key, 'p')
  141.     Q = getattr(key.key, 'q')
  142.     D = getattr(key.key, 'd')
  143.     PHI = (P - 1) * (Q - 1)
  144.     with open('rsa.txt', 'w') as file:
  145.         json.dump({'N': N,
  146.          'P': P,
  147.          'Q': Q,
  148.          'E': E,
  149.          'D': D,
  150.          'PHI': PHI}, file)
  151.  
  152. def make_file_dir():
  153.     if not os.path.isdir(FILE_DIR):
  154.         os.makedirs(FILE_DIR)
  155.  
  156. if __name__ == '__main__':
  157.     initialize_rsa_credentials()
  158.     make_file_dir()
  159.     service()
Advertisement
Add Comment
Please, Sign In to add comment