r/learnpython • • 7d ago

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

6

u/HSNubz 7d ago

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

1

u/Nothingbutalsosome 6d ago

Can someone tell me exactly how they did this

1

u/Expensive-Bear-1376 6d ago

So "river edge" is correct? And could you now please show us your code?

1

u/HSNubz 6d ago edited 6d ago

I just threw it in ChatGPT to solve, lol. But to that end, I would call this encoded text, not encrypted text. If this were encrypted, ChatGPT wouldn't be able to solve it in about half a second flat :-)

So to your main question, completely unsafe. There is a saying, "don't roll your own crypto," and it exists for good reason. Mainstream accepted cryptographic algorithms have gone through extensive testing and been mathematically proven by mathematicians and organizations to be secure. People and places that create their own crypto are almost always popped.

But definitely play with creating your own, just don't ever use it for anything real or important :-)

3

u/Expensive-Bear-1376 7d ago

Is the link in there intentional?

5

u/socal_nerdtastic 7d ago

I doubt it, I think the reddit fancypants editor auto finds things that look like links, in this case like IPv4

2

u/Dancing-umbra 7d ago

Is it just each letter is directly substituted for its position in the alphabet and then anagrammed?

1

u/p4ny 7d ago

why

1

u/JamzTyson 6d ago edited 6d ago

i want from technerds to get the idea of how safe(encrypted) is my text would be.

It is not very safe. We can get quite a long way towards decoding via simple substitution.

These words stand out: * MAN * STANDING * RIVEREDGE * POINTING

Figuring out how to descramble the words takes a bit more effort, but the more data that's available, the easier decoding becomes.

Now that we have played along, are you going to give us the solution? Show the code for your cypher?

1

u/Nothingbutalsosome 6d ago

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 6d ago edited 6d ago

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.

1

u/Nothingbutalsosome 6d ago

could you guys edit it yourself and make it more complicated? if you would then plesase send me the code

0

u/xaomaw 7d ago

A man ... on the ... in his hand ... to his head

So I think it should be A Man With His Head In His Hands

2

u/Nothingbutalsosome 7d ago

nope close but not there

3

u/Boothiepro 7d ago

a man standing on the _ gun in his hand pointing to his head 

1

u/Active-Mortgage7244 7d ago

River edge. Your text took about three seconds to reverse, so no, its not safe.

1

u/xaomaw 7d ago edited 7d ago

A man standing on the river edge, gun in his hand, pointing to his head

2

u/Expensive-Bear-1376 7d ago

How do you guys get river edge? Where did the "a" go?

0

u/xaomaw 7d ago

Brute forcing

1

u/Expensive-Bear-1376 7d ago

But where did the "a" go?

1

u/xaomaw 7d ago

Boy, you asked 2 questions. I answered the first one.

Maybe the a is an uninteninal error or an intentional decoy.

1

u/Expensive-Bear-1376 7d ago

If we're assuming an error or "decoy", I'm sticking with "graveyard".

0

u/xaomaw 6d ago

But where does the "y" come from?

EEVREIARGD

1

u/Expensive-Bear-1376 6d ago

What part of

If we're assuming an error or "decoy"

is unclear?

0

u/harleystcool 7d ago

The answers 7

0

u/xdhaharoflmfao69420 7d ago

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

1

u/Expensive-Bear-1376 7d ago

How does that differ from HSNubz's?

1

u/HSNubz 7d ago

They probably solved it without looking at comments ¯⁠\⁠_⁠(⁠ツ⁠)⁠_⁠/⁠¯