vigenere cipher - not adding correct values - python

I want to get specific values from a for loop to add to another string to create a vigenere cipher.
here's the code.
userinput = input('enter message')
keyword = input('enter keyword')
new = ''
for a in keyword:
pass
for i in (ord(x) for x in userinput):
if 96 < i < 123: #lowercase
new += chr(97 + (i+ord(a)-97)#keeps all values in alphabet
print(new)
so the answer i want if i do 'abcd' as my message and 'ab' as my keyword the desired outcome is 'bddf' as 'a' + 'a' is 'b' and 'b' + 'b' = 'd' and etc. how would i change the code to match my desired outcome or will i have to change it completely and how would i go about doing so.

try this (you are missing the mod 26-part):
from itertools import cycle
plaintext = input('enter message: ')
keyword = input('enter keyword: ')
def chr_to_int(char):
return 0 if char == 'z' else ord(char)-96
def int_to_chr(integer):
return 'z' if integer == 0 else chr(integer+96)
def add_chars(a, b):
return int_to_chr(( chr_to_int(a) + chr_to_int(b) ) % 26 )
def vigenere(plaintext, keyword):
keystream = cycle(keyword)
ciphertext = ''
for pln, key in zip(plaintext, keystream):
ciphertext += add_chars(pln, key)
return ciphertext
ciphertext = vigenere(plaintext, keyword)
print(ciphertext)
if you like list comprehensions, you can also write
def vigenere(plaintext, keyword):
keystream = cycle(keyword)
return ''.join(add_chars(pln, key)
for pln, key in zip(plaintext, keystream))
UPDATE
updated according to the wish that a+a=b. note that z is in that case the neutral element for the addition (z+char=z).

Related

How to avoid backspace/delete/null when print cipher text?

I am testing a substitution cipher. I'm using the 256 standard ASCII. Here's my code:
def Encrypt(psw, key):
res = ""
for i in psw:
new_letter = chr((ord(i)+key)%255)
if new_letter == '\n':
new_letter = '。'
res += new_letter
return res
def Decrypt(psw, key):
res = ""
for i in psw:
if i == '。':
i = '\n'
old_letter = chr((ord(i)-key)%255)
res += old_letter
return res
if __name__ == "__main__":
try:
while True:
i = input("1 for encryption, 2 for decryption")
s = "";k = 0
if i == '1':
s = input("Input Plaintext:")
k = (int)(input("Input Key:"))
print("Ciphertext is:"+Encrypt(s, k))
elif i == '2':
s = input("Input Ciphertext:")
k = (int)(input("Input Key:"))
print("Plaintext is:"+Decrypt(s, k))
else:
break
except:
print("Error")
When I test some keys, such as 6012, the ciphertext is ãøôøø³ø³ûº³ú
I used this result to restore the plaintext, but failed, and it became Peasee me wh's wrong
I think this is due to special operators such as Delete, Backspace, and Null in the encryption process. It caused me to be unable to recover. Is there any way to solve this problem?
Most of the time, you should not print binary data (ciphertext) in a human-text channel (stdout) because it will interpret the non-printable data instead of displaying it.
If what you want is just to be able to copy-paste, you should escape the non-printable bits. One simple way to do it is to use urllib.parse.quote and unquote :
import urllib.parse
[...]
print("Ciphertext is: "+urllib.parse.quote(Encrypt(s, k)))
[...]
s = urllib.parse.unquote(input("Input Ciphertext:"))
Example :
1 for encryption, 2 for decryption
1
Input Plaintext:hello
Input Key:6012
Ciphertext is:%C3%BB%C3%B8%00%00%03
1 for encryption, 2 for decryption
2
Input Ciphertext:%C3%BB%C3%B8%00%00%03
Input Key:6012
Plaintext is:hello

Need help fixing a ValueError of and 'i' index

So I'm making this code that uses strings to cipher a given phrase with a given key (I can't use the preset functions in python to do it). I believe I've got the idea of how to do it but I;m experiencing this error that I can't figure out how to fix. My code is the following.
def inputForChipher():
input1 = False
input2 = False
while input1 == False:
string = input('Enter the message: ')
input1 = verifyString(string)
while input2 == False:
key = input('Enter the key you want to use: ')
input2 = verifyString(key)
print(cipherWithKey(string, key))
def cipherWithKey(string, key):
string = string.lower()
key = key.lower()
letters = 'abcdefghijklmnopqrstuvwxyz'
count = 0
newCode = ''
for i in string:
if i == '':
count = 0
newCode += i
else:
if count > (len(key)-1):
count = 0
value1 = letters.index(i) + 1
value2 = letters.index(key[count]) + 1
num = value1 + value2
if num > 26:
num -= 26
newCode += letters[num-1]
count += 1
return newCode
And I'm getting the following error:
File "C:\Users\xxxxx\Desktop\funciones.py", line 29, in cipherWithKey
value1 = letters.index(i) + 1
ValueError: substring not found
I know what causes this error, but I'm not sure of why I'm getting it in this case.
well its clear , in your input 'tarea de' you are passing space in your input ( string varible) which is not in letters variable and it fails in that line.
you can add the possible characters to your letters variable like :
letters = 'abcdefghijklmnopqrstuvwxyz '
alternatively you can use string module to provide the full list of whitespaces
import string as st
letters = st.ascii_lowercase + st.whitespace

Python Vigenere Cipher Encrypt method not encrypting properly

The encrypt method in my program is not encrypting correctly. I thought I figured out why using debug mode; it's because it reads the spaces between words as something it has to encrypt. So I tried typing a message without spaces but it still didn't come out correctly.
I figure the issue is the if statement with the key. I tried commenting lines out, changing statements, changing the if statement to a for loop, but it still isn't correct.
def main():
vig_square = create_vig_square()
message = input("Enter a multi-word message with punctuation: ")
input_key = input("Enter a single word key with no punctuation: ")
msg = message.lower()
key = input_key.lower()
coded_msg = encrypt(msg, key, vig_square)
print("The encoded message is: ",coded_msg)
print("The decoded message is: ", msg)
def encrypt(msg,key,vig_square):
coded_msg = ""
key_inc = 0
for i in range(len(msg)):
msg_char = msg[i]
if key_inc == len(key)-1:
key_inc = 0
key_char = key[key_inc]
if msg_char.isalpha() and key_char.isalpha():
row_index = get_row_index(key_char,vig_square)
col_index = get_col_index(msg_char,vig_square)
coded_msg = coded_msg+vig_square[row_index][col_index]
else:
coded_msg = coded_msg + " "
key_inc = key_inc+1
return coded_msg
def get_col_index(msg_char, vig_square):
column_index = ord(msg_char) - 97
return column_index
def get_row_index(key_char, vig_square):
row_index = ord(key_char) - 97
return row_index
def create_vig_square():
vig_square = list()
for row in range(26):
next_row = list()
chr_code = ord('a') + row
for col in range(26):
letter = chr(chr_code)
next_row.append(letter)
chr_code = chr_code + 1
if chr_code > 122:
chr_code = ord('a')
vig_square.append(next_row)
return vig_square
main()
This example was given to us:
Enter a multi-word message with punctuation: The eagle has landed.
Enter a single word key with no punctuation: LINKED
The encoded message is: epr oejwm ukw olvqoh.
The decoded message is: the eagle has landed.
But my encoded message comes out as:
epr iloyo sif plvqoh
You have two errors:
First, you don't use all characters in the key. Change the following line:
if key_inc == len(key)-1:
key_inc = 0
to
if key_inc == len(key):
key_inc = 0
Second, you move the key pointer even if you process a non-alpha character in the message (e.g. spaces). Do it only if you encode a character, i.e. make the following change:
if msg_char.isalpha() and key_char.isalpha():
...
key_inc = key_inc+1 # Move this line here
else:
...

Interactive rot13 cipher

I have a code and I'm having trouble making it interactive.
Here's the problem:
"""Write a function called rot13 that uses the Caesar cipher to encrypt a message. The Caesar cipher works like a substitution cipher but each character is replaced by the character 13 characters to “its right” in the alphabet. So for example the letter “a” becomes the letter “n”. If a letter is past the middle of the alphabet then the counting wraps around to the letter “a” again, so “n” becomes “a”, “o” becomes “b” and so on. Hint: Whenever you talk about things wrapping around its a good idea to think of modulo arithmetic (using the remainder operator)."""
Here's the code to this problem:
def rot13(mess):
alphabet = 'abcdefghijklmnopqrstuvwxyz'
encrypted = ''
for char in mess:
if char == ' ':
encrypted = encrypted + ' '
else:
rotated_index = alphabet.index(char) + 13
if rotated_index < 26:
encrypted = encrypted + alphabet[rotated_index]
else:
encrypted = encrypted + alphabet[rotated_index % 26]
return encrypted
def main():
print(rot13('abcde'))
print(rot13('nopqr'))
print(rot13(rot13('since rot thirteen is symmetric you should see this message')))
if __name__ == "__main__":
main()
I want to make it interactive where you can input any message and you can rotate the letters however many times as you want. Here is my attempt. I understand you'd need two parameters to pass, but I'm clueless as to how to replace a few items.
Here's my attempt:
def rot13(mess, char):
alphabet = 'abcdefghijklmnopqrstuvwxyz'
encrypted = ''
for char in mess:
if char == ' ':
encrypted = encrypted + ' '
else:
rotated_index = alphabet.index(char) + mess
if rotated_index < 26:
encrypted = encrypted + alphabet[rotated_index]
else:
encrypted = encrypted + alphabet[rotated_index % 26]
return encrypted
def main():
messy_shit = input("Rotate by: ")
the_message = input("Type a message")
print(rot13(the_message, messy_shit))
if __name__ == "__main__":
main()
I don't know where my input should be taking place in the function. I have a feeling it could be encrypted?
This is probably what you're looking for. It rotates the message by the messy_shit input.
def rot13(mess, rotate_by):
alphabet = 'abcdefghijklmnopqrstuvwxyz'
encrypted = ''
for char in mess:
if char == ' ':
encrypted = encrypted + ' '
else:
rotated_index = alphabet.index(char) + int(rotate_by)
if rotated_index < 26:
encrypted = encrypted + alphabet[rotated_index]
else:
encrypted = encrypted + alphabet[rotated_index % 26]
return encrypted
def main():
messy_shit = input("Rotate by: ")
the_message = input("Type a message")
print(rot13(the_message, messy_shit))
def rot(message, rotate_by):
'''
Creates a string as the result of rotating the given string
by the given number of characters to rotate by
Args:
message: a string to be rotated by rotate_by characters
rotate_by: a non-negative integer that represents the number
of characters to rotate message by
Returns:
A string representing the given string (message) rotated by
(rotate_by) characters. For example:
rot('hello', 13) returns 'uryyb'
'''
assert isinstance(rotate_by, int) == True
assert (rotate_by >= 0) == True
alphabet = 'abcdefghijklmnopqrstuvwxyz'
rotated_message = []
for char in message:
if char == ' ':
rotated_message.append(char)
else:
rotated_index = alphabet.index(char) + rotate_by
if rotated_index < 26:
rotated_message.append(alphabet[rotated_index])
else:
rotated_message.append(alphabet[rotated_index % 26])
return ''.join(rotated_message)
if __name__ == '__main__':
while True:
# Get user input necessary for executing the rot function
message = input("Enter message here: ")
rotate_by = input("Enter your rotation number: ")
# Ensure the user's input is valid
if not rotate_by.isdigit():
print("Invalid! Expected a non-negative integer to rotate by")
continue
rotated_message = rot(message, int(rotate_by))
print("rot-ified message:", rotated_message)
# Allow the user to decide if they want to continue
run_again = input("Continue? [y/n]: ").lower()
while run_again != 'y' and run_again != 'n':
print("Invalid! Expected 'y' or 'n'")
run_again = input("Continue? [y/n]: ")
if run_again == 'n':
break
Note: It's more efficient to create a list of characters then join them to produce a string instead of using string = string + char. See Method 6 here and the Python docs here. Also, be aware that our rot function only works for lowercase letters of the alphabet. It'll break if you try to rot a message with uppercase characters or any character that isn't in alphabet.

Combining string an integers for the Vigenere cipher in python?

I am trying to write a vigenere cipher encrypter in python. I am getting another error...
def vigenere(string,key):
for i in range(len(key)):
if key[i].isupper():
tempList = list(key)
tempList[i] = chr(ord(key[i])-65)
key = "".join(tempList)
elif key[i].islower():
tempList = list(key)
tempList[i] = chr(ord(key[i])-97)
key = "".join(tempList)
k = 0
newstring = ''
for i in string:
if i.isupper():
newstring = newstring + ((ord(i)-65)+(key[k % len(key)]))%26 + 65
elif i.islower():
newstring = newstring + ((ord(i)-97)+(key[k % len(key)]))%26 + 97
k = k + 1
return newstring
"unsupported operand type(s) for +: 'int' and 'str'" -- any help?
First, you need to change:
key[i] + ord(key[i])-97
To:
key[i] = ord(key[i])-97
It seems that is a mistyping.
Second, the ord(...) function returns an int. You want to convert it back to a char using chr(...):
key[i] = chr(ord(key[i])-97)
Finally, in Python, strings are immutable. This means you can't change the individual char of a string. This is an easy way to do it:
if key[i].isupper():
tempList = list(key)
tempList[i] = chr(ord(key[i])-65)
key = "".join(tempList)
elif key[i].islower():
tempList = list(key)
tempList[i] = chr(ord(key[i])-97)
key = "".join(tempList)

Categories