Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 52 lines (39 sloc) 1.516 kb
f16b361 @asweigart Initial commit.
authored
1 # Caesar Cipher
2 # http://inventwithpython.com/codebreaker (BSD Licensed)
3
c4fd6ab @asweigart Pylint-suggested fixes.
authored
4 import pyperclip
5
f16b361 @asweigart Initial commit.
authored
6 # the string to be encrypted/decrypted
7 message = 'This is my secret message.'
8
9 # the encryption/decryption key
10 key = 13
11
12 # tells the program to encrypt of decrypt
13 mode = 'encrypt' # set to 'encrypt' or 'decrypt'
14
15 # every possible symbol that can be encrypted
29eac1b @asweigart Whole buncha changes. (Worst log message ever, I know.)
authored
16 LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
f16b361 @asweigart Initial commit.
authored
17
18 # stores the encrypted/decrypted form of the message
19 translated = ''
20
21 # capitalize the string in message
22 message = message.upper()
23
24 # run the encryption/decryption code on each symbol in the message string
25 for symbol in message:
91f8abb @asweigart First round of personal fixes. This is an unhelpful log message.
authored
26 if symbol in LETTERS:
f16b361 @asweigart Initial commit.
authored
27 # get the encrypted (or decrypted) number for this symbol
91f8abb @asweigart First round of personal fixes. This is an unhelpful log message.
authored
28 num = LETTERS.find(symbol) # get the number of the symbol
f16b361 @asweigart Initial commit.
authored
29 if mode == 'encrypt':
c4fd6ab @asweigart Pylint-suggested fixes.
authored
30 num = num + key
f16b361 @asweigart Initial commit.
authored
31 elif mode == 'decrypt':
c4fd6ab @asweigart Pylint-suggested fixes.
authored
32 num = num - key
f16b361 @asweigart Initial commit.
authored
33
24fc561 @asweigart Changes from unit testing.
authored
34 # handle the wrap around if num is larger than the length of
35 # LETTERS or less than 0
91f8abb @asweigart First round of personal fixes. This is an unhelpful log message.
authored
36 if num >= len(LETTERS):
37 num = num - len(LETTERS)
f16b361 @asweigart Initial commit.
authored
38 elif num < 0:
91f8abb @asweigart First round of personal fixes. This is an unhelpful log message.
authored
39 num = num + len(LETTERS)
f16b361 @asweigart Initial commit.
authored
40
41 # add encrypted/decrypted number's symbol at the end of translated
91f8abb @asweigart First round of personal fixes. This is an unhelpful log message.
authored
42 translated = translated + LETTERS[num]
f16b361 @asweigart Initial commit.
authored
43
44 else:
45 # just add the symbol without encrypting/decrypting
46 translated = translated + symbol
47
48 # print the encrypted/decrypted string to the screen
c4fd6ab @asweigart Pylint-suggested fixes.
authored
49 print(translated)
50
51 # copy the encrypted/decrypted string to the clipboard
52 pyperclip.copy(translated)
Something went wrong with that request. Please try again.