1

I made a simple script in python that takes a string and encrypts it by moving every letter by random number between 1 and 10:

from random import randint

n = input('>>')

for i in n:
    print(chr(ord(i) + randint(1,10)), end = '')

so for example i write "seven" and it gives me "yh~gx". What I want to know is it possible to make this happen as you are writing the string. For example if I wanted to write "seven" it would display "yh~gx". I know this is possible in C++ but I'm not sure how to do it in python.

1615903
  • 32,635
  • 12
  • 70
  • 99
WholesomeGhost
  • 1,101
  • 2
  • 17
  • 31
  • Why do you want to do this? And this isn't encrypting, it's just... scrambling I guess. I don't even know what to call it. If this is for fun, great. If not, **abandon ship**. – Luke Joshua Park Jul 17 '17 at 09:50
  • @LukePark yeah it is not for any practical purpose just fun. – WholesomeGhost Jul 17 '17 at 10:55
  • This has some resemblance to a Vigenere cipher, though that shifts by MOD 26, not by 10. You are using `randint` to generate the keystream rather than a fixed repeating key. Looking at Python implementations of the Vigenere cipher might help with some ideas. Specifically you need to look at how you will decrypt your encrypted stream. – rossum Jul 17 '17 at 10:55

1 Answers1

1

Python is not well-suited for this kind of work. Best solution I could find works only on Windows, and there is no way to exit the program but to kill the terminal window (or you can add a check for some character, if you want).

import msvcrt
from random import randint

while True:
    msvcrt.putch(chr((ord(msvcrt.getch()) + randint(1, 10)) % 128).encode('ascii'))

This solution was found here.

Oleksii Filonenko
  • 1,551
  • 1
  • 17
  • 27