Python Forum
Thread Rating:
  • 2 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cryptography in Python
#1
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 Sad .
Reply
#2
As your ultimate goal, are you trying to display something like this: https://python-reference.readthedocs.io/...ASCII.html
?
Reply
#3
(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/...ASCII.html
?

I am trying to have an output like this:
Output:
d e f g h i j k l m n o p q r s t u v w x y z { | }
Reply
#4
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 { | }'
>>>
Reply
#5
(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.
Reply
#6
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 { | }'
Reply
#7
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:
Output:
Enter Message Please: Hello World! Text : Hello World! Cipher: KhoorqZruogr De-Cipher: HellonWorldo
How can I modify this to have the following output instead?

Output:
Enter Message Please: Hello World! Text : Hello World! Cipher: Khoor Zruogr! De-Cipher: Hello World!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [cryptography.io] How to convert DER signature to ECDSA fstefanov 1 2,986 Jul-04-2019, 08:59 AM
Last Post: fstefanov
  cryptography help jorrdn 3 3,089 Mar-10-2018, 02:29 PM
Last Post: sparkz_alot
  Python cryptography or pycrypto lexlukkia 4 47,400 Jan-25-2017, 12:08 PM
Last Post: wavic

Forum Jump:

User Panel Messages

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