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
#1
Hi I'm new to this forum.
I'm trying to convert an existing c program to python.
If someone can help me that'd be awesome!!!!
As of right now I'm getting the error:
Traceback (most recent call last):
File "mitnick.py", line 106, in <module>
main()
File "mitnick.py", line 76, in main
found = found | attempt_ROT(i, j)
File "mitnick.py", line 84, in attempt_ROT
currentChar = ciphered_text[cipheredIndex][i]
TypeError: string indices must be integers

I thought it would be a good challenge to convert this to python but I'm having all types of problems. I'm fairly new to programming and have only been coding since the beginning on this year 2018. I started in February. Anyways,
Here is a link to the C program which decrypt's some ciphers from the book Ghost in the wires by Kevin Mitnick.
C code

Here is my Python code so far. As you can probably tell. I've probably made several mistakes.

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 i in ciphered_text[cipheredIndex]:
        currentChar = ciphered_text[cipheredIndex][i]

        if currentChar >= 65 and currentChar <= 90:
            currentChar = currentChar + 32
        elif currentChar >= 97 and currentChar <= 122:
            tmpString[i] = alphabet[(currentChar + rot) % 26]
        else:
            tmpString[i] = ciphered_text[cipheredIndex][i]

    tmpString[i] = '\0'

    # Rotation is done. Now search for keywords.

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


if __name__ == '__main__':
    main()
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 896 Yesterday, 10:17 PM
Last Post: DeaD_EyE
  Problem Converting Tradingview Indicator to Python code kralxs 1 245 Apr-27-2024, 06:10 PM
Last Post: kralxs
  Why can I not see the code correctly in Python IDLE.? Trump 8 741 Apr-04-2024, 07:47 AM
Last Post: jonesphedra
Sad Selenium update broke python code genericusername12414 1 260 Mar-16-2024, 07:33 PM
Last Post: snippsat
  Algorithm for extracting comments from Python source code Pavel1982 6 594 Feb-28-2024, 09:52 PM
Last Post: Pavel1982
  Python best library for Excel reports & review of existing code MasterOfDestr 4 736 Feb-14-2024, 03:39 PM
Last Post: MasterOfDestr
Lightbulb python code debuging yunus 1 353 Feb-11-2024, 03:48 PM
Last Post: deanhystad
  Python code to set column width 1418 11 1,345 Jan-20-2024, 07:20 AM
Last Post: Pedroski55
  Python code for alignment and font size 1418 0 336 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,079 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