Question

In: Computer Science

I need the code in python where I can encrypt and decrypt any plaintext. For example,...

I need the code in python where I can encrypt and decrypt any plaintext. For example, the plaintext "hello" from each of these Block Cipher modes of Operation.

Electronic Code Block Mode (ECB)

Cipher block Mode (CBC)

Cipher Feedback Mode (CFB)

Output feedback Mode (OFB)

Counter Mode (CTR)

Here is an example, Affine cipher expressed in C.

Encryption:

char cipher(unsigned char block, char key) {

return (key+11*block) }

Decryption:

char invcipher(unsigned char block, char key) {

return (163*(block-key+256)) }

Solutions

Expert Solution

Python supports many encryption algorithms. I have provided an example using one of the simplest Data Encryption Standard (DES) algorithm in Python.
DES Algorithm is supported in the DES module which is a part of Pycrypto package.

#This line of code imports the module DES
#Alternatively you can also use: from Crypto.Cipher import DES
import DES

#This line of code creates an object of type DES
#The first parameter to new function is the key used for encryption.
# In the second parameter you can select the mode which can be either of
# the six block cipher modes as follows
# MODE_ECB, MODE_CBC, MODE_CFB, MODE_OFB, MODE_CTR
# if the CBC or CFB modes are used, the new function will have a third parameter
# The third parameter should be same length as the block size.
obj = DES.new('key', DES.MODE_ECB)

plaintext = "hello"

# Append string 'PPP' to convert the length of plaintext string hello into multiple of 8
# for DES algorithm strings are required to be in multiples of 8
#encrypt the block cipher text
ciphertext = obj.encrypt(plaintext+'PPP')

#Decrypt the block cipher text
obj.decrypt(ciphertext)


Related Solutions

Write a small program to encrypt and decrypt a message using Python library.
Write a small program to encrypt and decrypt a message using Python library.
Using Python, write a simple application that takes user input of plaintext and key, and encrypt...
Using Python, write a simple application that takes user input of plaintext and key, and encrypt the plaintext with Vigenere Cipher. The application should then print out the plaintext and the ciphertext.
Hello! I'm trying to write a code that will either encrypt/decrypt a message from an inputed...
Hello! I'm trying to write a code that will either encrypt/decrypt a message from an inputed text. I'm having the absolute hardest time getting stared and figuring out how to identify in the code if the input needs to be encrypted/decrypted and identifying the string to encrypt/decrypt with. These are the instructions: Objectives Command line input File input and output Rethrowing exceptions Program Description Gaius Julius Caesar encoded his battle messages so that the opponent could not read them should...
Answer in python! 5.16 LAB: Cryptographic Hashing Algorithms Encrypting text allows us to encrypt and decrypt...
Answer in python! 5.16 LAB: Cryptographic Hashing Algorithms Encrypting text allows us to encrypt and decrypt the text using a special key. Another method of encrypting text / passwords is called hashing. Hashing uses special algorithms to 'scramble' text so that it is tougher to hack. The hash function can take numbers, letters, and symbols then uses one of the special algorithms to output scrambled text. The longer the output string, the harder to hack the data. The difference between...
this is a python code that i need to covert to C++ code...is this possible? if...
this is a python code that i need to covert to C++ code...is this possible? if so, can you please convert this pythin code to C++? def main(): endProgram = 'no' print while endProgram == 'no': print # declare variables notGreenCost = [0] * 12 goneGreenCost = [0] * 12 savings = [0] * 12 months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] getNotGreen(notGreenCost, months) getGoneGreen(goneGreenCost, months) energySaved(notGreenCost, goneGreenCost, savings) displayInfo(notGreenCost, goneGreenCost, savings, months)...
I need to add this checkpoint to an existing code that i have in python Checkpoint...
I need to add this checkpoint to an existing code that i have in python Checkpoint 1: Once you have created and tested the Bank Account Class create subclasses to the BankAccount class. There should be two subclasses, CheckingAccount and SavingsAccount. You should add interest_rate to the parent BankAccount class. To the CheckingAccount add variables called per_check_fee default to false and allow_overdraft default to True. To SavingsAccount add the variable transactions_per_month, default it to 5. Create instances of CheckingAccount and...
Python: I am not sure where to begin with this question, and I hope I can...
Python: I am not sure where to begin with this question, and I hope I can get an input on it. This is in regards to Downey's program, and we are asked to make two changes. Downey prints time as they do in the Army: 17:30:00 hours. We want to print that as 5:30 PM. Downey lets you define the time 25:00:00 - we want to turn over at 23:59:59 to 00:00:00. (I am asked to identify my changes with...
Q: decrypt the message MPFOAIMSTTAITLEYRO there is no any other hint. That is why I dont...
Q: decrypt the message MPFOAIMSTTAITLEYRO there is no any other hint. That is why I dont know how to decrypt it.. please figure it out with showing your method.
Q: Using Python: Am trying to write a code that can be tested. Any assistance will...
Q: Using Python: Am trying to write a code that can be tested. Any assistance will be appreciated. Would want Help in understanding and solving this example from Data Structure & Algorithms (Computer Science) with the steps of the solution to better understand, thanks. The purpose of this assignment is the application of queues. A prime number is a positive integer other than 1 and is divisible only by 1 and itself. For example, 7 is a prime number because...
I need a written contract that can be breached. It can be any kind of made...
I need a written contract that can be breached. It can be any kind of made up contract. Module 02 Course Project - The Contract: (This project is a compilation of skills required to understand Internet Law in its many facets. The students will demonstrate their knowledge of the Internet, laws associated with the internet, cases associated with the laws of the internet, possible remedies or sanctions given a break in the law and the ability to be self-reliant in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT