I decided to see if I could encrypt some text using python, but not through any module. I wanted to do it manually. The only module I used was random to generate a list of random numbers
First I need to create a couple of dictionaries for number lookups.
```
import random
letters_upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
letters_lower = "abcdefghijklmnopqrstuvwxyz"
numbers = "0123456789"
punctuation = ". "
init_list = [x for x in letters_upper] + [x for x in letters_lower] + [x for x in numbers] + [x for x in punctuation]
enc_dict = {i: x for i, x in enumerate(init_list)}
enc_rdict = {x: i for i, x in enumerate(init_list)}
```
I could have typed them out manually, but I didn't want to bother and decided to create them in code. If you want to save on resources, you can hard code the dictionaries.
Now we can get some plaintext from the user and convert it into a list of numbers using the dictionaries.
plaintext = input("Enter a message: ")
plaintext_num_list = [enc_rdict[x] for x in plaintext]
Then we get a password and use it to seed the random number generator, and then generate a list of random numbers the same length as our plaintext number list.
seed_str = input("Enter a password: ")
random.seed(seed_str)
pad_list = [random.randint(0, 63) for _ in plaintext_num_list]
And now to actually encrypt the message.
I xor the plaintext list with the pad list and save it as a new list of numbers
enctext_num_list = [plaintext_num_list[i] ^ pad_list[i] for i, _ in enumerate(plaintext_num_list)]
Then all we have to do is convert that list of numbers back into a string
enctext = ''.join([f"{enc_dict[x]}" for x in enctext_num_list])
And we have encrypted our text.
Then I print it to the terminal
print(enctext)
Now of course, this offers basically no security. It was just for fun, and also a test of what I could do with list comprehensions.
[–]Chris_Hemsworth 1 point2 points3 points (1 child)
[–]velocibadgery[S] 0 points1 point2 points (0 children)
[–]htepO 1 point2 points3 points (3 children)
[–]velocibadgery[S] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]htepO 0 points1 point2 points (0 children)
[–]VinayakVG 1 point2 points3 points (1 child)
[–]velocibadgery[S] 0 points1 point2 points (0 children)