Turtle Programming - Encryption in Python Final PDF
Turtle Programming - Encryption in Python Final PDF
First we import the turtle module. Then create a window, next we create turtle object and using
turtle method we can draw in the drawing board. “Turtle” is a Python feature like a drawing
board, which lets us command a turtle to draw all over it! We can use functions like
turtle.forward(…) and turtle.right(…) which can move the turtle around.Commonly used turtle
methods are :
fillcolor() Color name Changes the color of the turtle will use to fill a polygon
To make use of the turtle methods and functionalities, we need to import turtle.”turtle” comes
packed with the standard Python package and need not be installed externally.The roadmap for
executing a turtle program follows 4 steps:
6. After importing the turtle library and making all the turtle functionalities available to us,
we need to create a new drawing board(window) and a turtle. Let’s call the window as
wn and the turtle as skk. So we code as:
wn = turtle.Screen()
wn.bgcolor("light green")
wn.title("Turtle")
skk = turtle.Turtle()
7. Now that we have created the window and the turtle, we need to move the turtle. To
move forward 100 pixels in the direction skk is facing, we code:
skk.forward(100)
8. We have moved skk 100 pixels forward, Awesome! Now we complete the program with
the done() function and We’re done!
turtle.done()
9. So, we have created a program that draws a line 100 pixels long. We can draw various
shapes and fill different colors using turtle methods. There’s plethora of functions and
programs to be coded using the turtle library in python. Let’s learn to draw some of the
basic shapes.
So, Code will be:
import turtle
wn = turtle.Screen()
wn.bgcolor("light green")
wn.title("Turtle")
skk = turtle.Turtle()
skk.forward(100)
turtle.done()
import turtle
skk = turtle.Turtle()
for i in range(4):
skk.forward(50)
skk.right(90)
turtle.done()
import turtle
star = turtle.Turtle()
for i in range(50):
star.forward(50)
star.right(144)
turtle.done()
import turtle
polygon = turtle.Turtle()
side_length = 70
for i in range(num_sides):
polygon.forward(side_length)
polygon.right(angle)
turtle.done()
wn = turtle.Screen()
wn.bgcolor("light green")
skk = turtle.Turtle()
skk.color("blue")
def sqrfunc(size):
for i in range(4):
skk.fd(size)
skk.left(90)
size = size + 5
sqrfunc(6)
sqrfunc(26)
sqrfunc(46)
sqrfunc(66)
sqrfunc(86)
sqrfunc(106)
sqrfunc(126)
sqrfunc(146)
Encryption Decryption In Python
Cryptography
The science of writing secret codes is called cryptography. For thousands of years cryptography
has made secret messages that only the sender and recipient could read, even if someone
captured the messenger and read the coded message. A secret code system is called a cipher. The
cipher used by the program in this chapter is called the Caesar cipher.
In cryptography, we call the message that we want to be secret the plaintext. The plaintext could
look like this:
Hello there! The keys to the house are hidden under the flower pot.
Converting the plaintext into the encoded message is called encrypting the plaintext. The
plaintext is encrypted into the ciphertext. The ciphertext looks like random letters, and we cannot
understand what the original plaintext was just by looking at the ciphertext. Here is the previous
example encrypted into ciphertext:
Yvccf kyviv! Kyv bvpj kf kyv yfljv riv yzuuve leuvi kyv wcfnvi gfk.
But if you know about the cipher used to encrypt the message, you can decrypt the ciphertext
back to the plaintext. (Decryption is the opposite of encryption.)
Many ciphers also use keys. Keys are secret values that let you decrypt ciphertext that was
encrypted using a specific cipher. Think of the cipher as being like a door lock. You can only
unlock it with a particular key.
The key for the Caesar Cipher will be a number from 1 to 26. Unless you know the key (that is,
know the number used to encrypt the message), you won’t be able to decrypt the secret code.
The Caesar Cipher was one of the earliest ciphers ever invented. In this cipher, you encrypt a
message by taking each letter in the message (in cryptography, these letters are called symbols
because they can be letters, numbers, or any other sign) and replacing it with a “shifted” letter. If
you shift the letter A by one space, you get the letter B. If you shift the letter A by two spaces,
you get the letter C. Figure 14-1 is a picture of some letters shifted over by three spaces.
Figure 14-1: Shifting over letters by three spaces. Here, B becomes E.
To get each shifted letter, draw out a row of boxes with each letter of the alphabet. Then draw a
second row of boxes under it, but start a certain number (this number is the key) of spaces over.
After the letters at the end, wrap around back to the start of the boxes. Here is an example with
the letters shifted by three spaces:
The number of spaces you shift is the key in the Caesar Cipher. The example above shows the
letter translations for the key 3.
How do we implement this shifting of the letters as code? We can do this by representing each
letter as a number called an ordinal, and then adding or subtracting from this number to form a
new ordinal (and a new letter). ASCII (pronounced “ask-ee” and stands for American Standard
Code for Information Interchange) is a code that connects each character to a number between 32
and 126.
The capital letters “A” through “Z” have the ASCII numbers 65 through 90. The lowercase
letters “a” through “z” have the ASCII numbers 97 through 122. The numeric digits “0” through
“9” have the ASCII numbers 48 through 57. Table 14-1 shows all the ASCII characters and
ordinals.
Modern computers use UTF-8 instead of ASCII. But UTF-8 is backwards compatible with
ASCII, so the UTF-8 ordinals for ASCII characters are the same as ASCII’s ordinals.
Table 14-1: The ASCII Table
32 (space) 48 0 64 @ 80 P 96 ` 112 p
33 ! 49 1 65 A 81 Q 97 a 113 q
34 " 50 2 66 B 82 R 98 b 114 r
35 # 51 3 67 C 83 S 99 c 115 s
36 $ 52 4 68 D 84 T 100 d 116 t
37 % 53 5 69 E 85 U 101 e 117 u
38 & 54 6 70 F 86 V 102 f 118 v
39 ' 55 7 71 G 87 W 103 g 119 w
40 ( 56 8 72 H 88 X 104 h 120 x
41 ) 57 9 73 I 89 Y 105 i 121 y
42 * 58 : 74 J 90 Z 106 j 122 z
43 + 59 ; 75 K 91 [ 107 k 123 {
44 , 60 < 76 L 92 \ 108 l 124 |
45 - 61 = 77 M 93 ] 109 m 125 }
46 . 62 > 78 N 94 ^ 110 n 126 ~
47 / 63 ? 79 O 95 _ 111 o
So if you wanted to shift “A” by three spaces, you would do the following:
The chr() and ord() functions can convert between characters and ordinals.
>>> chr(65)
'A'
>>> ord('A')
65
>>> chr(65+8)
'I'
>>> chr(52)
'4'
>>> chr(ord('F'))
'F'
>>> ord(chr(68))
68
On the third line, chr(65+8) evaluates to chr(73). If you look at the ASCII table, you can see that 73
is the ordinal for the capital letter “I”.
On the fifth line, chr(ord('F')) evaluates to chr(70) which evaluates to 'F'. The ord() and chr() functions
are the opposite of each other.
CODE:
# CAESAR CIPHER
cipher = ''
elif char.isupper():
else:
return cipher
cipher = ''
elif char.isupper():
else:
return cipher
CT=encrypt(text, s)
OUTPUT:
enter string: abc
enter shift number: 1
original string: abc
after encryption: bcd
after decryption: abc