• Register
0 votes
453 views

Problem :

I need to write a program that can encode and decode caesar cipher python.
6.9k points

Please log in or register to answer this question.

2 Answers

0 votes

Solution:

PFB the program that can encode and decode caesar ciphar python

def main():
    print("This program can encode and decode Caesar Ciphers")
    inputText = input("Please enter a string of plaintext:").lower()
    inputValue = int(input("Please enter the value of the key:")) # use int(), don't eval unless you read more about it
    inputEorD = input("Please enter e (to encrypt) or d (to decrypt) ")
    codedMessage = ""

    if inputEorD == "e":
        for ch in inputText:
            codedMessage += chr((ord(ch) - 97 + inputValue)%26 + 97)
    elif inputEorD =="d":
            codedMessage += chr((ord(ch) - 97 - inputValue)%26 + 97)
    else:
        print("You did not enter E/D! Try again!!")
    print("The text inputed:", inputText,  ".Is:", inputEorD, ".By the key of",inputValue, ".To make the message", codedMessage)

main()

Further Readings:

38.6k points
edited by
0 votes

Solution:

Because you're dealing with .lower()-case letters, it's right to know that their ASCII range is [97-122].

A proper way to do the shifting circular would be to represent each letter with the range [0-25], which is done by ord(ch) - 97, and then include the key, then modulo the result with 26 so it becomes (ord(ch) - 97 + key)%26, we'll then have a result in range [0-25], including 97 will then obtain it's ASCII code:

def main():
    print("This program can encode and decode Caesar Ciphers")
    inputText = input("Please enter a string of plaintext:").lower()
    inputValue = int(input("Please enter the value of the key:")) # use int(), don't eval unless you read more about it
    inputEorD = input("Please enter e (to encrypt) or d (to decrypt) ")
    codedMessage = ""

    if inputEorD == "e":
        for ch in inputText:
            codedMessage += chr((ord(ch) - 97 + inputValue)%26 + 97)
    elif inputEorD =="d":
            codedMessage += chr((ord(ch) - 97 - inputValue)%26 + 97)
    else:
        print("You did not enter E/D! Try again!!")
    print("The text inputed:", inputText,  ".Is:", inputEorD, ".By the key of",inputValue, ".To make the message", codedMessage)

main()

Program that accepts a Text (string) and Shift value( integer) and returns the encrypted text.

// A C++ program to illustrate Caesar Cipher Technique 
#include <iostream> 
using namespace std; 

// This function receives text and shift and 
// returns the encrypted text 
string encrypt(string text, int s) 
{ 
	string result = ""; 

	// traverse text 
	for (int i=0;i<text.length();i++) 
	{ 
		// apply transformation to each character 
		// Encrypt Uppercase letters 
		if (isupper(text[i])) 
			result += char(int(text[i]+s-65)%26 +65); 

	// Encrypt Lowercase letters 
	else
		result += char(int(text[i]+s-97)%26 +97); 
	} 

	// Return the resulting string 
	return result; 
} 

// Driver program to test the above function 
int main() 
{ 
	string text="ATTACKATONCE"; 
	int s = 4; 
	cout << "Text : " << text; 
	cout << "\nShift: " << s; 
	cout << "\nCipher: " << encrypt(text, s); 
	return 0; 
} 

In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the easiest and most broadly known encryption techniques. It is a type of substitution cipher in which every letter in the plaintext is replaced by a letter few fixed number of positions down the alphabet. Example given- with a left shift of 3, D would be replaced by A, E would become B, and so on. The process is named after Julius Caesar, who employed it in his private conformation.

Python Code:

#https://gist.github.com/nchitalov/2f2b03e5cf1e19da1525
def caesar_encrypt(realText, step):
	outText = []
	cryptText = []
	
	uppercase = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
	lowercase = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

	for eachLetter in realText:
		if eachLetter in uppercase:
			index = uppercase.index(eachLetter)
			crypting = (index + step) % 26
			cryptText.append(crypting)
			newLetter = uppercase[crypting]
			outText.append(newLetter)
		elif eachLetter in lowercase:
			index = lowercase.index(eachLetter)
			crypting = (index + step) % 26
			cryptText.append(crypting)
			newLetter = lowercase[crypting]
			outText.append(newLetter)
	return outText

code = caesar_encrypt('abc', 2)
print()
print(code)
print()

Program for Caesar Cipher in Python

def encrypt(string, shift):
 
  cipher = ''
  for char in string: 
    if char == ' ':
      cipher = cipher + char
    elif  char.isupper():
      cipher = cipher + chr((ord(char) + shift - 65) % 26 + 65)
    else:
      cipher = cipher + chr((ord(char) + shift - 97) % 26 + 97)
  
  return cipher
 
text = input("enter string: ")
s = int(input("enter shift number: "))
print("original string: ", text)
print("after encryption: ", encrypt(text, s))

 

31.7k points
edited by

Related questions

0 votes
1 answer 209 views
209 views
Problem: how java provides a class Base64 to deal with encryption? You can encrypt and decrypt your data by using provided methods. You need to import java.util.Base64 in your source file to use its methods. how much class provides different encoders and decoders to encrypt information at each level.
asked Nov 6, 2020 Mashhoodch 13k points
0 votes
1 answer 13 views
13 views
Problem : How do I solve it: Write a program that can encode and decode caesar ciphers?
asked Jun 23 Siam55 110k points
0 votes
1 answer 9 views
9 views
Problem : Caesar cipher encryption and decryption program in c
asked Jun 27 Sifat55 108k points
0 votes
1 answer 20 views
20 views
Problem: I have a problem when I'm trying to decode an encrypted message. It decrypts almost everything as it should be, but when I try to decrypt 'w x y z' (all lower case), it doesn't work properly. It only decrypts those letters when they are UPPERCASE. What did I do wrong?
asked May 12 Irenka Kiri 85.5k points
0 votes
1 answer 11 views
11 views
Problem: What is the way of solving it? > C program for encryption and decryption using caesar cipher.
asked May 8 Irenka Kiri 85.5k points
0 votes
1 answer 6 views
6 views
Problem: Simple caesar cipher program in c language.
asked Jul 12 Rada Gracelynn 104k points
0 votes
1 answer 1 view
1 view
Problem : C++ caesar cipher encryption and decryption
asked Jul 10 Sifat55 108k points
0 votes
0 answers 8 views
8 views
Removes the numbers, special characters-Converts uppercase letters to lowercase letters, and vice versa
asked Mar 31 smita 120 points
0 votes
1 answer 10 views
10 views
Problem: Please give me an answer to this : Write a program that asks the user to enter five test scores python.
asked Jun 19 Florina Gulnar 106k points
1 vote
1 answer 256 views
256 views
Problem: I wrote a program that asks the user to enter the amount of a purchase and then it sums up the total bill with 15% vat and 0.07% service tax. I wrote the program in python: vat = 0.15 sales_tax = 0.075 product_price = float(input("Please enter the ... not defined I can not solve this thing. I am new in python programming language so please help me to solve this error. Thanks in advance.
asked May 6, 2020 samhaz 5k points