r/learnpython • • Oct 09 '26

any cybersecurity guy who can decrypt this:

1 13.14.1 4.20.14.19.7.9.1.14 14.15 8.5.20 5.5.22.18.5.9.1.18.7.4., 7.14.21 9.14 8.9.19 4.8.1.14 16.20.14.9.15.14.7.9 20.15 8.9.19 4.....5.8..1...

above is the encrypted text i encrypted by a python program i wrote, the text is just a query i want from technerds to get the idea of how safe(encrypted) is my text would be.

0 Upvotes

29 comments sorted by

View all comments

1

u/Nothingbutalsosome Oct 10 '26

okay here's the code:

import json
import random



class Encryption:
    FILE_NAME = "encrypt.json"


    alphabetdict = {chr(i + 96): str(i) for i in range(1, 27)}


    reverse_dict = {value: key for key, value in alphabetdict.items()}


    def load_records(self):
        try:
            with open(self.FILE_NAME, "r", encoding="utf-8") as file:
                return json.load(file)
        except (FileNotFoundError, json.JSONDecodeError):
            return []


    def save_records(self, records):
        with open(self.FILE_NAME, "w", encoding="utf-8") as file:
            json.dump(records, file, indent=4)


    def encode_text(self, text):
        encoded_words = []


        for word in text.split(" "):
            encoded_letters = []


            for char in word:
                if char.lower() in self.alphabetdict:
                    number = self.alphabetdict[char.lower()]


                    if char.isupper():
                        number = "C" + number


                    encoded_letters.append(number)
                else:
                    encoded_letters.append(char)


            encoded_words.append(".".join(encoded_letters))


        return " ".join(encoded_words)


    def shuffle_text(self, encoded_text):
        shuffled_words = []


        for word in encoded_text.split(" "):
            tokens = word.split(".")
            random.shuffle(tokens)
            shuffled_words.append(".".join(tokens))


        return " ".join(shuffled_words)


    def encrypter(self):
        text = input("Enter the text to encrypt: ")


        original_encoded = self.encode_text(text)
        records = self.load_records()


        existing_ciphertexts = {
            record["shuffled_text"] for record in records
        }


        # Find a shuffled version not already in the file.
        shuffled_text = None


        for attempt in range(100):
            candidate = self.shuffle_text(original_encoded)


            if candidate not in existing_ciphertexts:
                shuffled_text = candidate
                break


        if shuffled_text is None:
            print("Could not create a unique ciphertext. Try again.")
            return


        encryption_id = random.randint(1000, 9999)


        record = {
            "id": encryption_id,
            "original_encoded": original_encoded,
            "shuffled_text": shuffled_text
        }


        records.append(record)
        self.save_records(records)


        # Give the user only the ciphertext.
        print("\nEncrypted text:")
        print(shuffled_text)



class Decryption(Encryption):


    def decrypt_text(self):
        encrypted_text = input("Enter the encrypted text: ")
        records = self.load_records()


        # Find the saved record using only the ciphertext.
        matching_record = next(
            (
                record for record in records
                if record["shuffled_text"] == encrypted_text
            ),
            None
        )


        if matching_record is None:
            print("Encrypted text not found.")
            return


        original_encoded = matching_record["original_encoded"]
        decrypted_words = []


        for word in original_encoded.split(" "):
            decoded_letters = []


            for token in word.split("."):
                if token.startswith("C"):
                    decoded_letters.append(
                        self.reverse_dict.get(token[1:], token)
                        .upper()
                    )
                else:
                    decoded_letters.append(
                        self.reverse_dict.get(token, token)
                    )


            decrypted_words.append("".join(decoded_letters))


        print("\nDecrypted text:")
        print(" ".join(decrypted_words))



def main():
    encryption = Encryption()
    decryption = Decryption()


    while True:
        print("\n1. Encrypt")
        print("2. Decrypt")
        print("3. Exit")


        choice = input("Choose an option: ")


        if choice == "1":
            encryption.encrypter()


        elif choice == "2":
            decryption.decrypt_text()


        elif choice == "3":
            break


        else:
            print("Invalid choice.")



if __name__ == "__main__":
    main()

2

u/Expensive-Bear-1376 Oct 10 '26 edited Oct 10 '26

Okay, that proves that "river edge" was not in the input. What was the actual input?

Oh and some shorter code for the encryption part:

import re, random

s = "A man standing on the river edge, gun in his hand pointing to his head."

print(re.sub(
    '[a-z]+',
    lambda m: '.'.join(
        str(ord(c) - 96)
        for c in random.sample(m[0], len(m[0]))
    ),
    s.lower()
))

Sample output:

1 13.1.14 1.20.4.14.14.7.9.19 14.15 5.8.20 18.18.22.9.5 5.5.7.4, 7.21.14 14.9 19.8.9 4.1.14.8 20.9.14.7.16.15.9.14 15.20 8.9.19 1.4.8.5.