Crypto Python Encryption
I am trying to create a program that generates a coded message that looks like gibberish to any outsiders.
I want it to allow a message to be encrypted using any rotation amount. So, users will be able to type a message in the terminal and specify a rotation amount (13, 4, 600, etc) and the program will print the resulting encrypted message.
The final interactive program will run like this:
$ python caesar.py
Type a message:
Hello, World!
Rotate by:
5
Mjqqt, Btwqi!
I using some helper functions to help break the problem down into manageable steps.
I have tried writing a function called alphabet_position(letter), which receives a letter ( a string with only one alphabetic character) and returns the 0-based numerical position of that letter within the alphabet, assigning the values of 0 to a and A, 1 to b and B, all the way up to 25 for z and Z.
Then I wrote another function rotate_character(char, rot) which receives a character char ( a string of length 1), and an integer rot. The function should return a new string of length 1, the result of rotating char by rot number of places to the right. It should use the alphabet_position function above and wrap back around to the beginning of the alphabet if the rotation is greater than 25.
Then, I wrote one more function called encrypt(text, rot) which receives an input of a string and an integer. The second argument, rot, specifies the rotation amount. The function should return the result of rotating each letter in the text by rot places to the right.
The result in the terminal should look like this:
$ python caesar.py
Type a message:
Hello, World!
Rotate by:
5
Mjqqt, Btwqi!
For the final portion of this, I want to create a Vigenere cipher.
Vigenere uses a word as the encryption key, rather than an integer. The finished program will behave like this:
$ python vigenere.py
Type a message:
The crow flies at midnight!
Encryption key:
boom
Uvs osck rmwse bh auebwsih!
If you could provide any information on what I have done incorrectly with this code, I would appreciate it.
def encrypt(text,rot):
text_new = ""
for pos in range(len(text)):
text_new += rotate_character(text[pos],int(rot))
return text_new
def alphabet_position(letter):
alphabet_pos = {'A':0, 'a':0, 'B':1, 'b':1, 'C':2, 'c':2,
'D':3, 'd':3, 'E':4, 'e':4, 'F':5, 'f':5, 'G':6, 'g':6, 'H':7,
'h':7, 'I':8, 'i':8, 'J':9, 'j':9, 'K':10, 'k':10, 'L':11, 'l':11,
'M':12, 'm':12, 'N':13, 'n':13, 'O':14, 'o':14, 'P':15, 'p':15,
'Q':16, 'q':16, 'R':17, 'r':17, 'S':18, 's':18, 'T':19, 't':19,
'U':20, 'u':20, 'V':21, 'v':21, 'W':22, 'w':22, 'X':23, 'x':23,
'Y':24, 'y':24, 'Z':25, 'z':25}
pos = alphabet_pos[letter]
return alphabet_position(letter)
def rotate_character(char,rot):
x = (alphabet_position(char))
y = (x + rot)%26
if (ord(char) >= 97) and (ord(char) <= 122): # lowercase
return x.lower
elif (ord(char) >= 65) and (ord(char) <=90): # uppercase
return x.upper
else:
return char
char = input('Enter a character:')
rot = input('Enter a number to rotate by:')
print(rotate_character(char,rot))
def main():
text = input("Type a message")
print("text")
rot = input("Rotate by:")
if __name__ == "__main__":
main()
Instead of the expected results of encrypting the message, the terminal returned this instead:
line 10, in alphabet_position
return alphabet_position(letter)
[Previous line repeated 987 more times]
RecursionError: maximum recursion depth exceeded
python python-3.x cryptography
add a comment |
I am trying to create a program that generates a coded message that looks like gibberish to any outsiders.
I want it to allow a message to be encrypted using any rotation amount. So, users will be able to type a message in the terminal and specify a rotation amount (13, 4, 600, etc) and the program will print the resulting encrypted message.
The final interactive program will run like this:
$ python caesar.py
Type a message:
Hello, World!
Rotate by:
5
Mjqqt, Btwqi!
I using some helper functions to help break the problem down into manageable steps.
I have tried writing a function called alphabet_position(letter), which receives a letter ( a string with only one alphabetic character) and returns the 0-based numerical position of that letter within the alphabet, assigning the values of 0 to a and A, 1 to b and B, all the way up to 25 for z and Z.
Then I wrote another function rotate_character(char, rot) which receives a character char ( a string of length 1), and an integer rot. The function should return a new string of length 1, the result of rotating char by rot number of places to the right. It should use the alphabet_position function above and wrap back around to the beginning of the alphabet if the rotation is greater than 25.
Then, I wrote one more function called encrypt(text, rot) which receives an input of a string and an integer. The second argument, rot, specifies the rotation amount. The function should return the result of rotating each letter in the text by rot places to the right.
The result in the terminal should look like this:
$ python caesar.py
Type a message:
Hello, World!
Rotate by:
5
Mjqqt, Btwqi!
For the final portion of this, I want to create a Vigenere cipher.
Vigenere uses a word as the encryption key, rather than an integer. The finished program will behave like this:
$ python vigenere.py
Type a message:
The crow flies at midnight!
Encryption key:
boom
Uvs osck rmwse bh auebwsih!
If you could provide any information on what I have done incorrectly with this code, I would appreciate it.
def encrypt(text,rot):
text_new = ""
for pos in range(len(text)):
text_new += rotate_character(text[pos],int(rot))
return text_new
def alphabet_position(letter):
alphabet_pos = {'A':0, 'a':0, 'B':1, 'b':1, 'C':2, 'c':2,
'D':3, 'd':3, 'E':4, 'e':4, 'F':5, 'f':5, 'G':6, 'g':6, 'H':7,
'h':7, 'I':8, 'i':8, 'J':9, 'j':9, 'K':10, 'k':10, 'L':11, 'l':11,
'M':12, 'm':12, 'N':13, 'n':13, 'O':14, 'o':14, 'P':15, 'p':15,
'Q':16, 'q':16, 'R':17, 'r':17, 'S':18, 's':18, 'T':19, 't':19,
'U':20, 'u':20, 'V':21, 'v':21, 'W':22, 'w':22, 'X':23, 'x':23,
'Y':24, 'y':24, 'Z':25, 'z':25}
pos = alphabet_pos[letter]
return alphabet_position(letter)
def rotate_character(char,rot):
x = (alphabet_position(char))
y = (x + rot)%26
if (ord(char) >= 97) and (ord(char) <= 122): # lowercase
return x.lower
elif (ord(char) >= 65) and (ord(char) <=90): # uppercase
return x.upper
else:
return char
char = input('Enter a character:')
rot = input('Enter a number to rotate by:')
print(rotate_character(char,rot))
def main():
text = input("Type a message")
print("text")
rot = input("Rotate by:")
if __name__ == "__main__":
main()
Instead of the expected results of encrypting the message, the terminal returned this instead:
line 10, in alphabet_position
return alphabet_position(letter)
[Previous line repeated 987 more times]
RecursionError: maximum recursion depth exceeded
python python-3.x cryptography
You are having the functionalphabet_position(letter)return itself rather than return a variable inside the function. Basically it is recursing over itself which doesn't seem to be your desired behavior. I think you mean toreturn posin that function
– d_kennetz
Jan 2 at 22:52
add a comment |
I am trying to create a program that generates a coded message that looks like gibberish to any outsiders.
I want it to allow a message to be encrypted using any rotation amount. So, users will be able to type a message in the terminal and specify a rotation amount (13, 4, 600, etc) and the program will print the resulting encrypted message.
The final interactive program will run like this:
$ python caesar.py
Type a message:
Hello, World!
Rotate by:
5
Mjqqt, Btwqi!
I using some helper functions to help break the problem down into manageable steps.
I have tried writing a function called alphabet_position(letter), which receives a letter ( a string with only one alphabetic character) and returns the 0-based numerical position of that letter within the alphabet, assigning the values of 0 to a and A, 1 to b and B, all the way up to 25 for z and Z.
Then I wrote another function rotate_character(char, rot) which receives a character char ( a string of length 1), and an integer rot. The function should return a new string of length 1, the result of rotating char by rot number of places to the right. It should use the alphabet_position function above and wrap back around to the beginning of the alphabet if the rotation is greater than 25.
Then, I wrote one more function called encrypt(text, rot) which receives an input of a string and an integer. The second argument, rot, specifies the rotation amount. The function should return the result of rotating each letter in the text by rot places to the right.
The result in the terminal should look like this:
$ python caesar.py
Type a message:
Hello, World!
Rotate by:
5
Mjqqt, Btwqi!
For the final portion of this, I want to create a Vigenere cipher.
Vigenere uses a word as the encryption key, rather than an integer. The finished program will behave like this:
$ python vigenere.py
Type a message:
The crow flies at midnight!
Encryption key:
boom
Uvs osck rmwse bh auebwsih!
If you could provide any information on what I have done incorrectly with this code, I would appreciate it.
def encrypt(text,rot):
text_new = ""
for pos in range(len(text)):
text_new += rotate_character(text[pos],int(rot))
return text_new
def alphabet_position(letter):
alphabet_pos = {'A':0, 'a':0, 'B':1, 'b':1, 'C':2, 'c':2,
'D':3, 'd':3, 'E':4, 'e':4, 'F':5, 'f':5, 'G':6, 'g':6, 'H':7,
'h':7, 'I':8, 'i':8, 'J':9, 'j':9, 'K':10, 'k':10, 'L':11, 'l':11,
'M':12, 'm':12, 'N':13, 'n':13, 'O':14, 'o':14, 'P':15, 'p':15,
'Q':16, 'q':16, 'R':17, 'r':17, 'S':18, 's':18, 'T':19, 't':19,
'U':20, 'u':20, 'V':21, 'v':21, 'W':22, 'w':22, 'X':23, 'x':23,
'Y':24, 'y':24, 'Z':25, 'z':25}
pos = alphabet_pos[letter]
return alphabet_position(letter)
def rotate_character(char,rot):
x = (alphabet_position(char))
y = (x + rot)%26
if (ord(char) >= 97) and (ord(char) <= 122): # lowercase
return x.lower
elif (ord(char) >= 65) and (ord(char) <=90): # uppercase
return x.upper
else:
return char
char = input('Enter a character:')
rot = input('Enter a number to rotate by:')
print(rotate_character(char,rot))
def main():
text = input("Type a message")
print("text")
rot = input("Rotate by:")
if __name__ == "__main__":
main()
Instead of the expected results of encrypting the message, the terminal returned this instead:
line 10, in alphabet_position
return alphabet_position(letter)
[Previous line repeated 987 more times]
RecursionError: maximum recursion depth exceeded
python python-3.x cryptography
I am trying to create a program that generates a coded message that looks like gibberish to any outsiders.
I want it to allow a message to be encrypted using any rotation amount. So, users will be able to type a message in the terminal and specify a rotation amount (13, 4, 600, etc) and the program will print the resulting encrypted message.
The final interactive program will run like this:
$ python caesar.py
Type a message:
Hello, World!
Rotate by:
5
Mjqqt, Btwqi!
I using some helper functions to help break the problem down into manageable steps.
I have tried writing a function called alphabet_position(letter), which receives a letter ( a string with only one alphabetic character) and returns the 0-based numerical position of that letter within the alphabet, assigning the values of 0 to a and A, 1 to b and B, all the way up to 25 for z and Z.
Then I wrote another function rotate_character(char, rot) which receives a character char ( a string of length 1), and an integer rot. The function should return a new string of length 1, the result of rotating char by rot number of places to the right. It should use the alphabet_position function above and wrap back around to the beginning of the alphabet if the rotation is greater than 25.
Then, I wrote one more function called encrypt(text, rot) which receives an input of a string and an integer. The second argument, rot, specifies the rotation amount. The function should return the result of rotating each letter in the text by rot places to the right.
The result in the terminal should look like this:
$ python caesar.py
Type a message:
Hello, World!
Rotate by:
5
Mjqqt, Btwqi!
For the final portion of this, I want to create a Vigenere cipher.
Vigenere uses a word as the encryption key, rather than an integer. The finished program will behave like this:
$ python vigenere.py
Type a message:
The crow flies at midnight!
Encryption key:
boom
Uvs osck rmwse bh auebwsih!
If you could provide any information on what I have done incorrectly with this code, I would appreciate it.
def encrypt(text,rot):
text_new = ""
for pos in range(len(text)):
text_new += rotate_character(text[pos],int(rot))
return text_new
def alphabet_position(letter):
alphabet_pos = {'A':0, 'a':0, 'B':1, 'b':1, 'C':2, 'c':2,
'D':3, 'd':3, 'E':4, 'e':4, 'F':5, 'f':5, 'G':6, 'g':6, 'H':7,
'h':7, 'I':8, 'i':8, 'J':9, 'j':9, 'K':10, 'k':10, 'L':11, 'l':11,
'M':12, 'm':12, 'N':13, 'n':13, 'O':14, 'o':14, 'P':15, 'p':15,
'Q':16, 'q':16, 'R':17, 'r':17, 'S':18, 's':18, 'T':19, 't':19,
'U':20, 'u':20, 'V':21, 'v':21, 'W':22, 'w':22, 'X':23, 'x':23,
'Y':24, 'y':24, 'Z':25, 'z':25}
pos = alphabet_pos[letter]
return alphabet_position(letter)
def rotate_character(char,rot):
x = (alphabet_position(char))
y = (x + rot)%26
if (ord(char) >= 97) and (ord(char) <= 122): # lowercase
return x.lower
elif (ord(char) >= 65) and (ord(char) <=90): # uppercase
return x.upper
else:
return char
char = input('Enter a character:')
rot = input('Enter a number to rotate by:')
print(rotate_character(char,rot))
def main():
text = input("Type a message")
print("text")
rot = input("Rotate by:")
if __name__ == "__main__":
main()
Instead of the expected results of encrypting the message, the terminal returned this instead:
line 10, in alphabet_position
return alphabet_position(letter)
[Previous line repeated 987 more times]
RecursionError: maximum recursion depth exceeded
python python-3.x cryptography
python python-3.x cryptography
asked Jan 2 at 22:19
WesleyWesley
1
1
You are having the functionalphabet_position(letter)return itself rather than return a variable inside the function. Basically it is recursing over itself which doesn't seem to be your desired behavior. I think you mean toreturn posin that function
– d_kennetz
Jan 2 at 22:52
add a comment |
You are having the functionalphabet_position(letter)return itself rather than return a variable inside the function. Basically it is recursing over itself which doesn't seem to be your desired behavior. I think you mean toreturn posin that function
– d_kennetz
Jan 2 at 22:52
You are having the function
alphabet_position(letter) return itself rather than return a variable inside the function. Basically it is recursing over itself which doesn't seem to be your desired behavior. I think you mean to return pos in that function– d_kennetz
Jan 2 at 22:52
You are having the function
alphabet_position(letter) return itself rather than return a variable inside the function. Basically it is recursing over itself which doesn't seem to be your desired behavior. I think you mean to return pos in that function– d_kennetz
Jan 2 at 22:52
add a comment |
1 Answer
1
active
oldest
votes
Your alphabet_position function's return line calls itself with the same argument it was passed. You probably meant to return alphabet_pos[letter] (or better, pos) instead of alphabet_position(letter).
Okay, thanks! Then, when I ran it, it says:line 14, in rotate_character y = (x + rot)%26 TypeError: unsupported operand type(s) for +: 'int' and 'str'
– Wesley
Jan 2 at 22:34
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54013915%2fcrypto-python-encryption%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your alphabet_position function's return line calls itself with the same argument it was passed. You probably meant to return alphabet_pos[letter] (or better, pos) instead of alphabet_position(letter).
Okay, thanks! Then, when I ran it, it says:line 14, in rotate_character y = (x + rot)%26 TypeError: unsupported operand type(s) for +: 'int' and 'str'
– Wesley
Jan 2 at 22:34
add a comment |
Your alphabet_position function's return line calls itself with the same argument it was passed. You probably meant to return alphabet_pos[letter] (or better, pos) instead of alphabet_position(letter).
Okay, thanks! Then, when I ran it, it says:line 14, in rotate_character y = (x + rot)%26 TypeError: unsupported operand type(s) for +: 'int' and 'str'
– Wesley
Jan 2 at 22:34
add a comment |
Your alphabet_position function's return line calls itself with the same argument it was passed. You probably meant to return alphabet_pos[letter] (or better, pos) instead of alphabet_position(letter).
Your alphabet_position function's return line calls itself with the same argument it was passed. You probably meant to return alphabet_pos[letter] (or better, pos) instead of alphabet_position(letter).
answered Jan 2 at 22:27
Craig MeierCraig Meier
1,245612
1,245612
Okay, thanks! Then, when I ran it, it says:line 14, in rotate_character y = (x + rot)%26 TypeError: unsupported operand type(s) for +: 'int' and 'str'
– Wesley
Jan 2 at 22:34
add a comment |
Okay, thanks! Then, when I ran it, it says:line 14, in rotate_character y = (x + rot)%26 TypeError: unsupported operand type(s) for +: 'int' and 'str'
– Wesley
Jan 2 at 22:34
Okay, thanks! Then, when I ran it, it says:line 14, in rotate_character y = (x + rot)%26 TypeError: unsupported operand type(s) for +: 'int' and 'str'
– Wesley
Jan 2 at 22:34
Okay, thanks! Then, when I ran it, it says:line 14, in rotate_character y = (x + rot)%26 TypeError: unsupported operand type(s) for +: 'int' and 'str'
– Wesley
Jan 2 at 22:34
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54013915%2fcrypto-python-encryption%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown


You are having the function
alphabet_position(letter)return itself rather than return a variable inside the function. Basically it is recursing over itself which doesn't seem to be your desired behavior. I think you mean toreturn posin that function– d_kennetz
Jan 2 at 22:52