Python Forum
A deciphering problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A deciphering problem
#5
I made my own version of a cipher/decipher and solved ibreeden's
from dataclasses import dataclass
import random
from typing import List

ibreeden_list = [
    "iex?LMC9HeHI0F?G9H:",
    "!LMWoQSVRMRK,oQcoKEVHIRIVoTEPIoJVSQoJVMKLX,",
    "‘Thz1ly,DvulDtvtlu1,Dwslhzl’,DjhtlDy2uupunDpuzpklG",
    "‘oFcQGF0 JcJGK -8MK.cocO7Kc9MLLAF?cK.GGLc7!L JcK.GGL",
    "tSIpbMJSp2pYZWSJIpFSIpQTTPJI,pLWNRpwJFYMpYMJWJpXYTTIs",
    "TH8l4Hl11lwwpoHlyoHm0H5spHz5sp3H8l0HTHqwpo,",
    "Cvu!tujmm!eftdsjfe!ijt!iboe!dbtujoh!b!uisfbuA",
    "u9MN?L,eSIOLeBILM?,e9H!eQCNBeAI!MJ??!eF?NeG?eLC!?",
    "!So1WTELER,oaLMGLo1oQEcoVIEGLoIVIoJEPPoSJoRMKLXr’",
    "aopzDhm1lyuvvuD–DsvunDhm1lyDolDohkDzwlkD–",
    "oFcL. cH7JCcG!c9 07JK,cj 7L.cALcO7KcocE Lf",
    "‘BMd,’pYMZXp2pFXPJI,pbMNQJpMJpXYTTIpbFNYNSLpYMJWJ,",
    "‘OtoH0z6,H5st4Hxz3y,Hrt7pHx0H4p37ly5H46nsHlH4nl3pJ’",
    "Tnjmjohmz!dbnf!ijt!sfqmz:!‘Op!uisfbu,!gps!tvsf,!ju!xbt",
    "2B9NeM?HNeSIOLeA9L!?H?Le.F??CHAhe1OLJLCM?!eqeQ9M",
    "!SoJMRH,oMRoIEVPcoQSVR,oLIVIoWXMPPoEXoaSVOoEoQER",
    "dov,D1opzDzhtlDl3lupun,DPDhtD1vD1hrlDpuDPzwhohuG’",
]

SYMBOLS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 !?."


@dataclass
class CipherData:
    shift: str
    result: str


@dataclass
class CipherLine:
    line: str
    cipher_data_possibilites: List[CipherData]


@dataclass
class CeaserCipher:
    symbols: str

    def _shift_symbols_right(self, shift: int) -> str:
        return self.symbols[shift:] + self.symbols[:shift]

    def _shift_symbols_left(self, shift: int) -> str:
        shift = len(self.symbols) - shift
        return self.symbols[shift:] + self.symbols[:shift]

    def cipher(self, line: str, shift: int):
        shifted_sybols = self._shift_symbols_right(shift)
        result = line.translate(line.maketrans(self.symbols, shifted_sybols))
        return CipherData(shift, result)

    def decipher(self, line: str, shift: int) -> str:
        shifted_sybols = self._shift_symbols_left(shift)
        result = line.translate(line.maketrans(self.symbols, shifted_sybols))
        return CipherData(shift, result)

    def decipher_all_possibilties(self, line: str) -> list[str]:
        return [self.decipher(line, index) for index, _ in enumerate(self.symbols)]

    def cipher_random_shift(self, line: str) -> str:
        return self.cipher(line, random.randint(0, len(self.symbols)))


cipherer = CeaserCipher(SYMBOLS)
cipher_lines = [
    CipherLine(line, cipherer.decipher_all_possibilties(line)) for line in ibreeden_list
]

solved_index = (34, 44, 7, 32, 45, 11, 1, 34, 44, 7, 32, 45, 11, 1, 34, 44, 7)


for index, cipher_line in zip(solved_index, cipher_lines):
    print(cipher_line.cipher_data_possibilites[index])
Output:
CipherData(shift=34, result='A Persian nobleman:') CipherData(shift=44, result='This morning, my gardener pale from fright,') CipherData(shift=7, result='‘Master, one moment, please’, came running inside.') CipherData(shift=32, result='‘In yonder rose-bush I was cutting shoot after shoot') CipherData(shift=45, result='And when I turned and looked, grim Death there stood.') CipherData(shift=11, result='I was appalled and by the other way I fled,') CipherData(shift=1, result='But still descried his hand casting a threat.') CipherData(shift=34, result='Master, your horse, and with godspeed let me ride') CipherData(shift=44, result='To Ispahan, which I may reach ere fall of night.’') CipherData(shift=7, result='This afternoon – long after he had sped –') CipherData(shift=32, result='In the park of cedars, Death it was I met.') CipherData(shift=45, result='‘Why,’ thus I asked, while he stood waiting there,') CipherData(shift=11, result='‘Did you, this morn, give my servant such a scare?’') CipherData(shift=1, result='Smilingly came his reply: ‘No threat, for sure, it was') CipherData(shift=34, result='That sent your gardener fleeing. Surprised I was') CipherData(shift=44, result='To find, in early morn, here still at work a man') CipherData(shift=7, result='Who, this same evening, I am to take in Ispahan.’')

I then used
for line in mystery_list:
    print(cipherer.cipher_random_shift(line).result)
to create a new one
yoriz_list = [
    "Svr! zw!3Nz0Nsv  v9N yr5N!x3BQ",
    "XC952v2?P2!Pux??x P?1t7P26952v2?S",
    "HXbeaTzXhzQTiiTgziWPczRdbeaTm3",
    "tRPSOHanLVnEHWWHUnWKDQnFRPSOLFDWHGq",
    "AgVo6dn6WZooZm6ocVi6iZnoZY9",
    "xG6IJ0b.Jb70KK0IbK?6Eb90EJ0e",
    "LYUXUVcfcns5Wiohnm8",
    "uD75 3.Y53G7GY3F7B'HYGD75 3.Y7BCI90YHCY4F73?YH07YFI.7Gb",
    "mJRFMSEFiNP?ARGA?JGRWi.C?RQiNSPGRWl",
    "cBB?BCUC6?E02U!3F3BU.yCCUC703!D0IX",
    "o86y??QyD063w3.6EQ?36y8wyxT",
    "qHeNB?e.9 ?eI.e9G0CAOCNS,eL?.OM?eNB?eN?GJN9NCIHeNIeAO?MMh",
    "DRObOucRYeVNuLOuYXO--uKXNuZbOPObKLViuYXViuYXOu--YLfSYecugKiudYuNYuSdx",
    "3ZhVciUVyhVOhykOmyaOmybchyPSycPjWcigyOhyTWfghyibZSggymci'fSy6ihQV2",
    "i0DR4.RwzAAz?RA3v9R9zCz?U",
    "Z E7.F67V?4G4CV8DV.5E4?V14EE4CVE7z?V*C867E*V?.HY",
    "d1RA3zR48 7z8z9AvA409R4.R3v?yRA0RzE 7v49,R4A'.RvRwvyR4yzvU",
    "7PudROuSWZVOWOXdKdSYXuScuOKciudYuOhZVKSX,uSduWKiuLOuKuQYYNuSNOKx",
    "Zmyq52moq5Im4qI1zqIt1zwuzsIs4qm6IupqmI--Ixq6'5Ip1Iy14qI1rI6t15qJ",
]
Edit: added from typing import List so it works with pre 3.9 python versions
ibreeden likes this post
Reply


Messages In This Thread
A deciphering problem - by Truman - Aug-14-2021, 08:35 PM
RE: A deciphering problem - by ibreeden - Aug-15-2021, 07:31 AM
RE: A deciphering problem - by DPaul - Aug-16-2021, 07:19 AM
RE: A deciphering problem - by ibreeden - Aug-16-2021, 05:43 PM
RE: A deciphering problem - by Yoriz - Aug-16-2021, 07:25 PM
RE: A deciphering problem - by Truman - Aug-16-2021, 07:53 PM
RE: A deciphering problem - by Yoriz - Aug-16-2021, 08:02 PM
RE: A deciphering problem - by DPaul - Aug-17-2021, 06:19 AM
RE: A deciphering problem - by jamesaarr - Aug-18-2021, 08:12 AM
RE: A deciphering problem - by ibreeden - Aug-17-2021, 09:08 AM
RE: A deciphering problem - by DPaul - Aug-17-2021, 09:47 AM
RE: A deciphering problem - by ibreeden - Aug-17-2021, 10:22 AM
RE: A deciphering problem - by ibreeden - Aug-18-2021, 02:11 PM
RE: A deciphering problem - by Yoriz - Aug-18-2021, 05:12 PM
RE: A deciphering problem - by DPaul - Aug-19-2021, 02:47 PM
RE: A deciphering problem - by Truman - Aug-21-2021, 03:28 PM
RE: A deciphering problem - by DPaul - Aug-22-2021, 09:16 AM
RE: A deciphering problem - by Yoriz - Aug-22-2021, 11:53 AM
RE: A deciphering problem - by ibreeden - Aug-23-2021, 09:27 AM
RE: A deciphering problem - by DPaul - Aug-23-2021, 04:00 PM
RE: A deciphering problem - by Yoriz - Aug-23-2021, 04:18 PM
RE: A deciphering problem - by ibreeden - Aug-23-2021, 05:03 PM
RE: A deciphering problem - by DPaul - Aug-23-2021, 05:24 PM
RE: A deciphering problem - by Yoriz - Aug-23-2021, 06:05 PM
RE: A deciphering problem - by DPaul - Aug-24-2021, 07:07 AM
RE: A deciphering problem - by ibreeden - Aug-24-2021, 02:58 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020