Advertisement
obernardovieira

Encript your keys and save [Complete program]

Jan 16th, 2015
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.21 KB | None | 0 0
  1. #!C:\Python34\python.exe
  2.  
  3. import base64
  4. import os.path
  5.  
  6. pass_file = 'pass_file.txt'
  7. keys_file = 'keys_file.txt'
  8.  
  9. organized_keys = []
  10.  
  11. def option_chosen(option, key_name):
  12.    
  13.     if option == 1:
  14.         key_pass = ''
  15.  
  16.         while(key_pass == ''):
  17.             key_pass = input('Enter the password of the key: ')
  18.  
  19.         organized_keys.append([key_name, key_pass])
  20.         print('Added successfully!')
  21.        
  22.     elif option == 2:
  23.         if key_name == 'all':
  24.             print('Key name\tKey Password')
  25.             for z in range(0, len(organized_keys)):
  26.                 print(organized_keys[z][0], '\t\t',
  27.                       organized_keys[z][1])
  28.         else:
  29.             found = False
  30.             for z in range(0, len(organized_keys)):
  31.                 if organized_keys[z][0] == key_name:
  32.                     print('Key name\tKey Password')
  33.                     print(organized_keys[z][0], '\t\t',
  34.                           organized_keys[z][1])
  35.                     found = True
  36.                     break
  37.  
  38.             if found is False:
  39.                 print('Key name not found!')
  40.  
  41.     elif option == 3:
  42.         found = False
  43.         for z in range(0, len(organized_keys)):
  44.                 if organized_keys[z][0] == key_name:
  45.                     found = True
  46.                     print(organized_keys[z][0], '\t\t',
  47.                           organized_keys[z][1])
  48.                     repeat = True
  49.                     while repeat != False:
  50.                         toedit = input('Edit name or key?: ')
  51.                         tmpvalue = ''
  52.                         if toedit == 'name':
  53.                             while not len(tmpvalue):
  54.                                 tmpvalue = input('Write the new name: ')
  55.                             repeat = False
  56.                             organized_keys[z][0] = tmpvalue
  57.  
  58.                         elif toedit == 'key':
  59.                             while len(tmpvalue) == 0:
  60.                                 tmpvalue = input('Write the new key: ')
  61.                             repeat = False
  62.                             organized_keys[z][1] = tmpvalue
  63.  
  64.                         else:
  65.                             repeat = True
  66.                            
  67.                     print('Changed successfully!')
  68.                     break
  69.                        
  70.         if found is False:
  71.             print('Key name not found!')
  72.  
  73.     elif option == 4:
  74.         all_found = []
  75.        
  76.         for z in range(0, len(organized_keys)):
  77.             if organized_keys[z][0] == key_name:
  78.                 all_found.append(z)
  79.                
  80.         if len(all_found) == 1:
  81.             organized_keys.pop(all_found[0])
  82.             print('Deleted successfully!')
  83.  
  84.         elif not len(all_found):
  85.             print('Key name not found!')
  86.  
  87.         else:
  88.             print('This key was found more than on time!')
  89.             yes = input('Delete all? (yes/no)')
  90.             if yes == 'yes' or 'y':
  91.                 count = 0
  92.                 for x in range(0, len(all_found)):
  93.                     organized_keys.pop(all_found[x]-count)
  94.                     count += 1
  95.  
  96.                 print('All deleted successfully!')
  97.                    
  98.     elif option == 5:
  99.         finalized = ''
  100.         for z in range(0, len(organized_keys)):
  101.             finalized += organized_keys[z][0]+':'+\
  102.                          organized_keys[z][1]+'\n'
  103.            
  104.         file = open(keys_file,'wb')
  105.         file.write(base64.b85encode(finalized.encode()))
  106.         file.close()
  107.         print('Saved successfully!')
  108.        
  109.     return
  110.  
  111. def login():
  112.  
  113.     if os.path.exists(pass_file) is True:
  114.         file = open(pass_file,'rb')
  115.         tmppass = base64.b85decode(file.read())
  116.         file.close()
  117.         password = ''
  118.         while(password.encode() != tmppass):
  119.             password = input('Write your password: ')
  120.            
  121.     else:
  122.         reset = True
  123.         password = ''
  124.         while reset != False:
  125.             print('You don\'t have a password!\n')
  126.             password = input('Write your password: ')
  127.             cpassword = input('Please, confirm your password: ')
  128.             while(password != cpassword):
  129.                 print('If you want to reset, write \'reset\'')
  130.                 cpassword = input('Wrong password!\nPlease, confirm your password: ')
  131.                 if(cpassword == 'reset'):
  132.                     reset = True
  133.                     break
  134.             if(cpassword != 'reset'):  
  135.                 reset = False
  136.                
  137.         file = open(pass_file,'wb')
  138.         file.write(base64.b85encode(password.encode()))
  139.         file.close()
  140.  
  141.     return
  142.  
  143. def loadkeys():
  144.  
  145.     if os.path.exists(keys_file) is False:
  146.         file = open(keys_file,'w')
  147.         file.close()
  148.    
  149.     file = open(keys_file,'rb')
  150.     tmpkeys = file.read()
  151.     file.close()
  152.  
  153.     encoded = base64.b85decode(tmpkeys)
  154.     tmpkeys = encoded.decode().splitlines()
  155.  
  156.     for x in range(0,len(tmpkeys)):
  157.         organized_keys.append(tmpkeys[x].split(':'))
  158.  
  159.     return
  160.  
  161. def programe():
  162.     login()
  163.     print('Login done!')
  164.     loadkeys()
  165.     print('Load finished!')
  166.     while True:
  167.         print('\n\nChoose an option:\n' +\
  168.               '1 - add a key\n' +\
  169.               '2 - see a key\n' +\
  170.               '3 - edit a key\n' +\
  171.               '4 - delete a key\n' +\
  172.               '5 - save file\n' +\
  173.               '6 - finish program\n\n')
  174.         option = int(input('Your option: '))
  175.  
  176.         if option == 6:
  177.             break
  178.        
  179.         elif option == 1:
  180.             key_name = input('Enter the name of the key: ')
  181.             option_chosen(option, key_name)
  182.  
  183.         elif option == 5:
  184.             option_chosen(option, '')
  185.            
  186.         elif option > 1 and option < 5:
  187.             if not len(organized_keys):
  188.                 print('You don\'t have keys')
  189.                
  190.             else:
  191.                 key_name = '';
  192.                 input_msg = 'Key name' +\
  193.                     ((' ',' (use \'all\' fro all)')[option == 2]) +': '
  194.  
  195.                 key_name = input(input_msg)
  196.  
  197.                 option_chosen(option, key_name)
  198.  
  199.         else:
  200.             print('Invalid option!')
  201.            
  202.     return
  203.  
  204. programe()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement