carlhoggren
carlhoggren

Reputation: 13

Python 3: encryption using dictionaries

This is part of my code:

def encrypt_file(file_choice):
    encryption_key = {
"a": 1, "b": 3, "c": 5, "d": 7,"e": 9, "f": 11,"g":        
13, "h": 15, "i": 17, "j": 19,"k": 21, "l": 23, "m": 
25, "n": 27, "o": 29, "p": 31, "q": 33, "r": 35, "s":
 37, "t": 39, "u": 41,"v": 43,"w": 45,"x": 47, "y": 49, "z": 51}

keys = encryption_key.keys()

So, I have loaded a text file (file_choice) in another function. And I want every character in the text file, namely (a, b, c etc.) to be given the respective value from the dictionary.

For example if the text has the word "and". I want to create a for-loop that sets the "a" in "and" to "1", "n" in "and" to "27" and "d" in "and" to "7".

So my main problem is that I cannot effectively set the characters in the text file to the respective value in the dictionary. Or more specifically, I want to translate the text file into numbers.

Anyone has an idea of how to create such a loop?

Upvotes: 0

Views: 5478

Answers (3)

neehari
neehari

Reputation: 2612

Here's the function which takes a file, reads its content, changes each line to lower case, returns the encrypted output:

def encrypt_file(file_choice):
    encryption_key = {"a": 1, "b": 3, "c": 5, "d": 7,
                      "e": 9, "f": 11,"g": 13, "h": 15,
                      "i": 17, "j": 19,"k": 21, "l": 23,
                      "m": 25, "n": 27, "o": 29, "p": 31,
                      "q": 33, "r": 35, "s": 37, "t": 39,
                      "u": 41,"v": 43,"w": 45,"x": 47,
                      "y": 49, "z": 51}

    encrypted_txt = ''

    with open(file_choice) as f:
        for line in f:
            for ch in line.lower():
                if ch in encryption_key:
                    encrypted_txt += str(encryption_key[ch])
                else:
                    encrypted_txt += ch

    return encrypted_txt


print(encrypt_file("test.txt")) # 'and' in the text file

1277

>>>

Upvotes: 1

RoadRunner
RoadRunner

Reputation: 26315

Assuming this is a valid example text you wish to read from, called something like text.txt:

Hello world, it's a nice day today!

And you wish to write the encoded characters to a new file, let's say newtext.txt. You can do something like this:

d = {
"a": 1, "b": 3, "c": 5, "d": 7,"e": 9, "f": 11,"g":        
13, "h": 15, "i": 17, "j": 19,"k": 21, "l": 23, "m": 
25, "n": 27, "o": 29, "p": 31, "q": 33, "r": 35, "s":
37, "t": 39, "u": 41,"v": 43,"w": 45,"x": 47, "y": 49, "z": 51}

# open to be read file
with open("text.txt", 'r') as file_open:

    # create file to write to
    with open("newtext.txt", 'w') as file_write:
        for line in file_open:

            # encode characters
            new_line = "".join(str(d[c.lower()]) if c.lower() in d else c for c in line)

            # write to file
            file_write.write(new_line)

# open and print contents of file you just wrote to
with open("newtext.txt", 'r') as file_print:
    print(file_print.read())

Which outputs:

159232329 452935237, 1739'37 1 271759 7149 39297149!

Note: You probably will have to modify the code to get exactly want you want, but this gives the general idea.

Upvotes: 1

Utku Savaş
Utku Savaş

Reputation: 36

You can read the file char by char and make them a list. After that you can write the value of chars in a new file. Also keep in mind, there is no value of key "space".

Upvotes: 0

Related Questions