Question

Why will this not work to do a 3 rail encryption? How would I make it...

Why will this not work to do a 3 rail encryption? How would I make it work? Will it encrypt special characters? The program language Python.

userString=input("What would you like to decrypt and encrypt?: ")

def threeRailEncrypt(plainText):

railOne=""

railTwo=""

railThree=""

for i in range(0,len(plainText),3):

railOne=railOne+plainText[i]

for i in range(1,len(plainText),3):

railTwo=railTwo+plainText[i]

for i in range(2,len(plainText),3):

railThree=railThree+plainText[i]

return railThree+railTwo+railOne

print()

print("Encrypted version:")

print(threeRailEncrypt(userString))

def threeRailDecrypt(cipherText):

thirdLen=len(cipherText)//3

if len(cipherText)%3==2:

x=1

else:

x=0

railThree=cipherText[:thirdLen]

railTwo=cipherText[thirdLen:2*thirdLen+x]

railOne=cipherText[2*thirdLen+x:]

plainText=""

  

for i in range(thirdLen):

plainText+=railOne[i]+railTwo[i]+railThree[i]

if len(cipherText)%3==1:

plainText+=railOne[thirdLen]

if len(cipherText)%3==2:

plainText+=railOne[thirdLen]

plainText+=railTwo[thirdLen]

return plainText

print()

print("Decrypted version:")

print(threeRailDecrypt(threeRailEncrypt(userString)))

Homework Answers

Answer #1
userString=input("What would you like to decrypt and encrypt?: ")

# function to encrypr the decrypted text
def threeRailEncrypt(plainText):

   # create empty rails
   railOne=""
   railTwo=""
   railThree=""

   # create rails 
   for i in range(0,len(plainText),4):
      railOne=railOne+plainText[i]

   for i in range(1,len(plainText),2):
      railTwo=railTwo+plainText[i]

   for i in range(2,len(plainText),4):
      railThree=railThree+plainText[i]

   return railOne + railTwo + railThree

print()
print("Encrypted version:")
print(threeRailEncrypt(userString))

# function to decrypt the encrypted text
def threeRailDecrypt(cipherText):

   # creating empty rails
   railOne = [' ' for i in range(len(cipherText))]
   railTwo = [' ' for i in range(len(cipherText))]
   railThree = [' ' for i in range(len(cipherText))]
   j = 0

   # storing the values at the correct positions
   for i in range(0, len(cipherText), 4):
      railThree[i] = cipherText[j]
      j += 1
   
   for i in range(1, len(cipherText), 2):
      railTwo[i] = cipherText[j]
      j += 1
      
   
   for i in range(2, len(cipherText), 4):
      railOne[i] = cipherText[j]
      j += 1    
   
   # recollecting the text
   plainText = ''
   for i in range(len(cipherText)):
      if railThree[i] != ' ':
         plainText += railThree[i]
      if railTwo[i] != ' ':
         plainText += railTwo[i]
      if railOne[i] != ' ':
         plainText += railOne[i]
   
   return plainText

print()
print("Decrypted version:")
print(threeRailDecrypt(threeRailEncrypt(userString)))

FOR HELP PLEASE COMMENT.
THANK YOU.

Know the answer?
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
Similar Questions
C Programming I have this function to i want to return the cipher text, but its...
C Programming I have this function to i want to return the cipher text, but its not working, can anyone try to see what i'm doing wrong. I need it to return the cipher text. char* vigenereCipher(char *plainText, char *k) { int i; char cipher; int cipherValue; int len = strlen(k); char *cipherText = (char *)malloc(sizeof(plainText) * sizeof(char)); //Loop through the length of the plain text string for (i = 0; i < strlen(plainText); i++) { //if the character is...
USE C++!!!! Encryption and Decryption are two cryptographic techniques. Encryption is used to transform text to...
USE C++!!!! Encryption and Decryption are two cryptographic techniques. Encryption is used to transform text to meaningless characters, and decryption is used to transform meaningless characters into meaningful text. The algorithm that does the encryption is called a cipher. A simple encryption algorithm is Caesar cipher, which works as follows: replace each clear text letter by a letter chosen to be n places later in the alphabet. The number of places, n, is called the cipher key. For example, if...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise XOR) to encrypt/decrypt a message. The program will prompt the user to select one of the following menu options: 1. Enter and encrypt a message 2. View encrypted message 3. Decrypt and view the message (NOTE: password protected) 4. Exit If the user selects option 1, he/she will be prompted to enter a message (a string up to 50 characters long). The program will...
Python 3 Rewrite KNN sample code using KNeighborsClassifier . ● Repeat KNN Step 1 – 5,...
Python 3 Rewrite KNN sample code using KNeighborsClassifier . ● Repeat KNN Step 1 – 5, for at least five times and calculate average accuracy to be your result. ● If you use the latest version of scikit -learn, you need to program with Python >= 3.5. ● Use the same dataset: “ iris.data ” ● Split your data: 67% for training and 33% for testing ● Draw a line chart: Use a “for loop” to change k from 1...
""" ''' Write a python code to push all zeors to the end of an array...
""" ''' Write a python code to push all zeors to the end of an array ''' import numpy as np def Move_a(i):    num = len(a)    for k in range (i, num-1): a[k] = a[k+1] a[num-1] = 0    return a a = np.array([0,1,4,7,0,9,12,0,0,15,0,21]) #length of array (len) num = len(a) print (num) for i in range(0,num): if (a[i] == 0): #Functioon call to Move_a() a = Move_a(i)       print ("the array looks like") print (a) My...
For each of the following Python programs P and input strings I, give the output P(I),...
For each of the following Python programs P and input strings I, give the output P(I), using the formal definition of P(I) given, and employing any reasonable reference computer C: Definition of P(I), the output of a Python program. Let P be a Python program with respect to a reference computer system C. Suppose P has main function M, and let I be a string of ASCII characters that will be used as input to P. The output of P...
Python problem: write a main function that ask user for 2 file name, open files and...
Python problem: write a main function that ask user for 2 file name, open files and read the 1st line of each and compare them using hamming function(below is the hamming function I wrote). Just assume the fist line of each file only contains 0s and 1s, such as 0100111101. The main function may have to do some extra work to remove newline characters or other whitespace from the text read from each file. This is hamming function that need...
# Parts to be completed are marked with '<<<<< COMPLETE' import random N = 8 MAXSTEPS...
# Parts to be completed are marked with '<<<<< COMPLETE' import random N = 8 MAXSTEPS = 5000 # generates a random n-queens board # representation: a list of length n the value at index i is # row that contains the ith queen; # exampe for 4-queens: [0,2,0,3] means that the queen in column 0 is # sitting in row 0, the queen in colum 1 is in row, the queen in column 2 # is in row 0,...
I am a student taking python programming. Can this problem be modified using the define main...
I am a student taking python programming. Can this problem be modified using the define main method, def main()? import random #function definition #check for even and return 0 if even def isEven(number): if(number%2==0): return 0 #return 1 if odd else: return 1 #count variables even =0 odd = 0 c = 0 #loop iterates for 100 times for i in range(100): #generate random number n = random.randint(0,1000) #function call val = isEven(n) #check value in val and increment if(val==0):...
Hi I need to make a hangman game for school task and it is no an...
Hi I need to make a hangman game for school task and it is no an ordinary game. It has to incorporate the pictures and everytime something wrong is guessed it has to clock over the pictures. The pictures are: hangman_pics = [''' +---+ | | | | | | =========''', ''' +---+ | | O | | | | =========''', ''' +---+ | | O | | | | | =========''', ''' +---+ | | O | /| |...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT