daily pastebin goal
53%
SHARE
TWEET

Basic 'Encode/Decode' system in Python

Joseph_Joshua Jul 12th, 2018 (edited) 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2.  
  3. # Let the user choose to encode or decode
  4. choosen = input('Encode a string? (Y/N)')
  5. encode = True
  6.  
  7. # Check what the user has choosen
  8. if choosen == 'Y':
  9.     encode = True
  10.  
  11. elif choosen == 'N':
  12.     encode = False
  13.  
  14. else:
  15.     print('Please enter a valid answer!')
  16.     exit(-1)
  17.  
  18. # Check if the user has chosen to encode
  19.  
  20. if encode:
  21.  
  22.     # Let the user input a string to hide in uppercase
  23.     normal_str = input('Enter a string to hide: ')
  24.  
  25.     # Generate a random 'key' for the user
  26.     key = ''
  27.     for i in range(0, 12):
  28.         key += str(random.randrange(0, 10))
  29.  
  30.     # Cycle through each character in the string
  31.     secret_str = ''
  32.  
  33.     for char in normal_str:
  34.  
  35.         # Get the character code for that character
  36.         char_code = ord(char)
  37.  
  38.         # Set the seed for the random generator
  39.         random.seed(key)
  40.  
  41.         # Add a random number to the character code
  42.         char_code += random.randrange(1, 10)
  43.  
  44.         # Store the character of that code in a new string
  45.         secret_str += chr(char_code)
  46.  
  47.     # Add some salt to the secret string
  48.     for i in range(0, 5):
  49.  
  50.         # Set the seed for the random generator
  51.         random.seed(key + '12390f')
  52.  
  53.         # Add the salt to the secret string
  54.         secret_str += chr(random.randrange(1, 10))
  55.  
  56.     # Print out the secret string and the key
  57.     print('Secret string:', secret_str)
  58.     print('Key:', key)
  59.  
  60. else:
  61.  
  62.     # Let the user input the secret string and the key
  63.     secret_str = input('Please enter the secret string to decode:')
  64.     key = input('Please enter the key:')
  65.  
  66.     # Cycle through each character in the secret string
  67.     normal_str = ''
  68.  
  69.     secret_str = secret_str[0 : len(secret_str) - 5]
  70.     for char in secret_str:
  71.  
  72.         # Get the character code for that character
  73.         char_code = ord(char)
  74.  
  75.         # Set the seed for the random generator
  76.         random.seed(key)
  77.  
  78.         # Minus a random number to the character code
  79.         char_code -= random.randrange(1, 10)
  80.  
  81.         # Store the character of that code in a new string
  82.         normal_str += chr(char_code)
  83.  
  84.     print('Original string:', normal_str)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top