In: Computer Science
python programming
• Appropriate and well constructed while and/or for loops (as necessary). • Appropriate if, if-else, if-elif-else statements (as necessary). • The use of the ord() and chr() functions (as necessary). • The following three functions (refer to stage 6 for description): o display_details() o get_menu_choice() o get_offset() • Output that strictly adheres to the assignment specifications. If you are not sure about these details, you should check with the ‘Sample Output – Part II’ provided at the end of this document. • Good programming practice: o Consistent commenting, layout and indentation. You are to provide comments to describe: your details, program description, all variable definitions, and significant sections of code. o Meaningful variable names. • Your solutions MAY make use of the following built-in functions and methods: o Any of the Python built-in functions… specifically, you may find you may need the following: int(), input(), print(), range(), ord(),and chr(). • Your solutions MAY ALSO make use of the following: o Concatenation (+) operator to create/build new strings. o Access the individual elements in a string with an index (one element only). i.e. string_name[index]. o Your own (user-defined) functions (in addition to the three functions listed above). Your solutions MUST NOT use: • break, or continue statements in your solution. Do not use the quit() or exit() functions or the break or return statements (or any other techniques) as a way to break out of loops. Doing so will result in a significant mark deduction.
Sample output
*** Menu ***
1. Encrypt string
2. Decrypt string
3. Brute force decryption
4. Quit
What would you like to do [1,2,3,4]? 1
Please enter string to encrypt: Elvis has left the building!
Please enter offset value (1 to 94): 3
Encrypted string: Hoylv#kdv#ohiw#wkh#exloglqj$
*** Menu ***
1. Encrypt string
2. Decrypt string
3. Brute force decryption
4. Quit What would you like to do [1,2,3,4]? 2
Please enter string to decrypt: Hoylv#kdv#ohiw#wkh#exloglqj$ Please enter offset value (1 to 94): 3
Decrypted string: Elvis has left the building!
*** Menu ***
1. Encrypt string
2. Decrypt string
3. Brute force decryption
4. Quit What would you like to do [1,2,3,4]? 3
Please enter string to decrypt: Hoylv#kdv#ohiw#wkh#exloglqj$
Offset: 1 = Decrypted string: Gnxku"jcu"nghv"vjg"dwknfkpi# Offset: 2 = Decrypted string: Fmwjt!ibt!mfgu!uif!cvjmejoh" Offset: 3 = Decrypted string: Elvis has left the building! Offset: 4 = Decrypted string: Dkuhr~g`r~kdes~sgd~athkchmf Offset: 5 = Decrypted string: Cjtgq}f_q}jcdr}rfc}`sgjbgle~ Offset: 6 = Decrypted string: Bisfp|e^p|ibcq|qeb|_rfiafkd} Offset: 7 = Decrypted string: Ahreo{d]o{habp{pda{^qeh`ejc| Offset: 8 = Decrypted string: @gqdnzc\nzg`aozoc`z]pdg_dib{ Offset: 9 = Decrypted string: ?fpcmyb[myf_`nynb_y\ocf^chaz Offset: 10 = Decrypted string: >eoblxaZlxe^_mxma^x[nbe]bg`y Offset: 11 = Decrypted string: =dnakw`Ykwd]^lwl`]wZmad\af_x Offset: 12 = Decrypted string: P\IBCQ\QEB\?RFIAFKD] Offset: 39 = Decrypted string: !HREO[D=O[HABP[PDA[>QEH@EJC\ Offset: 40 = Decrypted string: GQDNZCCHAZ Offset: 42 = Decrypted string: }EOBLXA:LXE>?MXMA>X;NBE=BG@Y Offset: 43 = Decrypted string: |DNAKW@9KWD=>LWL@=W:MADW Offset: 45 = Decrypted string: zBL?IU>7IUB;;U8K?B:?D=V Offset: 46 = Decrypted string: yAK>HT=6HTA:;ITI=:T7J>A9>CH;EQ:3EQ>78FQF:7Q4G;>6;@9R Offset: 50 = Decrypted string: u=G:DP92DP=67EPE96P3F:=5:?8Q Offset: 51 = Decrypted string: t7P Offset: 52 = Decrypted string: s;E8BN70BN;45CNC74N1D8;38=6O Offset: 53 = Decrypted string: r:D7AM6/AM:34BMB63M0C7:27<5N Offset: 54 = Decrypted string: q9C6@L5.@L923ALA52L/B6916;4M Offset: 55 = Decrypted string: p8B5?K4-?K812@[email protected]:3L Offset: 56 = Decrypted string: o7A4>J3,>J701?J?30J-@47/492K Offset: 57 = Decrypted string: n6@3=I2+=I6/0>I>2/I,?36.381J Offset: 58 = Decrypted string: m5?2
25-270I Offset: 59 = Decrypted string: l4>1;G0);G4-.' 2>+$%3>3'$>!4(+#(-&? Offset: 69 = Decrypted string: b*4'1=&~1=*#$2=2= 3'*"',%> Offset: 70 = Decrypted string: a)3&0<%}0<)"#1<1%"<~2&)!&+$= Offset: 71 = Decrypted string: `(2%/;$|/;(!"0;0$!;}1%( %*#< Offset: 72 = Decrypted string: _'1$.:#{.:' !/:/# :|0$'~$)"; Offset: 73 = Decrypted string: ^&0#-9"z-9&~ .9."~9{/#&}#(!: Offset: 74 = Decrypted string: ]%/",8!y,8%}~-8-!}8z."%|"' 9 Offset: 75 = Decrypted string: \$.!+7 x+7$|},7, |7y-!${!&~8 Offset: 76 = Decrypted string: [#- *6~w*6#{|+6+~{6x, #z %}7 Offset: 77 = Decrypted string: Z",~)5}v)5"z{*5*}z5w+~"y~$|6 Offset: 78 = Decrypted string: Y!+}(4|u(4!yz)4)|y4v*}!x}#{5 Offset: 79 = Decrypted string: X *|'3{t'3 xy(3({x3u)| w|"z4 Offset: 80 = Decrypted string: W~){&2zs&2~wx'2'zw2t({~v{!y3 Offset: 81 = Decrypted string: V}(z%1yr%1}vw&1&yv1s'z}uz x2 Offset: 82 = Decrypted string: U|'y$0xq$0|uv%0%xu0r&y|ty~w1 Offset: 83 = Decrypted string: T{&x#/wp#/{tu$/$wt/q%x{sx}v0 Offset: 84 = Decrypted string: Sz%w".vo".zst#.#vs.p$wzrw|u/ Offset: 85 = Decrypted string: Ry$v!-un!-yrs"-"ur-o#vyqv{t. Offset: 86 = Decrypted string: Qx#u ,tm ,xqr!,!tq,n"uxpuzs- Offset: 87 = Decrypted string: Pw"t~+sl~+wpq + sp+m!twotyr, Offset: 88 = Decrypted string: Ov!s}*rk}*vop~*~ro*l svnsxq+ Offset: 89 = Decrypted string: Nu r|)qj|)uno})}qn)k~rumrwp* Offset: 90 = Decrypted string: Mt~q{(pi{(tmn|(|pm(j}qtlqvo) Offset: 91 = Decrypted string: Ls}pz'ohz'slm{'{ol'i|pskpun( Offset: 92 = Decrypted string: Kr|oy&ngy&rklz&znk&h{orjotm' Offset: 93 = Decrypted string: Jq{nx%mfx%qjky%ymj%gznqinsl& Offset: 94 = Decrypted string: Ipzmw$lew$pijx$xli$fymphmrk%
*** Menu ***
1. Encrypt string
2. Decrypt string
3. Brute force decryption
4. Quit What would you like to do [1,2,3,4]? 4 Goodbye.
Note: Your program must work with the printable ASCII character set. That is, all the characters from ASCII 32 (Space) to ASCII 126 (~). When the offset points to a character beyond 126 it should wrap around to the beginning of the set.
# display menu options def display_details(): print('***Menu***') print('1. Encrypt string') print('2. Decrypt string') print('3. Brute force decryption') print('4. Quit') # returns a number from 1 to 4 indicating menu option def get_menu_choice(): # prompt user for input choice = input('What would you like to do [1,2,3,4]? ') # validate user input if choice.isdigit(): option = int(choice) # check if option is in valid range if (option < 5) and (option > 0): # return valid input return option else: # option is not in valid range # print error and try again print('Invalid Input!') return get_menu_choice() else: # non digit character received # print error message print('Invalid Input!') # try again for valid input return get_menu_choice() # returns a number from 1 to 94 indicating offset value def get_offset(): # prompt user for input choice = input('Please enter offset value (1 to 94): ') # validate user input if choice.isdigit(): option = int(choice) # check if option is in valid range if (option < 95) and (option > 0): # return valid input return option else: # option is not in valid range # print error and try again print('Invalid Input!') return get_offset() else: # non digit character received # print error message print('Invalid Input!') # try again for valid input return get_offset() # Encrypts a given string with given offset # increase each character value in the string to given offset # prints resulting string def encrypt(message, offset): # create a string to print encoded = '' # loop through message and increase its character values for i in range(len(message)): # get integer value of individual character value = ord(message[i]) # increase value to offset value = value + offset # check if value is in printable range if value > 126: # wrap value at start value = value - 93 # get character of the new value character = chr(value) # build string character by character encoded = encoded + character # print encoded message print('Encrypted string: ' + encoded) # Decrypts a given string with given offset # decrease each character value in the string to given offset # prints resulting string def decrypt(message, offset): # create a string to print decoded = '' # loop through message and decrease its character values for i in range(len(message)): # get integer value of individual character value = ord(message[i]) # decrease value to offset value = value - offset # check if value is in printable range if value < 32: # wrap value at end value = value + 95 # get character of the new value character = chr(value) # build string character by character decoded = decoded + character # print decoded message print('Decrypted string: ' + decoded) # main function to run the program def main(): # prompt user for input till Quit is selected option = 0 # initialize value to enter in loop while option != 4: display_details() option = get_menu_choice() # check user selected option if option == 1: # user selected to Encrypt string # ask user for a string to encrypt message = input('Please enter string to encrypt: ') encrypt(message, get_offset()) elif option == 2: # user selected to Decrypt string # ask user for a string to decrypt message = input('Please enter string to decrypt: ') decrypt(message, get_offset()) elif option == 3: # user selected to brute force decryption # ask user for a string to decrypt message = input('Please enter string to decrypt: ') # try every offset from 1 to 94 to decrypt the message for i in range(94): # print each offset tried print('Offset:', end=' ') print(i+1, end=' = ') decrypt(message, i+1) else: # when option is Quit print end message print('Goodbye') # run the program main()
let me know if you have any doubts or problem running the code.