String index not working? - Python - python

I'm Making a sort of cipher that requires a string to be read backwards and i get an indexing error even though the index I reference is well within range:
M = str(input("Input Message: "))
M = M.upper()
L = len(M)
A = ["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"]
def DECRYPT():
global L
global M
global A
if L%2 != 0:
POS = False
else:
POS = True
i = L-1
NM = ""
while 1:
if M[i] != " ":
INDEX = A.index(M[i])
if POS == True:
INDEX += (i + 1)
else:
INDEX -= (i)
INDEX %= 26
NM = NM+A[INDEX]
i += 1
if POS == True:
POS = False
else:
POS = True
print("\n"+NM)
def ENCRYPT():
global L
global A
global M
POS = True
M = M[::-1]
i = 0
NM = ""
while 1:
if i == L:
break
if M[i] != " ":
INDEX = A.index(M[i])
if POS == True:
INDEX += (i + 1)
else:
INDEX -= (i + 1)
INDEX %= 26
NM = NM+A[INDEX]
i += 1
if POS == True:
POS = False
else:
POS = True
print("\n"+NM)
while 1:
C = int(input("\nWhat do you want to do:\n1) Encrypt Something\n2)Decrypt Something\n\n"))
if C == 1:
ENCRYPT()
if C == 2:
DECRYPT()
where i is a placeholder value. I run it and get this:
Input Message: ABC
What do you want to do:
1) Encrypt Something
2)Decrypt Something
2
Traceback (most recent call last):
File "C:\Users\Danny\Google Drive\SHIFT.py", line 67, in <module>
DECRYPT()
File "C:\Users\Danny\Google Drive\SHIFT.py", line 19, in DECRYPT
if M[i] != " ":
IndexError: string index out of range
I have tried changing value of i to no avail.

There are multiple problems with your code: as fernand0 noted, your index runs the wrong way; your inversion of POS happens at different levels in the code, the encryptor does it on every character, the decryptor does it on every letter -- they should work the same; five of your six global declarations aren't needed; you don't deal with word breaks correctly so the decryption won't match the original; once you encrypt, there's no way to decrypt in the same session as the local NM doesn't feedback into the global M.
Below is my rework of your code addressing the above problems and some style issues. The key phrase here is 'simplify'. I've kept your odd uppercase variable names but expanded them from single characters to what they represent:
MESSAGE = input("Input Message: ").upper()
ALPHABET = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
ALPHABET_LENGTH = len(ALPHABET)
def DECRYPT(MESSAGE):
LENGTH = len(MESSAGE)
IS_NEGATIVE = True
NEW_MESSAGE = ""
for I in range(LENGTH):
if MESSAGE[I] in ALPHABET:
INDEX = ALPHABET.index(MESSAGE[I])
if IS_NEGATIVE:
INDEX -= (I + 1)
else:
INDEX += (I + 1)
INDEX %= ALPHABET_LENGTH
NEW_MESSAGE += ALPHABET[INDEX]
IS_NEGATIVE = not IS_NEGATIVE
else:
NEW_MESSAGE += MESSAGE[I]
return NEW_MESSAGE[::-1]
def ENCRYPT(MESSAGE):
MESSAGE = MESSAGE[::-1]
LENGTH = len(MESSAGE)
IS_POSITIVE = True
NEW_MESSAGE = ""
for I in range(LENGTH):
if MESSAGE[I] in ALPHABET:
INDEX = ALPHABET.index(MESSAGE[I])
if IS_POSITIVE:
INDEX += (I + 1)
else:
INDEX -= (I + 1)
INDEX %= ALPHABET_LENGTH
NEW_MESSAGE += ALPHABET[INDEX]
IS_POSITIVE = not IS_POSITIVE
else:
NEW_MESSAGE += MESSAGE[I]
return NEW_MESSAGE
while True:
print("\nWhat do you want to do:")
print("1) Encrypt Message")
print("2) Decrypt Message")
CHOICE = int(input("\n"))
if CHOICE == 1:
MESSAGE = ENCRYPT(MESSAGE)
if CHOICE == 2:
MESSAGE = DECRYPT(MESSAGE)
print("\n" + MESSAGE)
TEST
> python3 file.py
Input Message: An opportunity to teach is an opportunity to learn
What do you want to do:
1) Encrypt Message
2) Decrypt Message
1
OPDAQ HB OEWAGIBFXIU JD RI JZEZZ GC NFVBFJAGWJT KC
What do you want to do:
1) Encrypt Message
2) Decrypt Message
2
AN OPPORTUNITY TO TEACH IS AN OPPORTUNITY TO LEARN
What do you want to do:
1) Encrypt Message
2) Decrypt Message

You are starting i at the end of the string and then, you are increasing it. I think the line i += 1 should be i -= 1

Related

Why does Python 3 for loop output and behave differently?

This is a password generator, I couldn't really determine where the problem is, but from the output, I could say it's around turnFromAlphabet()
The function turnFromAlphabet() converts an alphabetical character to its integer value.
The random module, I think doesn't do anything here as it just decides whether to convert a character in a string to uppercase or lowercase. And if a string is in either, when sent or passed to turnFromAlphabet() it is converted to lowercase first to avoid errors but there are still errors.
CODE:
import random
import re
#variables
username = "oogisjab" #i defined it already for question purposes
index = 0
upperOrLower = []
finalRes = []
index2a = 0
#make decisions
for x in range(len(username)):
decision = random.randint(0,1)
if(decision is 0):
upperOrLower.append(True)
else:
upperOrLower.append(False)
#Apply decisions
for i in range(len(username)):
if(upperOrLower[index]):
finalRes.append(username[index].lower())
else:
finalRes.append(username[index].upper())
index+=1
s = ""
#lowkey final
s = s.join(finalRes)
#reset index to 0
index = 0
def enc(that):
if(that is "a"):
return "#"
elif(that is "A"):
return "4"
elif(that is "O"):
return "0" #zero
elif(that is " "):
# reduce oof hackedt
decision2 = random.randint(0,1)
if(decision2 is 0):
return "!"
else:
return "_"
elif(that is "E"):
return "3"
else:
return that
secondVal = []
for y in range(len(s)):
secondVal.append(enc(s[index]))
index += 1
def turnFromAlphabet(that, index2a):
alp = "abcdefghijklmnopqrstuvwxyz"
alp2 = list(alp)
for x in alp2:
if(str(that.lower()) == str(x)):
return index2a+1
break
else:
index2a += 1
else:
return "Error: Input is not in the alphabet"
#real final
finalOutput = "".join(secondVal)
#calculate some numbers and chars from a substring
amount = len(finalOutput) - round(len(finalOutput)/3)
getSubstr = finalOutput[-(amount):]
index = 0
allFactors = {
};
#loop from substring
for x in range(len(getSubstr)):
hrhe = re.sub(r'\d', 'a', ''.join(e for e in getSubstr[index] if e.isalnum())).replace(" ", "a").lower()
print(hrhe)
#print(str(turnFromAlphabet("a", 0)) + "demo")
alpInt = turnFromAlphabet(hrhe, 0)
print(alpInt)
#get factors
oneDimensionFactors = []
for p in range(2,alpInt):
# if mod 0
if(alpInt % p) is 0:
oneDimensionFactors.append(p)
else:
oneDimensionFactors.append(1)
indexP = 0
for z in oneDimensionFactors:
allFactors.setdefault("index{0}".format(index), {})["keyNumber"+str(p)] = z
index+=1
print(allFactors)
I think that you are getting the message "Error: input is not in the alphabet" because your enc() change some of your characters. But the characters they becomes (for example '#', '4' or '!') are not in your alp variable defined in turnFromAlphabet(). I don't know how you want to fix that. It's up to you.
But I have to say to your code is difficult to understand which may explain why it can be difficult for you to debug or why others may be reluctant to help you. I tried to make sense of your code by removing code that don't have any impact. But even in the end I'm not sure I understood what you tried to do. Here's what I understood of your code:
import random
import re
#username = "oogi esjabjbb"
username = "oogisjab" #i defined it already for question purposes
def transform_case(character):
character_cases = ('upper', 'lower')
character_to_return = character.upper() if random.choice(character_cases) == 'upper' else character.lower()
return character_to_return
username_character_cases_modified = "".join(transform_case(current_character) for current_character in username)
def encode(character_to_encode):
translation_table = {
'a' : '#',
'A' : '4',
'O' : '0',
'E' : '3',
}
character_translated = translation_table.get(character_to_encode, None)
if character_translated is None:
character_translated = character_to_encode
if character_translated == ' ':
character_translated = '!' if random.choice((True, False)) else '_'
return character_translated
final_output = "".join(encode(current_character) for current_character in username_character_cases_modified)
amount = round(len(final_output) / 3)
part_of_final_output = final_output[amount:]
all_factors = {}
for (index, current_character) in enumerate(part_of_final_output):
hrhe = current_character
if not hrhe.isalnum():
continue
hrhe = re.sub(r'\d', 'a', hrhe)
hrhe = hrhe.lower()
print(hrhe)
def find_in_alphabet(character, offset):
alphabet = "abcdefghijklmnopqrstuvwxyz"
place_found = alphabet.find(character)
if place_found == -1 or not character:
raise ValueError("Input is not in the alphabet")
else:
place_to_return = place_found + offset + 1
return place_to_return
place_in_alphabet = find_in_alphabet(hrhe, 0)
print(place_in_alphabet)
def provide_factors(factors_of):
for x in range(1, int(place_in_alphabet ** 0.5) + 1):
(quotient, remainder) = divmod(factors_of, x)
if remainder == 0:
for current_quotient in (quotient, x):
yield current_quotient
unique_factors = set(provide_factors(place_in_alphabet))
factors = sorted(unique_factors)
all_factors.setdefault(f'index{index}', dict())[f'keyNumber{place_in_alphabet}'] = factors
print(all_factors)
Is near what your wanted to do?

Python: List index is out of range

I am making some 'password-cracking' software for fun with python. (Not for actual password cracking, for a randomly generated password) However when I run it, it says this:
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\Password Cracker.py", line 14, in <module>
if chars[trys] == example[index]:
IndexError: list index out of range
I don't know what the error is here, because this happens as soon as I run the code, and by default, index is set to 0, so how can it be out of range?
Here is my code:
import random
import string
chars = list(string.ascii_lowercase) + list(string.ascii_lowercase.upper())
print(chars)
trys = 0
rounds = 8
index = 0
example = (random.choice(chars) + random.choice(chars) + random.choice(chars) + random.choice(chars) + random.choice(chars) + random.choice(chars) + random.choice(chars) + random.choice(chars))
password = ""
while rounds != 0:
if chars[trys] == example[index]:
password = chars[trys]
rounds -= 1
index +=1
else:
trys += 1
print(password)
What is my mistake?
Increment index and decrements rounds should not be inside if block. This should be irrespective on if or else case.
You can try:
while rounds != 0:
if chars[trys] == example[index]:
password = chars[trys]
else:
trys += 1
rounds -= 1
index +=1
print(password)

Script stops executing once it encounters def line

So, I had 2 scripts, a Vigenere cipher and a Caesar cipher, however, when I decided to merge them into a "mainproject" file by using "import" to import the script after the wanted answer from the user is sent on the Terminal page, when I've done the code and decided to execute the mainproject, I've got the question whether to choose to use Vigenere or Caesar, once I typed caesar (1) it played the first 2-4 lines and stopped the code once I've encountered the "def" line on both of scripts, if thats the problem, I can't use imports with "def", how can I merge them both into one file that asks which script I would like to use?
Caesar:
import time
import sys
print("Welcome to Caesar Cipher")
time.sleep(3)
print("Choose the corresponding number to Encrypt or Decrypt in Caesar Cipher")
def encryption():
print("Encryption")
print("You have chose ENCRYPTION")
msg = input("Enter message: ")
key = int(input("Enter key(0-25): "))
encrypted_text = ""
for i in range(len(msg)):
if ord(msg[i]) == 32:
encrypted_text += chr(ord(msg[i]))
elif ord(msg[i]) + key > 122:
temp = (ord(msg[i]) + key) - 122
encrypted_text += chr(96+temp)
elif (ord(msg[i]) + key > 90) and (ord(msg[i]) <= 96):
temp = (ord(msg[i]) + key) - 90
encrypted_text += chr(64+temp)
else:
encrypted_text += chr(ord(msg[i]) + key)
print("Your Encrypted Message: " + encrypted_text)
def decryption():
print("Decryption")
print("You have chose DECRYPTION")
encrp_msg = input("Enter encrypted Text: ")
decrp_key = int(input("Enter key(0-25): "))
decrypted_text = ""
for i in range(len(encrp_msg)):
if ord(encrp_msg[i]) == 32:
decrypted_text += chr(ord(encrp_msg[i]))
elif ((ord(encrp_msg[i]) - decrp_key) < 97) and ((ord(encrp_msg[i]) - decrp_key) > 90):
temp = (ord(encrp_msg[i]) - decrp_key) + 26
decrypted_text += chr(temp)
elif (ord(encrp_msg[i]) - decrp_key) < 65:
temp = (ord(encrp_msg[i]) - decrp_key) + 26
decrypted_text += chr(temp)
else:
decrypted_text += chr(ord(encrp_msg[i]) - decrp_key)
print("Text has been Decrypted: " + decrypted_text)
choice = int(input("1. Encryption\n2. Decryption\nChoose(1,2): "))
if choice == 1:
encryption()
elif choice == 2:
decryption()
else:
print("Wrong Choice")
Vigenere:
import time
print("Welcome to Vigenere Cipher")
time.sleep(2)
print("Choose the corresponding number to Encrypt or Decrypt in Vigenere Cipher")
time.sleep(2.5)
def msg_and_key():
msg = input("Enter message: ").upper()
key = input("Enter key: ").upper()
key_map = ""
j=0
for i in range(len(msg)):
if ord(msg[i]) == 32:
key_map += " "
else:
if j < len(key):
key_map += key[j]
j += 1
else:
j = 0
key_map += key[j]
j += 1
return msg, key_map
def create_vigenere_table():
table = []
for i in range(26):
table.append([])
for row in range(26):
for column in range(26):
if (row + 65) + column > 90:
table[row].append(chr((row+65) + column - 26))
else:
table[row].append(chr((row+65)+column))
return table
def cipher_encryption(message, mapped_key):
table = create_vigenere_table()
encrypted_text = ""
for i in range(len(message)):
if message[i] == chr(32):
encrypted_text += " "
else:
row = ord(message[i])-65
column = ord(mapped_key[i]) - 65
encrypted_text += table[row][column]
print("Encrypted Message: {}".format(encrypted_text))
def itr_count(mapped_key, message):
counter = 0
result = ""
for i in range(26):
if mapped_key + i > 90:
result += chr(mapped_key+(i-26))
else:
result += chr(mapped_key+i)
for i in range(len(result)):
if result[i] == chr(message):
break
else:
counter += 1
return counter
def cipher_decryption(message, mapped_key):
table = create_vigenere_table()
decrypted_text = ""
for i in range(len(message)):
if message[i] == chr(32):
decrypted_text += " "
else:
decrypted_text += chr(65 + itr_count(ord(mapped_key[i]), ord(message[i])))
print("Decrypted Message: {}".format(decrypted_text))
print("Key and Message can only be alphabetic")
time.sleep(1.5)
choice = int(input("1. Encryption\n2. Decryption\nChoose(1,2): "))
if choice == 1:
print("You have chose ENCRYPTION")
message, mapped_key = msg_and_key()
cipher_encryption(message, mapped_key)
elif choice == 2:
print("You have chose DECRYPTION")
message, mapped_key = msg_and_key()
cipher_decryption(message, mapped_key)
else:
print("Wrong choice")
Any help would be appreciated!
def only defines a function. To actually execute it you need to call it.
It seems the following part should be outside of decryption function body i.e. indented to the left. It:
call be executed directly on the module level
can be a separate function e.g. main that you can call
As main:
def main():
choice = int(input("1. Encryption\n2. Decryption\nChoose(1,2): "))
if choice == 1:
encryption()
elif choice == 2:
decryption()
else:
print("Wrong Choice")
And now you can call the main function:
main()
Welcome to Stackoverflow. This answer ended up more as general advice than a specific point solution to your problem, but I hope it's helpful.
One obvious remark is that it would be nice not to have to import the code for the cypher you don't intend to use.
Imports are executable statements, so this is quite practical.
Your code is far from optimally organised. Much of the code on the two files is the same or very similar. This is not unusual for new programmers, as it takes a while to learn how to break problems down and extract the common elements. The real problem is that while it defines lots of functions, none of them actually get called (unless there's more code you haven't included).
I would recommend that you modify your code so that there's a top-level program that collects the key and data and performs the required operations, along with your two
encryption/decryption modules. The encryption and decryption routines shouldn't do any input or output at all, just deal with the data.
Let's assume you have a variable mtype that holds the type of encryption you want to use. The top-level logic could look something like this:
if mtype == "caesar":
from caesar import encrypt, decrypt
elif mtype == "vigniere":
from vigniere import encrypt, decrypt
else:
sys.exit("Unrecognised message type")
If this code succeeds you would then have an encrypt and a decrypt function from the correct module. This means that they have to have the same interface, since your logic must now be written to accommodate either.
The remainder of your logic would look something like this (untested):
operation = input("E(ncrypt) or D(ecrypt)? ")
if operation.upper().startswith("E"):
function = encrypt
elif operation.upper().startswith("D"):
function = decrypt
else:
sys.exit("Unknown operation")
message = input(...)
key = input(...)
output = function(message, key)
This will hopefully make your code clearer by separating out the I/O responsibility
from the encryption and decryption tasks.

"List index out of range" error in python 3 file

I have a homemade text encryption script which outputs a "List index out of range" error when I input "Hey dude, how are you doing?" as a text to encrypt and "KFC" as an encryption key.
I get this more precisely:
Traceback (most recent call last):
File "/Users/BCPianist/Google Drive/Project-Cryptonite/NCrypt.py", line 75, in <module>
main(userIn, a)
File "/Users/BCPianist/Google Drive/Project-Cryptonite/NCrypt.py", line 47, in main
currentscrtrank += 1 + scrtlst[scrtrankcounter]
IndexError: list index out of range
I have two files used in this script (the main script and a function library). I just can't figure where this error is coming from...
Here is my main script:
import random
from cryptolib import *
# !/usr/bin/env python
# -*- coding: UTF-8 -*-
# enable debugging
print("Content-Type: text/plain;charset=utf-8") # Definit le code d'ecriture
print() # Insere une ligne d'espacement.
def main(userin, numberedsecretkey): # Fonction principale (le gros de l'encryption)
"""
This is my main.
>>> main("Allo", "Hey")
10842839726
"""
comments = True # Definit si les commentaires lors de l'impression sont actif ou non (False les desactive)
total = convdecimal(userin) # Time to deal with the encryption key:
scrtkeytotal = convdecimal(numberedsecretkey)
# converting numbered message to list
msglst = [int(elem) for elem in str(total)]
if comments == True:
printdebug("The initial numbered message is:%s " % msglst)
# converting numbered key to list
scrtlst = [int(thingy) for thingy in str(scrtkeytotal)]
if not comments != True:
printdebug("The initial encryption key is:%s" % scrtlst)
# Attempting Encryption
scrtrankcounter = 0
currentscrtrank = scrtlst[scrtrankcounter]
while currentscrtrank < len(msglst):
randomgen = random.randint(0, 9)
msglst.insert(currentscrtrank, randomgen)
if comments:
printdebug(str(randomgen) + " was inserted at rank: " + str(
scrtrankcounter) + ", therefore, at index position: " + str(currentscrtrank) + ".")
printdebug("List now appears as: " + str(msglst))
scrtrankcounter += 1
if scrtrankcounter > len(scrtlst):
scrtrankcounter = 0
currentscrtrank += 1 + scrtlst[scrtrankcounter]
return listtoint(msglst)
def convdecimal(userin):
rank = len(userin)
total = 0
for character in userin:
rank -= 1 # decreasing the letter rank by one
letter = ord(character)
if letter < 32:
pass
elif letter == 27:
pass
else:
rankmult = 255 ** rank # Making a multiplier
lettervalue = letter * rankmult # Multiplying the letter by this multiplier
total += lettervalue # Adding the letter's value to the total
return total
if __name__ == "__main__":
userIn = input("Enter the word/Sentence you want to encrypt:")
a = input("Enter a password with which you would like to encrypt your message:")
print(userIn)
main(userIn, a)
And here is my function library file:
import os
DEBUG = True
def printdebug(log: object) -> object:
if DEBUG:
print(log)
def loading(sec=10):
start = 1
input("Press Enter to continue")
printdebug("loading...")
while start <= sec:
printdebug("...")
start += 1
def codepause():
os.system("Pause")
input("press enter to continue")
def listtoint(msglst):
# Conversion from list to int
"""
>>> listtoint([1,2,3,4,5,6,7,8,9,0])
1234567890
"""
ncryptedkey = 0
base = 10
for d in msglst:
ncryptedkey = base * ncryptedkey + d
# printDebugJob:
printdebug("The encrypted message is:" + str(ncryptedkey))
return ncryptedkey
def convdecimal(userin):
rank = len(userin)
total = 0
for character in userin:
rank -= 1 # decreasing the letter rank by one
letter = ord(character)
if letter < 32:
pass
elif letter == 27:
pass
else:
rankmult = 255 ** rank # Making a multiplier
lettervalue = letter * rankmult # Multiplying the letter by this multiplier
total += lettervalue # Adding the letter's value to the total
def letterprint(nb):
nb = chr(nb)
print(nb, end='')
Thanks Already!
I've not tested this, but it looks like you need to swap the greater than sign for greater than or equals, as it's probably going 1 index too high.
if scrtrankcounter >= len(scrtlst):
scrtrankcounter = 0
currentscrtrank += 1 + scrtlst[scrtrankcounter]
For example, if scrtlst has a length of 5, the highest index is 4, so it'll give an error if you try scrtlst[5].

Caesar Cipher Function in Python

I'm trying to create a simple Caesar Cipher function in Python that shifts letters based on input from the user and creates a final, new string at the end. The only problem is that the final cipher text shows only the last shifted character, not an entire string with all the shifted characters.
Here's my code:
plainText = raw_input("What is your plaintext? ")
shift = int(raw_input("What is your shift? "))
def caesar(plainText, shift):
for ch in plainText:
if ch.isalpha():
stayInAlphabet = ord(ch) + shift
if stayInAlphabet > ord('z'):
stayInAlphabet -= 26
finalLetter = chr(stayInAlphabet)
cipherText = ""
cipherText += finalLetter
print "Your ciphertext is: ", cipherText
return cipherText
caesar(plainText, shift)
I realize that this answer doesn't really answer your question, but I think it's helpful anyway. Here's an alternative way to implementing the caesar cipher with string methods:
def caesar(plaintext, shift):
alphabet = string.ascii_lowercase
shifted_alphabet = alphabet[shift:] + alphabet[:shift]
table = string.maketrans(alphabet, shifted_alphabet)
return plaintext.translate(table)
In fact, since string methods are implemented in C, we will see an increase in performance with this version. This is what I would consider the 'pythonic' way of doing this.
You need to move cipherText = "" before the start of the for loop. You're resetting it each time through the loop.
def caesar(plainText, shift):
cipherText = ""
for ch in plainText:
if ch.isalpha():
stayInAlphabet = ord(ch) + shift
if stayInAlphabet > ord('z'):
stayInAlphabet -= 26
finalLetter = chr(stayInAlphabet)
cipherText += finalLetter
print "Your ciphertext is: ", cipherText
return cipherText
This is an improved version of the code in the answer of #amillerrhodes that works with different alphabets, not just lowercase:
def caesar(text, step, alphabets):
def shift(alphabet):
return alphabet[step:] + alphabet[:step]
shifted_alphabets = tuple(map(shift, alphabets))
joined_aphabets = ''.join(alphabets)
joined_shifted_alphabets = ''.join(shifted_alphabets)
table = str.maketrans(joined_aphabets, joined_shifted_alphabets)
return text.translate(table)
Example of usage:
>>> import string
>>> alphabets = (string.ascii_lowercase, string.ascii_uppercase, string.digits)
>>> caesar('Abc-xyZ.012:789?жñç', step=4, alphabets=alphabets)
'Efg-bcD.456:123?жñç'
References:
Docs on str.maketrans.
Docs on str.translate.
Docs on the string library
Using some ascii number tricks:
# See http://ascii.cl/
upper = {ascii:chr(ascii) for ascii in range(65,91)}
lower = {ascii:chr(ascii) for ascii in range(97,123)}
digit = {ascii:chr(ascii) for ascii in range(48,58)}
def ceasar(s, k):
for c in s:
o = ord(c)
# Do not change symbols and digits
if (o not in upper and o not in lower) or o in digit:
yield o
else:
# If it's in the upper case and
# that the rotation is within the uppercase
if o in upper and o + k % 26 in upper:
yield o + k % 26
# If it's in the lower case and
# that the rotation is within the lowercase
elif o in lower and o + k % 26 in lower:
yield o + k % 26
# Otherwise move back 26 spaces after rotation.
else: # alphabet.
yield o + k % 26 -26
x = (''.join(map(chr, ceasar(s, k))))
print (x)
Batteries included
while 1:
phrase = raw_input("Could you please give me a phrase to encrypt?\n")
if phrase == "" : break
print "Here it is your phrase, encrypted:"
print phrase.encode("rot_13")
print "Have a nice afternoon!"
https://docs.python.org/2/library/codecs.html#python-specific-encodings
Python 3 update
The fine docs say
[Now the rot_13] codec provides a text transform: a str to str mapping. It is not supported by str.encode() (which only produces bytes output).
Or, in other words, you have to import encode from the codecs module and use it with the string to be encoded as its first argument
from codecs import decode
...
print(encode(phrase, 'rot13'))
The problem is that you set cipherText to empty string at every cycle iteration, the line
cipherText = ""
must be moved before the loop.
As pointed by others, you were resetting the cipherText in the iteration of the for loop. Placing cipherText before the start of the for loop will solve your problem.
Additionally, there is an alternate approach to solving this problem using Python's Standard library. The Python Standard Library defines a function maketrans() and a method translate that operates on strings.
The function maketrans() creates translation tables that can be used with the translate method to change one set of characters to another more efficiently. (Quoted from The Python Standard Library by Example).
import string
def caesar(plaintext, shift):
shift %= 26 # Values greater than 26 will wrap around
alphabet_lower = string.ascii_lowercase
alphabet_upper = string.ascii_uppercase
shifted_alphabet_lower = alphabet_lower[shift:] + alphabet_lower[:shift]
shifted_alphabet_upper = alphabet_upper[shift:] + alphabet_upper[:shift]
alphabet = alphabet_lower + alphabet_upper
shifted_alphabet = shifted_alphabet_lower + shifted_alphabet_upper
table = string.maketrans(alphabet, shifted_alphabet)
return plaintext.translate(table)
Here, a more functional way:
(if you use shift i to encode, then use -i to decode)
def ceasar(story, shift):
return ''.join([ # concentrate list to string
(lambda c, is_upper: c.upper() if is_upper else c) # if original char is upper case than convert result to upper case too
(
("abcdefghijklmnopqrstuvwxyz"*2)[ord(char.lower()) - ord('a') + shift % 26], # rotate char, this is extra easy since Python accepts list indexs below 0
char.isupper()
)
if char.isalpha() else char # if not in alphabet then don't change it
for char in story
])
plainText = raw_input("What is your plaintext? ")
shift = int(raw_input("What is your shift? "))
def caesar(plainText, shift):
for ch in plainText:
if ch.isalpha():
stayInAlphabet = ord(ch) + shift
if stayInAlphabet > ord('z'):
stayInAlphabet -= 26
finalLetter = chr(stayInAlphabet)
#####HERE YOU RESET CIPHERTEXT IN EACH ITERATION#####
cipherText = ""
cipherText += finalLetter
print "Your ciphertext is: ", cipherText
return cipherText
caesar(plainText, shift)
As an else to if ch.isalpha() you can put finalLetter=ch.
You should remove the line: cipherText = ""
Cheers.
As #I82much said, you need to take cipherText = "" outside of your for loop. Place it at the beginning of the function. Also, your program has a bug which will cause it to generate encryption errors when you get capital letters as input. Try:
if ch.isalpha():
finalLetter = chr((ord(ch.lower()) - 97 + shift) % 26 + 97)
>>> def rotate(txt, key):
... def cipher(i, low=range(97,123), upper=range(65,91)):
... if i in low or i in upper:
... s = 65 if i in upper else 97
... i = (i - s + key) % 26 + s
... return chr(i)
... return ''.join([cipher(ord(s)) for s in txt])
# test
>>> rotate('abc', 2)
'cde'
>>> rotate('xyz', 2)
'zab'
>>> rotate('ab', 26)
'ab'
>>> rotate('Hello, World!', 7)
'Olssv, Dvysk!'
I have a hard time remember the char to int conversions so this could be optimized
def decryptCaesar(encrypted, shift):
minRange = ord('a')
decrypted = ""
for char in encrypted:
decrypted += chr(((ord(char) - minRange + shift) % 26) + minRange)
return decrypted
def encrypt():
plainText = input("What is your plaintext? ")
shift = int(input("What is your shift? "))
cipherText = ""
for ch in plainText:
if ch.isalpha():
stayInAlphabet = ord(ch) + shift
if stayInAlphabet > ord('z'):
stayInAlphabet -= 26
finalLetter = chr(stayInAlphabet)
cipherText += finalLetter
print ("Your ciphertext is: ", cipherText,"with a shift of",shift)
def decrypte():
encryption=input("enter in your encrypted code")
encryption_shift=int(input("enter in your encryption shift"))
cipherText1 = ""
for c in encryption:
if c.isalpha():
stayInAlphabet1 = ord(c) - encryption_shift
if stayInAlphabet1 > ord('z'):
stayInAlphabet1 += 26
finalLetter1 = chr(stayInAlphabet1)
cipherText1 += finalLetter1
print ("Your ciphertext is: ", cipherText1,"with negative shift of",encryption_shift)
from tkinter import *
menu=Tk()
menu.title("menu")
menu.geometry("300x300")
button1= Button(menu,text="encrypt",command=encrypt)
button1.pack()
button2= Button(menu,text="decrypt",command=decrypte)
button2.pack()
button3= Button(menu,text="exit",command=exit)
button3.pack()
menu.mainloop()
message = 'The quick brown fox jumped over the lazy dog. 1234567890 !##$%^&*()_+-'
encrypted = ''.join(chr(ord(char)+3) for char in message)
decrypted = ''.join(chr(ord(char)-3) for char in encrypted)
print(encrypted)
print(decrypted)
# Wkh#txlfn#eurzq#ir{#mxpshg#ryhu#wkh#od}|#grj1#456789:;<3#$C&'(a)-+,b.0
# The quick brown fox jumped over the lazy dog. 1234567890 !##$%^&*()_+-
def encrypt(text,shift):
'''
INPUT: text as a string and an integer for the shift value.
OUTPUT: The shifted text after being run through the Caeser cipher.
'''
# Create a placeholder list
encrypted_text = list(range(len(text)))
alphabet = string.ascii_lowercase
# Create shifted alphabet
first_half = alphabet[:shift]
second_half = alphabet[shift:]
shifted_alphabet = second_half+first_half
for i,letter in enumerate(text.lower()):
# Check for spaces or punctuation
if letter in alphabet:
# Find the original index position
original_index = alphabet.index(letter)
# Shifted letter
new_letter = shifted_alphabet[original_index]
encrypted_text[i] = new_letter
# Punctuation or space
else:
encrypted_text[i] = letter
return ''.join(encrypted_text)
For example, decod string:
"uo jxuhu! jxyi yi qd unqcfbu ev q squiqh syfxuh. muhu oek qrbu je tusetu yj? y xefu ie! iudt cu q cuiiqwu rqsa myjx jxu iqcu evviuj!".
This message has an offset of 10.
Code below:
import string
alphabet = list(string.ascii_lowercase)
print(alphabet, len(alphabet))
messege = "xuo jxuhu! jxyi yi qd unqcfbu ev q squiqh syfxuh. muhu oek qrbu je tusetu yj? y xefu ie! iudt cu q cuiiqwu rqsa myjx jxu iqcu evviuj!"
messege_split = messege.split()
print(messege_split)
encrypted_messege = ""
position = 0
for i in messege_split:
for j in i:
if ord(j) < 65:
encrypted_messege += j
else:
for k in alphabet:
if j == k:
position = alphabet.index(k)
if (position + 10) >= len(alphabet):
encrypted_messege += alphabet[abs((position + 10) - len(alphabet))]
else:
encrypted_messege += alphabet[position + 10]
encrypted_messege += " "
print(encrypted_messege)
Decoded string:
"hey there! this is an example of a caesar cipher. were you able to decode it? i hope so! send me a message back with the same offset!"
TRY IT!
Using cyclic generator:
import string
from itertools import cycle
def caesarCipherEncryptor(s, key):
def generate_letters():
yield from cycle(string.ascii_lowercase)
def find_next(v, g, c):
# Eat up characters until we arrive at the plaintext character
while True:
if v == next(g):
break
# Increment the plaintext character by the count using the generator
try:
for _ in range(c):
item = next(g)
return item
except UnboundLocalError:
return v
return "".join([find_next(i, generate_letters(), key) for i in s])
# Outputs
>>> caesarCipherEncryptor("xyz", 3)
>>> 'abc'
from string import ascii_lowercase as alphabet
class CaesarCypher:
alpha_len = len(alphabet)
min_guess_rate = 0.2
Encryption and decryption is a same stuff. when you want to decrypt for example with shift 10 that means that you can encrypt it with shift 26 - 10. In this case cycle will repeat at if you going to shift whole alphabet it will be the same. Also here i've proceed upper case and non chars
def __call__(self, text, offset, encrypt=True):
if not encrypt:
offset = self.alpha_len - offset
result = []
for letter in text:
if not letter.isalpha():
result.append(letter)
continue
letter_to_process = letter.lower()
processed_letter = self._encrypt_letter(letter_to_process, offset)
if letter.isupper():
processed_letter = processed_letter.upper()
result.append(processed_letter)
return ''.join(result)
all encryption goes here at most.
def _encrypt_letter(self, letter, offset=0):
position = (alphabet.find(letter) + offset) % self.alpha_len
return alphabet[position]
this part is for broot force and guess throug dictionary frequency.
#staticmethod
def __how_many_do_i_know(text):
clean_words = filter(lambda x: x.isalpha(), text.split())
clean_words = ['\'{}\''.format(x) for x in clean_words]
cursor = conn.cursor()
query = 'SELECT COUNT(*) FROM mydictionary WHERE word IN ({})'.format(",".join(clean_words))
cursor.execute(query)
response = cursor.fetchone()[0]
return response / len(clean_words)
def guess_encode(self, text):
options = [self(text, offset, encrypt=False) for offset in range(self.alpha_len)]
best_option = [self.__how_many_do_i_know(option) for option in options]
best_key, guess_rate = max(enumerate(best_option), key=lambda x: x[-1])
guess_text = options[best_key]
return best_key, guess_rate, guess_text
import string
wrd=raw_input("Enter word").lower()
fwrd=""
for let in wrd:
fwrd+=string.ascii_lowercase[(string.ascii_lowercase).index(let)+3]
print"Original word",wrd
print"New word",fwrd
according to me this answer is useful for you:
def casear(a,key):
str=""
if key>26:
key%=26
for i in range(0,len(a)):
if a[i].isalpha():
b=ord(a[i])
b+=key
#if b>90: #if upper case letter ppear in your string
# c=b-90 #if upper case letter ppear in your string
# str+=chr(64+c) #if upper case letter ppear in your string
if b>122:
c=b-122
str+=chr(96+c)
else:
str+=chr(b)
else:
str+=a[i]
print str
a=raw_input()
key=int(input())
casear(a,key)
This function shifts all letter to right according to given key.
Why not use the function reverse on the shift input, and and join the plain_text with the shift, and input it as the cipher text:
Plain = int(input("enter a number "))
Rev = plain[::-1]
Cipher = " ".join(for cipher_text in Rev)
The code is very large, but easy to understand. I think it fits your situation.
alphabet = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
class CaesarCipher(object):
def __init__(self, shift):
self.shift = shift
def encode(self, str):
encode = ''
str = str.lower()
for i in str:
if i in alphabet:
encode += alphabet[alphabet.index(i) + self.shift]
else:
encode += i
return encode.upper()
def decode(self, str):
decode = ''
str = str.lower()
for i in str:
if i in alphabet:
decode += alphabet[alphabet.index(i) - self.shift]
else:
decode += i
return decode.upper()
Using map:
def caesar(text, key):
return ''.join(map(lambda c:
chr((ord(c.lower()) - ord('a') + key) % 26 + ord('a')) if c.isalpha() else ''
, text))
This solution is more intuitively without the use of ord function:
def caesar_cipher(raw_text, key):
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
shifted_alphabet = alphabet[26-key:]+alphabet[0:(26-key)]
cipher_text = ""
for i in range(len(raw_text)):
char = raw_text[i]
idx = alphabet.find(char.upper())
if idx == -1:
cipher_text = cipher_text + char
elif char.islower():
cipher_text = cipher_text + shifted_alphabet[idx].lower()
else:
cipher_text = cipher_text + shifted_alphabet[idx]
return(cipher_text)
And an example:
plain_text = "The quick brown fox jumps over the lazy dog!"
caesar_cipher(plain_text,3)
And we get:
'Qeb nrfzh yoltk clu grjmp lsbo qeb ixwv ald!'
If we want to decrypt it:
caesar_cipher(caesar_cipher(plain_text,3),26-3)
and we get:
'The quick brown fox jumps over the lazy dog!'
More details here:https://predictivehacks.com/caesar-cipher-in-python/
caesar-cipher
message = str(input("Enter you message:"))
shift = int(input("Enter a number:"))
# encode
stringValue = [ord(message) - 96 for message in message]
print(stringValue)
encode_msg_val = []
[encode_msg_val.append(int(stringValue[i])+shift) for i in
range(len(stringValue))]
encode_msg_array = []
for i in range(len(encode_msg_val)):
encode_val = encode_msg_val[i] + 96
encode_msg_array.append(chr(encode_val))
print(encode_msg_array)
encode_msg = ''.join(encode_msg_array)
# dedcode
[deocde_msg_val = [ord(encode_msg) - 96 for encode_msg in encode_msg]
decode_val = []
[decode_val.append(deocde_msg_val[i] - shift) for i in
range(len(deocde_msg_val))]
decode_msg_array = []
[decode_msg_array.append(decode_val[i] + 96) for i in range(len(decode_val))]
decode_msg_list = []
[decode_msg_list.append(chr(decode_msg_array[i])) for i in
range(len(decode_msg_array))]
decode_msg = ''.join(decode_msg_list)
print(decode_msg)
alph = 'abcdefghijklmnopqrstuvwxyz'
# shift = int(input("Please enter the number of places to shift:"))
shift = 15
text = "python is fun!"
alph_len = len(alph)
if shift >=0 and shift <= alph_len:
# text = input("Please enter a sentence:")
shifted_alph = alph[shift:] + alph[:shift] # rotate
text = text.lower()
crypted_text = ""
for letter in text:
if letter in alph:
ind = alph.index(letter)
crypted_letter = shifted_alph[ind]
else:
crypted_letter = letter
crypted_text += crypted_letter
print(crypted_text)
else:
print(f"You need to enter a number between 0 and {alph_len}!")
# eniwdc xh ujc! # output
key = 3
def wub():
def choice():
choice = input("Do you wish to Encrypt of Decrypt?")
choice = choice.lower()
if choice == "e" or "encrypt":
return choice
elif choice == "d" or "decrypt":
return choice
else:
print("Invalid response, please try again.")
choice()
def message():
user = input("Enter your message: ")
return user
def waffle(choice, message, key):
translated = ""
if choice == "e" or "encrypt":
for character in message:
num = ord(character)
num += key
translated += chr(num)
derek = open('Encrypted.txt', 'w')
derek.write(translated)
derek.close()
return translated
else:
for character in message:
num = ord(character)
num -= key
translated += chr(num)
return translated
choice = choice() #Runs function for encrypt/decrypt selection. Saves choice made.
message = message() #Run function for user to enter message. Saves message.
final = waffle(choice, message, key) #Runs function to translate message, using the choice, message and key variables)
print("\n Operation complete!")
print(final)
wub()

Categories

Resources