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

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

Related

Don't know how to decode from double pair "ab" to "a"

I've been trying how to decode my encryption from "kl" to "a", or "klfg" to "ab"
I have tried str.find(), str.replace() and many other options, but I couldn't get any good result
My code:
print ("Welcome")
message = input("Entry text")
encoded= ""
for char in message:
if char== "a" or char == "A":
encoded += "kl"
elif char== "b" or char == "B":
encoded += "fg"
elif char== "z" or char == "Z":
encoded += "wt"
print (encoded)
You can get rid of the "if..." by building dictionaries to handle the mapping between unencoded and encoded versions of the string. Then its just a question of building the dictionary keys. When encoding, lower and upper case map to the same encoded value. When decoding, grab 2 characters at a time.
encoding = (("A", "kl"), ("B","fg"), ("C","wt"))
encodemap = {frm:to for frm,to in encoding}
decodemap = {to:frm for frm,to in encoding}
def encoder(message):
return "".join(encodemap.get(c.upper(), "??") for c in message)
def decoder(encrypted):
return "".join(decodemap.get(encrypted[i:i+2], "?")
for i in range(0, len(encrypted), 2))
e = encoder("abcABC")
print(e)
d = decoder(e)
print(d)
So if I'm reading your question correct, you're looking to decode an encrypted message like for example "klklfg", which is what you get if you input aab into your code.
If you're then looking to decode that message you can do so using the enumerate function which gives you the index as well as the character in a string
decoded = ''
for i, char in enumerate(encoded):
if char == 'k' and encoded[i + 1] == 'l':
decoded += 'a'
if char == 'f' and encoded[i + 1] == 'g':
decoded += 'b'
print(decoded)
Pasting this code after your code will decrypt your encrypted message! Again, I'm not exactly sure if that was your question but here's my answer anyways.
Not very efficient, but I think it will solve your problem:
print ("Welcome")
decrypted = ["a", "b", "z"]
encrypted = ["kl", "fg", "wt"]
encryptor = dict(zip(decrypted, encrypted))
decryptor = dict(zip(encrypted, decrypted))
message = input("Entry text:\n")
encoded = ""
for char in message:
encoded += encryptor.get(char, "?")
print("Encoded:", encoded)
decoded = ""
start = 0
end = 1
while start < end and start < len(encoded):
while end <= len(encoded):
substr = encoded[start:end]
if substr in encrypted:
decoded += decryptor.get(substr, "?")
start = end
end = start + 1
else:
end += 1
print("Decoded:", decoded)

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:
...

Equivalent strings don't give equivalent bitstrings in Python

I'm currently making an encryption program for an assignment however I cannot decrypt. The cause of this is that the key is a string made from a randomly generated bitstring however when turning the key back into a bitstring I get a different bitstring. I realized this after checking and finding out that the bitstring after turning the key back to binary is shorter than the bitstring used to make the key.
##imports##################################################
from socket import *
import random
##functions################################################
def CCipher(message, k):
output = ""
for x in message:
i = ord(x)
i = (i+k)%128
output += chr(i)
return output
def toBinary(message):
output = ""
for x in message:
i = bin(ord(x))[2:]
output += i
return output
def XOR(bitstring1, bitstring2):
output = ""
if(len(bitstring1) != len(bitstring2)):
print("Use bitstrings of the same length")
return None
for x in range(len(bitstring1)):
if(bitstring1[x] == "1" and bitstring2[x] == "0"):
output += "1"
elif(bitstring1[x] == "0" and bitstring2[x] == "1"):
output += "1"
else:
output += "0"
return output
def randomBinary(k):
output = ""
for x in range(k):
i = random.randint(0,1)
output = output + str(i)
return output
def toString(message):
output = ""
i = ""
n = 0
for x in message:
n += 1
i += x
if(n == 7):
output += chr(int(i,2))
n = 0
i = ""
return output
##server stuff#########################################
serverName = "OmariPC"
serverPort = 12347
clientSocket = socket(AF_INET,SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
##files################################################
nowar = open("NoWar.dat",'r')
trump = open("Trump.dat","w")
##encryption###########################################
message = nowar.read()
mesBin = toBinary(message)
bkey = randomBinary(len(mesBin))
encrypted = XOR(mesBin, bkey)
encrypted = toString(encrypted)
key = toString(bkey)
trump.write(encrypted)
trump.close()
enKey = CCipher(key, 4)
##sending###############################################
clientSocket.send(enKey.encode())
print(len(enKey))
clientSocket.send(encrypted.encode())
print(len(encrypted))
##testing###############################################
if key == toString(bkey):
print(True)
else:
print(False)
if len(toBinary(key)) == len(bkey):
print(True)
else:
print(False)
output:
168
168
True
False
def toBinary(message):
output = ""
for x in message:
i = bin(ord(x))[2:]
output += i
return output
bin(ord(x)) generates strings of variable length. For example:
>>> bin(ord(' '))
0b100000
>>> bin(ord('a'))
0b1100001
You concatenate all of the results of bin() together, so there's no way to separate them later. Meanwhile, your toString() function reads exactly 7 "bits" at a time.
You could make bin() append chunks of a fixed-size instead:
# Pad the left with 0s to always generate a string of length 7.
# (Assumes that all input characters are ASCII.)
assert(ord(x) < 128)
bin(ord(x))[2:].rjust(7, '0')

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.

XOR cryptographic algorithm not working for > 54 chars

I recently began to play around with cryptography after playing with Project Euler Problem 59, and I made a basic XOR cryptography system. In this case the concatentation of your output and a random key are made and saved to a text file (only as a test, I will make it better once I've fixed this bug), but I've noticed that certain messages do not encrypt or decrypt correctly. I've narrowed this down to messages of length > 54. My code is as follows:
#encrypt.py
import random
msg = raw_input("Enter your message: ")
out = ""
key = ""
for i in xrange(len(msg)):
key += chr(random.randint(0,255))
k = 0
for char in msg:
out += chr(ord(msg[k]) ^ ord(key[k]))
k += 1
print "\nYour output is:", out
print "Your key is:", key
raw_input()
with open("output.txt","r+") as f:
f.truncate()
f.write(out+key)
And decryption:
#decrypt.py
import sys
if not len(sys.argv) > 1: exit()
with open(sys.argv[1],"r+") as f:
content = f.read()
msg, key = content[:len(content)/2], content[len(content)/2:]
out = ""
k = 0
for char in msg:
out += chr(ord(char) ^ ord(key[k]))
k += 1
print out
raw_input()
For longer messages (than 54 chars), when they are decrypted they will give a string of random characters. Does anyone have an idea as to why this happens?
Since making characters out of random bits is likely to produce characters that look ugly on the screen, you can try hex-encoding the output before printing it.
#encrypt.py
import random
msg = raw_input("Enter your message: ")
out = ""
key = ""
for i in xrange(len(msg)):
key += chr(random.randint(0,255))
k = 0
for char in msg:
out += chr(ord(msg[k]) ^ ord(key[k]))
k += 1
out = ":".join("{:02x}".format(ord(c)) for c in out)
key = ":".join("{:02x}".format(ord(c)) for c in key)
print "\nYour output is:", out
print "Your key is:", key
raw_input()
with open("output.txt","w+") as f:
f.truncate()
f.write(out+key)
Then for decryption, you have to remove the colons and hex-decode before you can continue with regular decryption.
#decrypt.py
import sys
if not len(sys.argv) > 1: exit()
with open(sys.argv[1],"r+") as f:
content = f.read()
content = content.replace(':', '')
msg, key = content[:len(content)/2], content[len(content)/2:]
msg = msg.decode('hex')
key = key.decode('hex')
out = ""
k = 0
for char in msg:
out += chr(ord(char) ^ ord(key[k]))
k += 1
print out
raw_input()
At the console, the results look like this:
$ python encrypt.py
Enter your message: onetwothreefourfivesixseveneightnineteneleventwelvethirteen
Your output is: 7f:09:d0:8c:2e:1a:e0:a0:84:e7:bf:3b:e8:cc:f3:4e:35:e4:4f:20:c8:00:72:9d:f0:bc:de:54:88:30:a6:3d:93:3f:c1:6d:46:4a:68:ca:96:2e:16:50:43:0f:30:fa:27:f3:f2:8d:35:4b:6a:11:cc:02:04
Your key is: 10:67:b5:f8:59:75:94:c8:f6:82:da:5d:87:b9:81:28:5c:92:2a:53:a1:78:01:f8:86:d9:b0:31:e1:57:ce:49:fd:56:af:08:32:2f:06:af:fa:4b:60:35:2d:7b:47:9f:4b:85:97:f9:5d:22:18:65:a9:67:6a
$ cat output.txt
7f:09:d0:8c:2e:1a:e0:a0:84:e7:bf:3b:e8:cc:f3:4e:35:e4:4f:20:c8:00:72:9d:f0:bc:de:54:88:30:a6:3d:93:3f:c1:6d:46:4a:68:ca:96:2e:16:50:43:0f:30:fa:27:f3:f2:8d:35:4b:6a:11:cc:02:0410:67:b5:f8:59:75:94:c8:f6:82:da:5d:87:b9:81:28:5c:92:2a:53:a1:78:01:f8:86:d9:b0:31:e1:57:ce:49:fd:56:af:08:32:2f:06:af:fa:4b:60:35:2d:7b:47:9f:4b:85:97:f9:5d:22:18:65:a9:67:6a
$ python decrypt.py output.txt
onetwothreefourfivesixseveneightnineteneleventwelvethirteen

Categories