Python Forum
Convert C code to Python code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert C code to Python code
#3
Hey thanks for the response man I really appreciate it. I made the changes like you suggested however now I'm getting a different error. ("sigh")

line 83, in attempt_ROT
ord(currentChar) + rot) % 26]
IndexError: list assignment index out of range

Thanks for being patient with me.

here is what my updated python code looks like now:

import string

"""This is a python version of ROT ciphers to crack ghost in the wires chapter puzzles."""

ciphered_text = [
    "yjcv ku vjg pcog qh vjg uauvgo wugf da jco qrgtcvqtu vq ocmg htgg rjqpg ecnnu ?",
    "wbth lal voe htat oy voe wxbirtn vfzbqt wagye C poh aeovsn vojgav ?",
    "Nyrk grjjnfiu uzu Z xzmv kf jvklg re rttflek fe Kyv Rib ?",
    "Flle ujw esc wexp mo xsp kjr hsm hiwwcm, \"Wplpll stq lec qma e wzerg mzkk!\" ?",
    "Bmfy ytbs ini N mnij tzy ns zsynq ymj Ozajsnqj Htzwy qtxy ozwnxinhynts tajw rj ?",
    "Kyoo olxi rzr Niyovo Cohjpcx ojy dn T apopsy ?",
    "Kvoh wg hvs boas ct hvs Doqwtwq Pszz sadzcmss kvc fsor hvs wbhsfboz asac opcih am voqywbu oqhwjwhwsq cjsf hvs voa forwc ?",
    "Iwh xwqv wpvpj fwr Vfvyj qks wf nzc ncgsoo esg psd gwc ntoqujvr ejs rypz nzfs ?",
    "Hsle td esp epcx qzc dzqehlcp mfcypo zy esp nsta esle Yzglepw dpye xp ?",
    "Bprf cup esanqneu xmm gtknv amme U biiwy krxheu Iwqt Taied ?",
    "Lwpi idlc sxs bn upiwtg axkt xc lwtc X bdkts xc lxiw wxb ?",
    "Yhlt xak tzg iytfrfad RanBfld squtpm uhst uquwd ce mswf tz wjrwtsr a wioe lhsv Ecid mwnlkoyee bmt oquwdo't ledn mp acomt ?",
    "Zkdw lv wkh qdph ri wkh SL ilup wkdw zdv zluhwdsshg eb Sdflilf Ehoo ?",
    "Plpki ytw eai rtc aaspx M llogw qj wef ms rh xq ?",
    # CH 15
    "Ituot oaybmzk ymwqe ftq pqhuoq ftmf Xqiue geqp fa buow gb mzk dmpua eusmxe zqmd Qduo ?",
    "Kwth qzrva rbq lcq rxw Svtg vxcz zm vzs lbfieerl nsem rmh dg ac oef'l cwamu ?",
    "Epib qa bpm vium wn bpm ixizbumvb kwuxtmf epmzm Q bziksml lwev Mzqk Pmqvh ?",
    # CH 18
    "Khkp wg wve kyfcqmm yb hvh TBS oeidr trwh Yhb MmCiwus Wko ogvwgxar hr ?",
    "Rcvo dn ivhz ja ocz omvinvxodji oj adiy v kzmnji'n njxdvg nzxpmdot iphwzm pndib oczdm ivhz viy yvoz ja wdmoc ?",
    # CH 20
    "Wspa wdw gae ypte rj gae dilan lbnsp loeui V tndllrhh gae awvnh \"HZO, hzl jaq M uxla nvu\"",
    # "4A 75 6E 67 20 6A 6E 66 20 62 68 65 20 61 76 70 78 61 6E 7A 72 20 74 76 69 72 61 20 67 62 20 47 72 65 65 6C 20 55 6E 65 71 6C 3F ",
    #  The above string is just Hexadecimal for character encoding, it translates to the following
    "Jung jnf bhe avpxanzr tvira gb Greel Uneql ?",

    "Gsig cof dsm fkqeoe vnss jo farj tbb epr Csyvd Nnxub mzlr ut grp lne ?",
    "Fqjc nunlcaxwrl mnerln mrm cqn OKR rwcnwcrxwjuuh kanjt fqnw cqnh bnjalqnm vh jyjacvnwc rw Ljujkjbjb ?",
    # CH 24
    "Xvof jq qis bmns lg hvq thlss ktffb J cifsok EAJ uojbthwsbhlsg ?",
    "Cngz zuct ngy znk grsg sgzkx lux znk xkgr Kxoi Ckoyy ?",
    "Aslx jst rlxi bx ns wgzzcmgw UP jnsh hlrjf nyk TT seq s cojorpdw pssx gxmyeie ao bzy glc ?",
    # CH 27
    # "85 102 121 114 32 103 113 32 114 102 99 32 108 121 107 99 32 109 100 32 114 102 99 32 122 109 105 113 114 109 112 99 32 71 32 100 112 99 111 115 99 108 114 99 98 32 103 108 32 66 99 108 116 99 112 63",
    "Ufyr gq rfc lykc md rfc zmiqrmpc G dpcosclrcb gl Bcltcp ?",
    "Phtm zvvvkci sw mhx Fmtvr VOX Ycmrt Emki vqimgv vowx hzh L cgf Ecbst ysi ?",
    "126 147 172 163 040 166 172 162 040 154 170 040 157 172 162 162 166 156 161 143 040 145 156 161 040 163 147 144 040 115 156 165 144 153 153 040 163 144 161 154 150 155 172 153 040 162 144 161 165 144 161 040 150 155 040 122 172 155 040 111 156 162 144 077",
    "Ouop lqeg gs zkds ulv V deds zq lus DS urqstsn't wwiaps ?",
    "Alex B25 rixasvo hmh M ywi xs xli HQZ qemrjveqi ?",
    "Caem alw Ymek Xptq'd tnwlchvw xz lrv lkkzxv ?",
    # CH 33
    "Ozg ojglw lzw hshwj gf AH Khggxafy lzsl BKR skcww ew stgml ?",
    "Nvbx nte hyv bqgs pj gaabv jmjmwdi whd hyv UVT'g Giuxdoc Gctcwd Hvyqbuvz hycoij ?",
    "2B 2T W 2X 2Z 26 36 2P 36 2V 3C W 3A 32 39 38 2Z W 3D 33 31 38 2V 36 3D W 2R 2Z W 3E 3C 2V 2X 2Z 2Y W 3E 39 W 2R 32 2V 2E W 2V 3A 2V 3C 3E 37 2X 38 3E W 2X 39 37  3A 36 2Z 2S 1R",
    "Lsar JSA cryoi ergiu lq wipz tnrs dq dccfunaqi zf oj wqpctkiel dpzpgp I jstcgo cu dy hgq ?",
    # CH 37
    "V2hhdCBGQkkgYWdlbnQgYXNrZWQgU3VuIE1pY3Jvc3lzdGVtcyB0byBjbGFpbSB0aGV51Gxvc3QgODAgbWlsbGlvbiBkb2xsYXJzPw==",
    # CH 38
    "100-1111-10-0 011-000-1-111 00-0100 1101-10-1110-000-101-11-0-1 \
	0111-110-00-1001-1-101 111-0-11-0101-010-1-101 111-10-0100 11-00-11"
]

alphabet = list(string.ascii_lowercase)
keywords = ['what ', 'is ', 'the ', 'of ', 'by ',
            'to ', 'did ', 'in ', 'when ', 'how ']


def main():
    numTexts = len(ciphered_text)
    found = 0
    for j in range(numTexts):
        print("%s\n" % ciphered_text[j])
        found = 0
        for i in range(26):
            found = found | attempt_ROT(i, j)
        if not found:
            print("%d: No Candidate.\n" % (j+1))


def attempt_ROT(rot, cipheredIndex):
    tmpString = []
    for currentChar in ciphered_text[cipheredIndex]:
        if ord(currentChar) >= 65 and ord(currentChar) <= 90:
            currentChar = ord(currentChar) + 32
        elif ord(currentChar) >= 97 and ord(currentChar) <= 122:
            tmpString[ord(currentChar)] = alphabet[(
                ord(currentChar) + rot) % 26]
        else:
            tmpString[ord(currentChar)
                      ] = ciphered_text[cipheredIndex][ord(currentChar)]

    tmpString[ord(currentChar)] = '\0'

    # Rotation is done. Now search for keywords.

    for key in keywords:
        if tmpString in key:
            print("%d: Candidate (ROT%d): %s\n" %
                  (cipheredIndex+1, rot, tmpString))
            return 1
    return 0


if __name__ == '__main__':
    main()

Also,
Are you familiar with ICHABOD aka ICH the freight train graffiti artist?
That would be cool. I also like to paint trains. Although it's been years since the last time i painted one.
Reply


Messages In This Thread
Convert C code to Python code - by lostMarbles - Sep-11-2018, 11:45 AM
RE: Convert C code to Python code - by ichabod801 - Sep-11-2018, 12:35 PM
RE: Convert C code to Python code - by lostMarbles - Sep-17-2018, 09:27 AM
RE: Convert C code to Python code - by ichabod801 - Sep-17-2018, 06:09 PM
RE: Convert C code to Python code - by lostMarbles - Sep-18-2018, 05:50 AM
RE: Convert C code to Python code - by ichabod801 - Sep-18-2018, 04:43 PM
RE: Convert C code to Python code - by pcsailor - Sep-19-2018, 06:23 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Equivalent Python code from VBA Mishal0488 4 1,064 May-02-2024, 10:17 PM
Last Post: DeaD_EyE
  Problem Converting Tradingview Indicator to Python code kralxs 1 329 Apr-27-2024, 06:10 PM
Last Post: kralxs
  Why can I not see the code correctly in Python IDLE.? Trump 8 827 Apr-04-2024, 07:47 AM
Last Post: jonesphedra
Sad Selenium update broke python code genericusername12414 1 297 Mar-16-2024, 07:33 PM
Last Post: snippsat
  Algorithm for extracting comments from Python source code Pavel1982 6 675 Feb-28-2024, 09:52 PM
Last Post: Pavel1982
  Python best library for Excel reports & review of existing code MasterOfDestr 4 852 Feb-14-2024, 03:39 PM
Last Post: MasterOfDestr
Lightbulb python code debuging yunus 1 386 Feb-11-2024, 03:48 PM
Last Post: deanhystad
  Python code to set column width 1418 11 1,542 Jan-20-2024, 07:20 AM
Last Post: Pedroski55
  Python code for alignment and font size 1418 0 367 Jan-14-2024, 03:56 AM
Last Post: 1418
  My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor jst 4 1,177 Nov-15-2023, 06:56 PM
Last Post: jst

Forum Jump:

User Panel Messages

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