Cryptography in Python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Cryptography in Python (/thread-15267.html) |
Cryptography in Python - NLittle17 - Jan-10-2019 print("CryptoPrac-1a.") for x in range(97, 123): # 97 starts the lowercase alphabet and 122 is z (ending) print(chr(x), end = ' ') print('\n') for x in range(97, 123): print(chr(x + 3), end = ' ') print("\n CryptoPrac-1b.")I am trying to add to the second part of this code. What I am trying to do is keep the shift by three but only this time I am trying to use the modulus operator. I am using the ASCII table but I don't know how to start a for loop to do what I want to do . RE: Cryptography in Python - Larz60+ - Jan-10-2019 As your ultimate goal, are you trying to display something like this: https://python-reference.readthedocs.io/en/latest/docs/str/ASCII.html ? RE: Cryptography in Python - NLittle17 - Jan-10-2019 (Jan-10-2019, 12:07 PM)Larz60+ Wrote: As your ultimate goal, are you trying to display something like this: https://python-reference.readthedocs.io/en/latest/docs/str/ASCII.html I am trying to have an output like this:
RE: Cryptography in Python - Larz60+ - Jan-10-2019 There are better ways to do this: If you are using python 3.6 or newer: >>> import string >>> funk = '{|}' >>> mystr = f'{string.ascii_lowercase[3:]}{funk}' >>> mystr = ' '.join(mystr) >>> mystr 'd e f g h i j k l m n o p q r s t u v w x y z { | }' >>>otherwise: >>> import string >>> funk = '{|}' >>> mystr = '{}{}'.format(string.ascii_lowercase[3:], funk)>>> mystr = ' '.join(mystr) >>> mystr 'd e f g h i j k l m n o p q r s t u v w x y z { | }' >>>actually you can combine the join: >>> import string >>> funk = '{|}' >>> mystr = ' '.join(f'{string.ascii_lowercase[3:]}{funk}') >>> mystr 'd e f g h i j k l m n o p q r s t u v w x y z { | }' >>> RE: Cryptography in Python - NLittle17 - Jan-13-2019 (Jan-10-2019, 12:58 PM)Larz60+ Wrote: There are better ways to do this: If you are using python 3.6 or newer:>>> import string >>> funk = '{|}' >>> mystr = f'{string.ascii_lowercase[3:]}{funk}' >>> mystr = ' '.join(mystr) >>> mystr 'd e f g h i j k l m n o p q r s t u v w x y z { | }' >>>otherwise:>>> import string >>> funk = '{|}' >>> mystr = '{}{}'.format(string.ascii_lowercase[3:], funk)>>> mystr = ' '.join(mystr) >>> mystr 'd e f g h i j k l m n o p q r s t u v w x y z { | }' >>>actually you can combine the join:>>> import string >>> funk = '{|}' >>> mystr = ' '.join(f'{string.ascii_lowercase[3:]}{funk}') >>> mystr 'd e f g h i j k l m n o p q r s t u v w x y z { | }' >>> Thank you. RE: Cryptography in Python - Larz60+ - Jan-13-2019 using: >>> import string >>> funk = '{|}' >>> mystr = ' '.join(f'{string.ascii_lowercase[3:]}{funk}') >>> mystr 'd e f g h i j k l m n o p q r s t u v w x y z { | }' >>>won't work. the >>> is the python interactive interpreter prompt in code use (for example): import string funk = '{|}' mystr = f'{string.ascii_lowercase[3:]}{funk}' mystr = ' '.join(mystr) print(mystr) 'd e f g h i j k l m n o p q r s t u v w x y z { | }' Cryptography in Python (Encrypt and Decrypt) - NLittle17 - Jan-13-2019 So I was able to come up with the following code: def encrypt(text): result = "" s = 3 # traverse text for i in range(len(text)): char = text[i] # Encrypt uppercase characters if (char.isupper()): result += chr((ord(char) + s-65) % 26 + 65) # Encrypt lowercase characters else: result += chr((ord(char) + s - 97) % 26 + 97) return result def decrypt(text): result = "" s = 26-3 # traverse text for i in range(len(text)): char = text[i] # Decrypt uppercase characters if (char.isupper()): result += chr((ord(char) + s-65) % 26 + 65) # Decrypt lowercase characters else: result += chr((ord(char) + s - 97) % 26 + 97) return result #check the above function text = input("Enter Message Please:") print("Text : " + text) cipher = encrypt(text) print("Cipher: " + cipher) print("De-Cipher: " + decrypt(cipher))HOWEVER, the output is: How can I modify this to have the following output instead?
|