Guest User

Untitled

a guest
Mar 19th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. def rc4algo(msg, key):
  2. # key scheduling
  3. s = [i for i in range(256)]
  4. k = [ord(key[i]) for i in range(len(key))]
  5. # print (k)
  6. j = 0
  7. for i in range(256):
  8. j = (j + s[i] + k[i % len(key)]) % 256
  9. # print (j)
  10. s[i], s[j] = s[j], s[i]
  11.  
  12. #pseudo-random generation
  13. r = []
  14. i = 0
  15. j = 0
  16. for x in range(len(msg)):
  17. i = (i + 1) % 256
  18. j = (j + s[i]) % 256
  19. s[i], s[j] = s[j], s[i]
  20. r.append(s[(s[i] + s[j]) % 256])
  21.  
  22. #XOR
  23. c = [ord(msg[x]) ^ r[x] for x in range(len(msg))]
  24. ctext = ''
  25. for x in range(len(msg)):
  26. ctext += chr(c[x])
  27.  
  28. return ctext
  29.  
  30.  
  31. def main():
  32. print('Enter the message: ', end='')
  33. msg = input()
  34. print('Enter the key: ', end='')
  35. key = input()
  36.  
  37. ch = 0
  38. while (ch != 3):
  39. print(
  40. '1. To Encrypt\n2. To Decrypt\n3. To Exit\nEnter the choice: ',
  41. end='')
  42. ch = int(input())
  43.  
  44. msg = rc4algo(msg, key)
  45. if (ch == 1):
  46. print('Encrypted message: ', msg)
  47. elif (ch == 2):
  48. print('Decrypted message: ', msg)
  49.  
  50.  
  51. main()
Add Comment
Please, Sign In to add comment