Question

Write a script in python that inputs a line of plaintext and a distance value and...

Write a script in python that inputs a line of plaintext and a distance value and outputs an encrypted text using a Caesar cipher. The script should work for any printable characters.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


NOTE: The code i have written will encrypt every printable character including space, special symbol etc.


Code:

def main():
        # fetching plaintext as input
        line = input('Enter a plaintext: ')
        # taking the distance value
        dist = int(input('Enter the shift value: '))

        enc_char = ''   
        # iterating through each character in line
        for char in line:
                # if char is an alphabet
                if char.isalpha():
                        num = ord(char)
                        num += dist

                        # if char is a lowercase                        
                        if char.islower():
                                if num > ord('z'):
                                        num -= 26
                                elif num < ord('a'):
                                        num += 26

                        # if char is an uppercase
                        elif char.isupper():
                                if num > ord('Z'):
                                        num -= 26
                                elif num < ord('A'):
                                        num += 26

                        enc_char += chr(num)            

                # if char is not an alphabet and it is some digit or special character
                else:
                        num = ord(char)
                        num += dist
                        # assuming ascii characters are upto 127 characters
                        # if the char crossed this 127 we will rotate it
                        if num > 127:
                                num -= 127
                        enc_char += chr(num)

        print('The encrypted text is: ', enc_char)

if __name__=='__main__':
        main()

Sample Output:


Add a comment
Know the answer?
Add Answer to:
Write a script in python that inputs a line of plaintext and a distance value and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • So I wrote a script that inputs a line of plaintext and a distance value and...

    So I wrote a script that inputs a line of plaintext and a distance value and outputs an encrypted text using a Caesar cipher. The script should work for any printable characters: plainText = input("Enter a message: ") distance = int(input("Enter the distance value: ")) code = "" for ch in plainText: ordValue = ord(ch) cipherValue = ordValue + distance if cipherValue > 127: cipherValue = distance - (127 - ordValue + 1) code += chr(cipherValue) print(code) Now I need...

  • Python Programing Exercise 4.1

    Write a script that inputs a line of plaintext and a distance value and outputs an encrypted text using a Caesar cipher.

  • Using Python; Caesar part of the homework: Write c_encrypt() and c_decrypt(), both of which take two...

    Using Python; Caesar part of the homework: Write c_encrypt() and c_decrypt(), both of which take two arguments, the first one a string and the second one an integer key. Both should return a string. Vigenère part of the homework: Write vig_encrypt() and vig_decrypt() functions. Each takes two strings as inputs, with the first being the plaintext/ciphertext, and the second being the key. Both should be calling functions you wrote earlier to help make the work easier. The key will be...

  • Write a Python program which implements the following two classical cryptosystem which we covered n class:...

    Write a Python program which implements the following two classical cryptosystem which we covered n class: a) Affine Cipher b) Vigenere Cipher Your program should consist of at least five functions: a) Two functions named encrypt, one for each of the two algorithms which accepts a lowercase alphabetical plaintext string and key as input and outputs a corresponding cipher text string. b) Two functions named decrypt, one for each of the two algorithms which accepts a lowercase alphabetical ciphertext string...

  • Write a Python program that reads text from a file, encrypts it with a Caesar Cipher,...

    Write a Python program that reads text from a file, encrypts it with a Caesar Cipher, and displays the encrypted text. Do not process punctuation. Convert the original string to all lower-case before encrypting it.

  • USING C LANGUAGE Write a program to compute a Caesar Cipher . You must read in...

    USING C LANGUAGE Write a program to compute a Caesar Cipher . You must read in a file of text and convert some of the letters to another letter using the following array: char key[27] = “efghijklmnopqrstuvwxyzabcd”; This means that if you encounter the letter ‘a’, then you will replace it with the letter ‘e’,etc. If you encounter any other characters in the input file (example: ‘A’ or space, etc, you will write them as is. You will write each...

  • Write the programming C please, not C++. The main function should be to find the offset...

    Write the programming C please, not C++. The main function should be to find the offset value of the ciper text "wPL2KLK9PWWZ7K3ST24KZYKfPMKJ4SKLYOKRP4KFKP842LK0ZTY43 " and decrypt it. In cryptography, a Caesar Cipher is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be...

  • Python code. NO importing modules. you may not use: contains(), index(), sum(),join(), split(). def column_cipher(plaintext): •...

    Python code. NO importing modules. you may not use: contains(), index(), sum(),join(), split(). def column_cipher(plaintext): • Parameter(s): plaintext ----- a string; the message to be encrypted • Return value: A string; the ciphertext after applying the column cipher algorithm • Assumptions: o The standard English alphabet is used: "abcdefghijklmnopqrstuvwxyz" o All strings will be non-empty, entirely lowercase, and only contain valid letters in the alphabet. • How it works: o First, transpose the text into 5 different columns by writing...

  • C Program In this assignment you'll write a program that encrypts the alphabetic letters in a...

    C Program In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters Your program must compile and run from the command line. The program executable must be named “vigenere” (all lower...

  • Question 7 12.5 pts Recall that book ciphers do not necessarily require a full book to...

    Question 7 12.5 pts Recall that book ciphers do not necessarily require a full book to decode, but instead any written text, such as the Declaration of Independence. For the example discussed in class (the second Beale cipher), we created the key by numbering words and taking the first letter. But we can also number characters themselves instead, making sure to skip spaces. The following is a message encrypted with a book cipher, using the text of this question as...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT