Home > Blockchain >  Python txt Encrypter and Decrypter- 'Decrypter' Not Working
Python txt Encrypter and Decrypter- 'Decrypter' Not Working

Time:08-25

import random

a = random.randint(1, 99999999)
b = random.randint(1, 99999999)
c = random.randint(1, 99999999)
d = random.randint(1, 99999999)
e = random.randint(1, 99999999)
f = random.randint(1, 99999999)
g = random.randint(1, 99999999)
h = random.randint(1, 99999999)
i = random.randint(1, 99999999)
j = random.randint(1, 99999999)
k = random.randint(1, 99999999)
l = random.randint(1, 99999999)
m = random.randint(1, 99999999)
n = random.randint(1, 99999999)
o = random.randint(1, 99999999)
p = random.randint(1, 99999999)
q = random.randint(1, 99999999)
r = random.randint(1, 99999999)
s = random.randint(1, 99999999)
t = random.randint(1, 99999999)
u = random.randint(1, 99999999)
v = random.randint(1, 99999999)
w = random.randint(1, 99999999)
x = random.randint(1, 99999999)
y = random.randint(1, 99999999)
z = random.randint(1, 99999999)
one = random.randint(1, 99999999)
two = random.randint(1, 99999999)
three = random.randint(1, 99999999)
four = random.randint(1, 99999999)
five = random.randint(1, 99999999)
six = random.randint(1, 99999999)
seven = random.randint(1, 99999999)
eight = random.randint(1, 99999999)
nine = random.randint(1, 99999999)
zero = random.randint(1, 99999999)

print("Copy And Paste All The Data You Want To Encrypt In The 'Data.txt' File In The Data Encrypter Folder")
print("Type 'Done' When Your Done:")
ans = input()
if ans.lower() == "done":
    def encrypt():
        Data = open("Data.txt", "r")
        NewData = Data.read()
        Data.close()
        NewData = NewData.replace("a", str(a))
        NewData = NewData.replace("b", str(b))
        NewData = NewData.replace("c", str(c))
        NewData = NewData.replace("d", str(d))
        NewData = NewData.replace("e", str(e))
        NewData = NewData.replace("f", str(f))
        NewData = NewData.replace("g", str(g))
        NewData = NewData.replace("h", str(h))
        NewData = NewData.replace("i", str(i))
        NewData = NewData.replace("j", str(j))
        NewData = NewData.replace("k", str(k))
        NewData = NewData.replace("l", str(l))
        NewData = NewData.replace("m", str(m))
        NewData = NewData.replace("n", str(n))
        NewData = NewData.replace("o", str(o))
        NewData = NewData.replace("p", str(p))
        NewData = NewData.replace("q", str(q))
        NewData = NewData.replace("r", str(r))
        NewData = NewData.replace("s", str(s))
        NewData = NewData.replace("t", str(t))
        NewData = NewData.replace("u", str(u))
        NewData = NewData.replace("v", str(v))
        NewData = NewData.replace("w", str(w))
        NewData = NewData.replace("x", str(x))
        NewData = NewData.replace("y", str(y))
        NewData = NewData.replace("z", str(o))
        NewData = NewData.replace("1", str(one))
        NewData = NewData.replace("2", str(two))
        NewData = NewData.replace("3", str(three))
        NewData = NewData.replace("4", str(four))
        NewData = NewData.replace("5", str(five))
        NewData = NewData.replace("6", str(six))
        NewData = NewData.replace("7", str(seven))
        NewData = NewData.replace("8", str(eight))
        NewData = NewData.replace("9", str(nine))
        NewData = NewData.replace("0", str(zero))
        open("Data.txt", "w")
        my_file = open("Data.txt", "w")
        my_file.write(NewData)
        my_file.close()
    encrypt()
print("Encrypted!")

print("Would You Like To Decrypt If Yes Type 'Yes' If No Type 'No':") if input().lower == "yes": def decrypt(): Data = open("Data.txt", "r") NewData = Data.read() Data.close() NewData = NewData.replace(str(a), "a") NewData = NewData.replace(str(b), "b") NewData = NewData.replace(str(c), "c") NewData = NewData.replace(str(d), "d") NewData = NewData.replace(str(e), "e") NewData = NewData.replace(str(f), "f") NewData = NewData.replace(str(g), "g") NewData = NewData.replace(str(h), "h") NewData = NewData.replace(str(i), "i") NewData = NewData.replace(str(j), "j") NewData = NewData.replace(str(k), "k") NewData = NewData.replace(str(l), "l") NewData = NewData.replace(str(m), "m") NewData = NewData.replace(str(n), "n") NewData = NewData.replace(str(o), "o") NewData = NewData.replace(str(p), "p") NewData = NewData.replace(str(q), "q") NewData = NewData.replace(str(r), "r") NewData = NewData.replace(str(s), "s") NewData = NewData.replace(str(t), "t") NewData = NewData.replace(str(u), "u") NewData = NewData.replace(str(v), "v") NewData = NewData.replace(str(w), "w") NewData = NewData.replace(str(x), "x") NewData = NewData.replace(str(y), "y") NewData = NewData.replace(str(z), "z") NewData = NewData.replace(str(one), "1") NewData = NewData.replace(str(two), "2") NewData = NewData.replace(str(three), "3") NewData = NewData.replace(str(four), "4") NewData = NewData.replace(str(five), "5") NewData = NewData.replace(str(six), "6") NewData = NewData.replace(str(seven), "7") NewData = NewData.replace(str(eight), "8") NewData = NewData.replace(str(nine), "9") NewData = NewData.replace(str(zero), "0") open("Data.txt", "w") my_file = open("Data.txt", "w") my_file.write(NewData) my_file.close() decrypt() print("Unencrypted")

CodePudding user response:

  1. Maybe the reason is because you didn't save the value of the a,b,c,... so when you tried to decrypt it would return different value
  2. Because it is random from 1 to 99999999 so they might contain different string. For example:
  • The file value is ab and a is 11234 then b is 112345 After encrypt the file value is 11234112345 so when you do NewData = NewData.replace(str(a), "a") it will return aa5

CodePudding user response:

Second block of code is not properly edited, take it into account for writing further questions

In your case problem is that before decryption you tried lowercase input, but forgot brackets input().lower, so it creates something like this: VAR - <built-in method lower of str object at 0x0000027B314500F0>, TYPE - <class 'builtin_function_or_method'>. And can't be compared with "yes" (always throws False)

This was the problem in code, but there is major problem in program's logic:

Suppose word_to_encypt = "aaa", at first your filter replaces all "a"'s with some random number, but after that all numbers are also replaced with some another random numbers. So it creates total mess with total amount of not 3, bot from 3 to ∞ encryptions.

Maybe you can try decoding in reverse order, so:

a = 900, 9 = 1, 0 = 2

# encoding
"aa" --> 900900 --> 122122

# decoding (from last to first)
# 2 = 0, 1 = 9, 900 = a
122122 --> 100100 --> 900900 --> "aa"

[!] BUT there may be some other conflicts, that I can't imagine, and it is better to use slightly another way of encoding.

If your task was to implement exactly such an algorithm, then it is better to make all the all the encryption keys the same length (as it is in BASE64). If your want so, you will need to split data letter by letter and encode them separately. If you want - write a comment on my answer and I'll provide code

I have an assumption that you are new to Python, and I recommend using (or implement by your own hands for better understanding) some popular encoding methods:

P.P.S. From my own experience I recommend using something like os.path.join(os.path.dirname(__file__), "Data.txt") while defining filepath in open, otherwise script will crush if you run it from another folder (More).

Btw for what first open is used here:

open("Data.txt", "w")
my_file = open("Data.txt", "w")

I my opinion first line is unnecessary, and better to use with open construction, so as not to worry about closing everythin

  • Related