Python Tutorial http://www.cryptoden.com/index.php/python 2015-07-03T08:40:32+00:00 Joomla! - Open Source Content Management Introduction 2012-07-14T06:50:29+00:00 2012-07-14T06:50:29+00:00 http://www.cryptoden.com/index.php/python/5-introduction Super User mikejcowan@me.com <div class="feed-description"><p><span style="font-family: verdana, geneva; font-size: small;">Many people have a computer but are not getting full value from it because they are unaware of how to write programs. For them I have written this tutorial.  Among their number are some of my colleagues in the American Cryptogram Association. Consequently in the teaching I am going to use examples drawn from the interesting world of ciphers, though the programming methods learned will be equally applicable to any application. </span></p> <p><span style="font-family: verdana, geneva; font-size: small;">Learning to program in python is straightforward. Unfortunately most books complicate things with unnecessary detail, baffling the learner and leading to a premature exit.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;"> I am going to teach you by doing. First we will make a program that enciphers plaintext with a key. Then we shall make a program to decipher with a key. You will learn by doing it, with the minimum amount of extraneous detail but acquiring the skills for writing other programs of your own.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;"> Finally we shall accomplish quite a difficult feat: we will write a program to break a cipher whose key is unknown. The program will use my CHURN algorithm to find the message.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">With the confidence you gain from making this start in programming you will probably want to delve into books on python, to widen your knowledge of what can be done. I will give you a few leads.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;"> Why have I chosen the python programming language? Because everyone can download it free, because it is modern and user-friendly, and because it is well supported with Help documents. I will begin by describing what python is and how to get python into your computer.</span></p> <p style="text-align: center;"><a href="index.php/python/6-downloading-python"><span style="font-family: verdana, geneva; font-size: small;">NEXT</span></a></p></div> <div class="feed-description"><p><span style="font-family: verdana, geneva; font-size: small;">Many people have a computer but are not getting full value from it because they are unaware of how to write programs. For them I have written this tutorial.  Among their number are some of my colleagues in the American Cryptogram Association. Consequently in the teaching I am going to use examples drawn from the interesting world of ciphers, though the programming methods learned will be equally applicable to any application. </span></p> <p><span style="font-family: verdana, geneva; font-size: small;">Learning to program in python is straightforward. Unfortunately most books complicate things with unnecessary detail, baffling the learner and leading to a premature exit.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;"> I am going to teach you by doing. First we will make a program that enciphers plaintext with a key. Then we shall make a program to decipher with a key. You will learn by doing it, with the minimum amount of extraneous detail but acquiring the skills for writing other programs of your own.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;"> Finally we shall accomplish quite a difficult feat: we will write a program to break a cipher whose key is unknown. The program will use my CHURN algorithm to find the message.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">With the confidence you gain from making this start in programming you will probably want to delve into books on python, to widen your knowledge of what can be done. I will give you a few leads.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;"> Why have I chosen the python programming language? Because everyone can download it free, because it is modern and user-friendly, and because it is well supported with Help documents. I will begin by describing what python is and how to get python into your computer.</span></p> <p style="text-align: center;"><a href="index.php/python/6-downloading-python"><span style="font-family: verdana, geneva; font-size: small;">NEXT</span></a></p></div> Downloading Python 2012-07-14T07:58:55+00:00 2012-07-14T07:58:55+00:00 http://www.cryptoden.com/index.php/python/6-downloading-python Super User mikejcowan@me.com <div class="feed-description"><h1><span style="font-family: verdana, geneva; font-size: small;">What is python?</span></h1> <p><span style="font-family: verdana, geneva; font-size: small;">Python is a programming language. The language is made up of instructions that you put in a program in order to tell your computer what to do. For example to get it to print ‘Hello’ on the screen, you just write:</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">print “Hello”</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">That’s all there is to programming! You have to learn the available instructions and the required way of typing them into a program. Then you press the ‘run’ button and the computer does as it’s told.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">Python is also the software that interprets your instructions into a form that the computer can understand. It is this software that you first have to download and install on your computer.</span></p> <h1><br /><span style="font-family: verdana, geneva; font-size: small;">Downloading python.</span></h1> <p><span style="font-family: verdana, geneva; font-size: small;">At the website http://www.python.org/download/ you will find downloads for both Windows and for Mac. The current version is 3.2.3 (April 2012).</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">After downloading, an icon will appear on your desktop. Double-click this to instal python, and if you’re operating system is Windows you’ll find a new folder ‘Python 25’ in your root directory and also in your Start|All Programs list. Click this latter folder, and you’ll find an icon called ‘Idle’ inside. Click on ‘Idle’ and python starts by putting a form called Python Shell on your screen.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">On the Mac, installation of python puts a folder ‘Python for Mac’ in your Applications folder and this contains ‘Idle’. Click it to get the Python Shell.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">If you type 2+2 in the Shell and press Enter, python displays 4. You can play such games. But better is to click File|New Window which will give you a form on which to write your first program. Type the two lines below, making sure you start at the left hand side:</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">print (‘hello’)</span><br /><span style="font-family: verdana, geneva; font-size: small;">print (2+2)</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">and press the key F5 to run this little program. After you give a file name (test1.py will do), and press Enter, your program will run, and display the result of your commands in Python Shell, just as shown below. The Shell is where the output of a program is displayed:</span></p> <p style="text-align: center;"><span style="font-family: verdana, geneva; font-size: small;"><img src="images/python3_jpg.jpg" border="0" alt="" /><br /></span></p> <p><span style="text-align: left; font-family: verdana, geneva; font-size: small;">The program code</span></p> <p style="text-align: left;"><span style="font-family: verdana, geneva; font-size: small;">print('hello')</span></p> <p style="text-align: left;"><span style="font-family: verdana, geneva; font-size: small;">prints the word 'hello'  and then a carriage return so that the next item to be printed starts on a new line. If you want to printing something else later on the same line then the command must be altered to</span></p> <p style="text-align: left;"><span style="font-family: verdana, geneva; font-size: small;">print('hello',end="")</span></p> <p style="text-align: left;"><span style="font-family: verdana, geneva; font-size: small;">and the printed line will end without a carriage return. This terminology is new to Python v3 and is, in my opinion, much clumsier than previously (which I won't bother you with) but sometimes complexity is the price of progress.</span></p> <p style="text-align: center;"><span style="font-family: verdana, geneva; font-size: small;"><a href="index.php/python/5-introduction">PREVIOUS</a>           <a href="index.php/python/15-enter-message">NEXT </a></span></p> <p style="text-align: left;"><span style="font-family: verdana, geneva; font-size: small;"> </span></p></div> <div class="feed-description"><h1><span style="font-family: verdana, geneva; font-size: small;">What is python?</span></h1> <p><span style="font-family: verdana, geneva; font-size: small;">Python is a programming language. The language is made up of instructions that you put in a program in order to tell your computer what to do. For example to get it to print ‘Hello’ on the screen, you just write:</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">print “Hello”</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">That’s all there is to programming! You have to learn the available instructions and the required way of typing them into a program. Then you press the ‘run’ button and the computer does as it’s told.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">Python is also the software that interprets your instructions into a form that the computer can understand. It is this software that you first have to download and install on your computer.</span></p> <h1><br /><span style="font-family: verdana, geneva; font-size: small;">Downloading python.</span></h1> <p><span style="font-family: verdana, geneva; font-size: small;">At the website http://www.python.org/download/ you will find downloads for both Windows and for Mac. The current version is 3.2.3 (April 2012).</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">After downloading, an icon will appear on your desktop. Double-click this to instal python, and if you’re operating system is Windows you’ll find a new folder ‘Python 25’ in your root directory and also in your Start|All Programs list. Click this latter folder, and you’ll find an icon called ‘Idle’ inside. Click on ‘Idle’ and python starts by putting a form called Python Shell on your screen.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">On the Mac, installation of python puts a folder ‘Python for Mac’ in your Applications folder and this contains ‘Idle’. Click it to get the Python Shell.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">If you type 2+2 in the Shell and press Enter, python displays 4. You can play such games. But better is to click File|New Window which will give you a form on which to write your first program. Type the two lines below, making sure you start at the left hand side:</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">print (‘hello’)</span><br /><span style="font-family: verdana, geneva; font-size: small;">print (2+2)</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">and press the key F5 to run this little program. After you give a file name (test1.py will do), and press Enter, your program will run, and display the result of your commands in Python Shell, just as shown below. The Shell is where the output of a program is displayed:</span></p> <p style="text-align: center;"><span style="font-family: verdana, geneva; font-size: small;"><img src="images/python3_jpg.jpg" border="0" alt="" /><br /></span></p> <p><span style="text-align: left; font-family: verdana, geneva; font-size: small;">The program code</span></p> <p style="text-align: left;"><span style="font-family: verdana, geneva; font-size: small;">print('hello')</span></p> <p style="text-align: left;"><span style="font-family: verdana, geneva; font-size: small;">prints the word 'hello'  and then a carriage return so that the next item to be printed starts on a new line. If you want to printing something else later on the same line then the command must be altered to</span></p> <p style="text-align: left;"><span style="font-family: verdana, geneva; font-size: small;">print('hello',end="")</span></p> <p style="text-align: left;"><span style="font-family: verdana, geneva; font-size: small;">and the printed line will end without a carriage return. This terminology is new to Python v3 and is, in my opinion, much clumsier than previously (which I won't bother you with) but sometimes complexity is the price of progress.</span></p> <p style="text-align: center;"><span style="font-family: verdana, geneva; font-size: small;"><a href="index.php/python/5-introduction">PREVIOUS</a>           <a href="index.php/python/15-enter-message">NEXT </a></span></p> <p style="text-align: left;"><span style="font-family: verdana, geneva; font-size: small;"> </span></p></div> Enter message 2012-07-16T04:39:37+00:00 2012-07-16T04:39:37+00:00 http://www.cryptoden.com/index.php/python/15-enter-message Super User mikejcowan@me.com <div class="feed-description"><h1 style="text-align: center;"><span>Entering the message to encipher</span></h1> <p><span style="font-family: verdana, geneva; font-size: small;">The enciphering program takes a message and, with a key, produces a cipher that will look meaningless, effectively hiding the message. Let’s say our message is:</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">I must go down to the seas again, for the call of the</span><br /><span style="font-family: verdana, geneva; font-size: small;">running tide is a wild call, and a clear call, that</span><br /><span style="font-family: verdana, geneva; font-size: small;">may not be denied.</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">We have to get this message into the computer. In Idle, we click on File|New Window</span></p> <p><span><img src="images/Python 2.jpg" border="0" alt="" /> </span></p> <p><span style="font-family: verdana, geneva; font-size: small;">to get a blank page on which to write the program. Now we type into our program a name, an equals sign and the message enclosed by triple inverted commas. I've chosen the name 'message' but you can choose almost any name you like, such as 'plaintext' or 'pt' or 'p49'.</span><br /><br /><span style="font-family: 'courier new', courier; font-size: small;">message = ‘’’I must go down to the seas again, for the call of the</span><br /><span style="font-family: 'courier new', courier; font-size: small;">running tide is a wild call, and a clear call, that</span><br /><span style="font-family: 'courier new', courier; font-size: small;">may not be denied.’’’</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">The message is called a string of letters. Each character is designated by an order number, beginning with zero. So message[0] is ‘I’, message [1] is a space, message[2] is ‘m’ and so on. This ability to designate a letter by its position in a string is an essential part of programming.</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">Instead of typing the message letter by letter, you can copy and paste it. Whichever way you do it, the message is long enough to cover several lines. We use triple inverted commas around the message to tell python that it’s all one string.</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">We can measure the length of this string using the python command len() in the following way:</span><br /><br /><span style="font-family: 'courier new', courier; font-size: small;">length= len(message)</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">This line counts the characters in the message, including the spaces, and puts the number in the entity I have called ‘length’. You could have chosen any other name for this entity that appeals to you, like ‘x’ or leng or len1. But you can’t call it ‘len’ because that is reserved as a command.</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">The name ‘length’ stands for a number and is called a ‘variable’. On my computer, length is 134. It may be different on yours if you’ve typed the message into fewer lines than me, and so have fewer spaces.</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">We can get the computer to display the message in Python Shell, character by character, with these commands:</span><br /><br /><span style="font-family: 'courier new', courier; font-size: small;">for j in range(length):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    print (message[j],end="")</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">You will notice that the first line ends with a colon ‘:’ and the second line is indented four spaces.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">The words of the first line mean: make the variable ‘j’ equal to zero, then make it 1, then 2 … all the way up to, but not including, the value of ‘length’ (which you’ll recall is the length of the cipher). The colon and the indentation mean: for every value in the first line carry out the instruction in the second line.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">In other words the instruction is print message[0], then print message[1], then message[2]... and so on up to message[length-1]; after that ‘j’ gets to the value of ‘length’ and the routine is finished.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">You can indent a whole series of actions to be carried out for every value of ‘j’. So we could write:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">for j in range(length):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    print (‘The value of j is = ’,end="")</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    print (j,end="")</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    print (‘ and the letter is ’,end="")</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    print (message[j])</span><br /><span style="font-family: 'courier new', courier; font-size: small;">print (‘All done’)</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">The computer would then print out:</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">The value of j = 0 and the letter is I</span><br /><span style="font-family: verdana, geneva; font-size: small;">The value of j = 1 and the letter is (this is the space) </span><br /><span style="font-family: verdana, geneva; font-size: small;">The value of j = 2 and the letter is m</span><br /><span style="font-family: verdana, geneva; font-size: small;">The value of j = 3 and the letter is u</span><br /><span style="font-family: verdana, geneva; font-size: small;">The value of j = 4 and the letter is s</span><br /><span style="font-family: verdana, geneva; font-size: small;">The value of j = 5 and the letter is t</span><br /><span style="font-family: verdana, geneva; font-size: small;">…all the way up to…</span><br /><span style="font-family: verdana, geneva; font-size: small;">The value of j = 133 and the letter is .</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">When ‘j’ gets to the value of length the routine is completed. The computer then goes on to the next command, which you can see is not indented, and prints out “All done”.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">A final comment – the comma at the end of a print command means ‘don’t end the line’. When the comma is omitted, python will print and then start a new line.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">Go ahead and write this program and run it and see what happens. </span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">The next step is to ensure all letters in the message are in lower case and that any punctuation is removed. Converting to lower case is easily done with the special python command:</span><br /><br /><span style="font-family: 'courier new', courier; font-size: small;">message = message.lower()</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">which takes all the steps necessary to convert any upper case letter in the string 'message' to its equivalent lower case letter.</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">To get rid of punctuation is a bit more complicated. We shall instruct the computer to take each character in the string 'message', then test to see whether it is a letter between 'a' and 'z', and if so to add it to a new string which I’ll call 'plain'. To do this I first have to tell python that 'plain' is a string, otherwise python could equally well think ‘plain’ is a number variable. A string is enclosed by an inverted comma at each end. In this case the string is empty so we write:</span><br /><br /><span style="font-family: 'courier new', courier; font-size: small;">plain = ‘’</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">Here’s the program code to extract the letters from ‘message’ and put them into ‘plain’. Pay particular attention to the indenting which must regularly be 4 spaces, and don’t forget the semi colons! The lines in red beginning with # are comments to help understand the program; the hash tells python they are not part of the program.</span><br /><br /><span style="font-family: 'courier new', courier; font-size: small; color: #d84a26;">#..declare ‘plain’ as an empty string..</span><br /><span style="font-family: 'courier new', courier; font-size: small;">plain=’’ </span><br /><span style="font-family: 'courier new', courier; font-size: small; color: #d84a26;">#..take each letter of message &amp; if it's between a and z add it to</span><br /><span style="font-family: 'courier new', courier; font-size: small; color: #d84a26;"># the new string 'plain'.</span><br /><span style="font-family: 'courier new', courier; font-size: small;">for j in range(length):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    if (message[j] &gt;='a' and message[j]&lt;='z'):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">        plain = plain + message[j]</span><br /><span style="font-family: 'courier new', courier; font-size: small; color: #d84a26;">#...display the list called ‘plain’ in the Shell &amp; its length..</span><br /><span style="font-family: 'courier new', courier; font-size: small;">print ("plain= ",end="")</span><br /><span style="font-family: 'courier new', courier; font-size: small;">for j in range(length):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    print (plain[j],end="")</span><br /><span style="font-family: 'courier new', courier; font-size: small;">print ("length=",length)</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">Add these lines to your program.  The complete program is given in <a href="index.php/python/14-appendix">Appendix</a> 1 and you can copy it from there if you like. <span>Save the program with File|Save and run it by pressing the F5 key or with File|Run.</span></span></p> <p><span style="font-family: verdana, geneva; font-size: small;">Now the punctuation and spaces between words have gone, the letters are all lower case and the length has shrunk appropriately to 93. Here is the output you will get:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">plain=imustgodowntotheseasagainforthecalloftherunningtideisawildcall</span><span style="font-family: 'courier new', courier; font-size: small;">andaclearcallthatmaynotbedenied</span><br /><span style="font-family: 'courier new', courier; font-size: small;">length= 93</span></p> <p style="text-align: center;"><span style="font-family: 'courier new', courier; font-size: small;"><a href="index.php/python/6-downloading-python">PREVIOUS</a>          <a href="index.php/python/7-enciphering">NEXT</a></span></p></div> <div class="feed-description"><h1 style="text-align: center;"><span>Entering the message to encipher</span></h1> <p><span style="font-family: verdana, geneva; font-size: small;">The enciphering program takes a message and, with a key, produces a cipher that will look meaningless, effectively hiding the message. Let’s say our message is:</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">I must go down to the seas again, for the call of the</span><br /><span style="font-family: verdana, geneva; font-size: small;">running tide is a wild call, and a clear call, that</span><br /><span style="font-family: verdana, geneva; font-size: small;">may not be denied.</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">We have to get this message into the computer. In Idle, we click on File|New Window</span></p> <p><span><img src="images/Python 2.jpg" border="0" alt="" /> </span></p> <p><span style="font-family: verdana, geneva; font-size: small;">to get a blank page on which to write the program. Now we type into our program a name, an equals sign and the message enclosed by triple inverted commas. I've chosen the name 'message' but you can choose almost any name you like, such as 'plaintext' or 'pt' or 'p49'.</span><br /><br /><span style="font-family: 'courier new', courier; font-size: small;">message = ‘’’I must go down to the seas again, for the call of the</span><br /><span style="font-family: 'courier new', courier; font-size: small;">running tide is a wild call, and a clear call, that</span><br /><span style="font-family: 'courier new', courier; font-size: small;">may not be denied.’’’</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">The message is called a string of letters. Each character is designated by an order number, beginning with zero. So message[0] is ‘I’, message [1] is a space, message[2] is ‘m’ and so on. This ability to designate a letter by its position in a string is an essential part of programming.</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">Instead of typing the message letter by letter, you can copy and paste it. Whichever way you do it, the message is long enough to cover several lines. We use triple inverted commas around the message to tell python that it’s all one string.</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">We can measure the length of this string using the python command len() in the following way:</span><br /><br /><span style="font-family: 'courier new', courier; font-size: small;">length= len(message)</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">This line counts the characters in the message, including the spaces, and puts the number in the entity I have called ‘length’. You could have chosen any other name for this entity that appeals to you, like ‘x’ or leng or len1. But you can’t call it ‘len’ because that is reserved as a command.</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">The name ‘length’ stands for a number and is called a ‘variable’. On my computer, length is 134. It may be different on yours if you’ve typed the message into fewer lines than me, and so have fewer spaces.</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">We can get the computer to display the message in Python Shell, character by character, with these commands:</span><br /><br /><span style="font-family: 'courier new', courier; font-size: small;">for j in range(length):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    print (message[j],end="")</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">You will notice that the first line ends with a colon ‘:’ and the second line is indented four spaces.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">The words of the first line mean: make the variable ‘j’ equal to zero, then make it 1, then 2 … all the way up to, but not including, the value of ‘length’ (which you’ll recall is the length of the cipher). The colon and the indentation mean: for every value in the first line carry out the instruction in the second line.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">In other words the instruction is print message[0], then print message[1], then message[2]... and so on up to message[length-1]; after that ‘j’ gets to the value of ‘length’ and the routine is finished.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">You can indent a whole series of actions to be carried out for every value of ‘j’. So we could write:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">for j in range(length):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    print (‘The value of j is = ’,end="")</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    print (j,end="")</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    print (‘ and the letter is ’,end="")</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    print (message[j])</span><br /><span style="font-family: 'courier new', courier; font-size: small;">print (‘All done’)</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">The computer would then print out:</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">The value of j = 0 and the letter is I</span><br /><span style="font-family: verdana, geneva; font-size: small;">The value of j = 1 and the letter is (this is the space) </span><br /><span style="font-family: verdana, geneva; font-size: small;">The value of j = 2 and the letter is m</span><br /><span style="font-family: verdana, geneva; font-size: small;">The value of j = 3 and the letter is u</span><br /><span style="font-family: verdana, geneva; font-size: small;">The value of j = 4 and the letter is s</span><br /><span style="font-family: verdana, geneva; font-size: small;">The value of j = 5 and the letter is t</span><br /><span style="font-family: verdana, geneva; font-size: small;">…all the way up to…</span><br /><span style="font-family: verdana, geneva; font-size: small;">The value of j = 133 and the letter is .</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">When ‘j’ gets to the value of length the routine is completed. The computer then goes on to the next command, which you can see is not indented, and prints out “All done”.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">A final comment – the comma at the end of a print command means ‘don’t end the line’. When the comma is omitted, python will print and then start a new line.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">Go ahead and write this program and run it and see what happens. </span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">The next step is to ensure all letters in the message are in lower case and that any punctuation is removed. Converting to lower case is easily done with the special python command:</span><br /><br /><span style="font-family: 'courier new', courier; font-size: small;">message = message.lower()</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">which takes all the steps necessary to convert any upper case letter in the string 'message' to its equivalent lower case letter.</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">To get rid of punctuation is a bit more complicated. We shall instruct the computer to take each character in the string 'message', then test to see whether it is a letter between 'a' and 'z', and if so to add it to a new string which I’ll call 'plain'. To do this I first have to tell python that 'plain' is a string, otherwise python could equally well think ‘plain’ is a number variable. A string is enclosed by an inverted comma at each end. In this case the string is empty so we write:</span><br /><br /><span style="font-family: 'courier new', courier; font-size: small;">plain = ‘’</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">Here’s the program code to extract the letters from ‘message’ and put them into ‘plain’. Pay particular attention to the indenting which must regularly be 4 spaces, and don’t forget the semi colons! The lines in red beginning with # are comments to help understand the program; the hash tells python they are not part of the program.</span><br /><br /><span style="font-family: 'courier new', courier; font-size: small; color: #d84a26;">#..declare ‘plain’ as an empty string..</span><br /><span style="font-family: 'courier new', courier; font-size: small;">plain=’’ </span><br /><span style="font-family: 'courier new', courier; font-size: small; color: #d84a26;">#..take each letter of message &amp; if it's between a and z add it to</span><br /><span style="font-family: 'courier new', courier; font-size: small; color: #d84a26;"># the new string 'plain'.</span><br /><span style="font-family: 'courier new', courier; font-size: small;">for j in range(length):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    if (message[j] &gt;='a' and message[j]&lt;='z'):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">        plain = plain + message[j]</span><br /><span style="font-family: 'courier new', courier; font-size: small; color: #d84a26;">#...display the list called ‘plain’ in the Shell &amp; its length..</span><br /><span style="font-family: 'courier new', courier; font-size: small;">print ("plain= ",end="")</span><br /><span style="font-family: 'courier new', courier; font-size: small;">for j in range(length):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    print (plain[j],end="")</span><br /><span style="font-family: 'courier new', courier; font-size: small;">print ("length=",length)</span><br /><br /><span style="font-family: verdana, geneva; font-size: small;">Add these lines to your program.  The complete program is given in <a href="index.php/python/14-appendix">Appendix</a> 1 and you can copy it from there if you like. <span>Save the program with File|Save and run it by pressing the F5 key or with File|Run.</span></span></p> <p><span style="font-family: verdana, geneva; font-size: small;">Now the punctuation and spaces between words have gone, the letters are all lower case and the length has shrunk appropriately to 93. Here is the output you will get:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">plain=imustgodowntotheseasagainforthecalloftherunningtideisawildcall</span><span style="font-family: 'courier new', courier; font-size: small;">andaclearcallthatmaynotbedenied</span><br /><span style="font-family: 'courier new', courier; font-size: small;">length= 93</span></p> <p style="text-align: center;"><span style="font-family: 'courier new', courier; font-size: small;"><a href="index.php/python/6-downloading-python">PREVIOUS</a>          <a href="index.php/python/7-enciphering">NEXT</a></span></p></div> Enciphering 2012-07-14T08:24:03+00:00 2012-07-14T08:24:03+00:00 http://www.cryptoden.com/index.php/python/7-enciphering Super User mikejcowan@me.com <div class="feed-description"><h1 style="text-align: center;"><span style="font-family: verdana, geneva; font-size: small;"> <span style="font-size: medium;">Making a program to encipher by substitution</span></span></h1> <h3 style="text-align: left;"><span style="font-family: verdana, geneva; font-size: small; color: #000080;"> </span></h3> <p><span style="font-family: verdana, geneva; font-size: small;">We have just used two of the most important basic routines in python:</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">1. repetition:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">for x in range(y):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    do something</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">2. conditional testing. We used above</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">i<span style="font-family: 'courier new', courier;">f (message[j] &gt;='a' and message[j]&lt;='z'):</span></span></p> <p><span style="font-family: verdana, geneva; font-size: small;">which means “if message[j] is greater than or equal to ‘a’ and if message[j] is less than or equal to ‘z’ “</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">Here are some other examples:</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">(a)   if x is greater than y    if ( x &gt; y):</span><br /><span style="font-family: verdana, geneva; font-size: small;">(b)   if x equals y               if (x==y):</span><br /><span style="font-family: verdana, geneva; font-size: small;">(c)   if x is less than y        if (x&lt;y):</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">x and y stand for numerical variables. If they stand for letters then they must be enclosed in inverted commas, as in the program.</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">You might like to make a few trial programs of your own with these routines to get the hang of it. For example this will print out 0 to 9:</span><br /> <br /><span style="font-family: 'courier new', courier; font-size: small;">for k in range(10):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    (print k,end="")</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">The little program below will print the letters of the alphabet. The computer recognizes characters as a numbered list where 'A' is at 65, 'B' at 66, ‘C’ at 67 .. and Z at 90. The line below instructs: "for the variable ‘m’ starting at 65, ending at 90 and moving forward by 1 print out the relevant character".</span><br /> <br /><span style="font-family: 'courier new', courier; font-size: small;">for m in range(65,91,1):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    (print chr(m),end="")</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">Similarly the lower case letters are numbered from 97 for 'a' to 122 for 'z'</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">Here's a little conditional program to try. If 'x' is less than 5, it will print 'less', otherwise it will print 'more'.</span><br /> <br /><span style="font-family: 'courier new', courier; font-size: small;">for x in range (10):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    if(x&lt;5):</span><br /><span style="font-family: 'courier new', courier; font-size: small;"> print ("less ",end="")</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    else:</span><br /><span style="font-family: 'courier new', courier; font-size: small;"> print ("more ",end="")</span><br /><br /></p> <h3><span style="font-family: verdana, geneva; font-size: small; color: #000080;">An enciphering program</span></h3> <p><span style="font-family: verdana, geneva; font-size: small;">Enciphering is done with a key which often is an easy-to-remenber word like HYPNOTIZABLE.   The rest of the alphabet is then added on, in order: HYPNOTIZABLECDFGJKMQRSUVWX. </span></p> <p><span style="font-family: verdana, geneva; font-size: small;">This string, that I will call ‘key’, represents the cipher letters. It is then set against a plain alphabet to represent the plain letters, like this:</span><br /> <br /><span style="font-family: 'courier new', courier; font-size: small;">key:      HYPNOTIZABLECDFGJKMQRSUVWX</span><br /><span style="font-family: 'courier new', courier; font-size: small;">alphabet: abcdefghijklmnopqrstuvwxyz</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">So plain 'a' will be enciphered to 'H', plain 'b' to 'Y' and so on.</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">Following this method for the first 13 letters of ‘plain’</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">i m u s t g o d o w n t o</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">we get cipher letters as below:</span><br /><span style="font-family: verdana, geneva; font-size: small;">ACRMQ IFNFU DQF</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">To mimic this process, the computer must</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">-start with an empty string called cipher</span><br /><span style="font-family: verdana, geneva; font-size: small;"> -take the first plain letter 'i'</span><br /><span style="font-family: verdana, geneva; font-size: small;"> -go through the alphabet, letter by letter, until it finds 'i'</span><br /><span style="font-family: verdana, geneva; font-size: small;"> -note the position of 'i' and get the cipher letter in the same position, which is 'A'</span><br /><span style="font-family: verdana, geneva; font-size: small;"> -add 'A' to the string 'cipher'</span><br /><span style="font-family: verdana, geneva; font-size: small;"> -repeat this process for all the other letters in the message.</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">The string 'cipher' will then hold the encrypted message.</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">Python provides a command to go through ‘alphabet’ and find the position of a letter. To find the position of ‘i’:</span><br /> <br /><span style="font-family: 'courier new', courier; font-size: small;">position=alphabet.index(‘i’)</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">Using this command we write the following programming lines to encipher ‘plain’:</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small; color: #e54219;">#...declare ‘cipher’ as an empty string…</span><br /><span style="font-family: 'courier new', courier; font-size: small;">cipher = ‘’</span><br /><span style="font-family: 'courier new', courier; font-size: small;">key = 'HYPNOTIZABLECDFGJKMQRSUVWX'</span><br /><span style="font-family: 'courier new', courier; font-size: small;">alphabet='abcdefghijklmnopqrstuvwxyz'</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small; color: #e54219;">#..for each plain letter in a string ‘length’ long..</span><br /><span style="font-family: 'courier new', courier; font-size: small;">for j in range(length):</span><br /><span style="font-family: verdana, geneva; font-size: small;"><span style="color: #e54219;"> #....find position of plain letter in alphabet</span> </span><br /><span style="font-family: 'courier new', courier; font-size: small;"> position=key.index(plain[j])</span><br /><span style="font-family: verdana, geneva; font-size: small; color: #e54219;"> #...add corresponding key letter to ‘cipher’</span><br /><span style="font-family: 'courier new', courier; font-size: small;"> cipher = cipher + key[position]</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">It's usual to print out ciphertext in 5-letter blocks. This is done by adding a few more lines of program code to put a space in the string after every 5 letters:</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small; color: #e54219;">#...declare the new string ‘cipher’...</span><br /><span style="font-family: 'courier new', courier; font-size: small;">cipher = ''</span><br /><span style="font-family: verdana, geneva; font-size: small; color: #e54219;">#.. declare the variable ‘n’, to be used as a counter</span><br /><span style="font-family: verdana, geneva; font-size: small;"><span style="font-family: 'courier new', courier;">n = 0</span> </span><br /><span style="font-family: verdana, geneva; font-size: small; color: #e54219;">#..for each plain letter up to ‘length’..</span><br /><span style="font-family: 'courier new', courier; font-size: small;">for j in range(length):</span><br /><span style="font-family: verdana, geneva; font-size: small;"><span style="color: #e54219;"> #...find position of plain letter in alphabet</span> </span><br /><span style="font-family: verdana, geneva; font-size: small;"><span style="font-family: verdana, geneva;">     </span><span style="font-family: 'courier new', courier;">position=key.index(plain[j])</span></span><br /><span style="font-family: verdana, geneva; font-size: small; color: #e54219;"> #...add corresponding key letter to ‘cipher’</span><br /><span style="font-family: verdana, geneva; font-size: small;">     <span style="font-family: 'courier new', courier;">cipher = cipher + key[position]</span></span><br /><span style="font-family: verdana, geneva; font-size: small; color: #e54219;"> #...increment counter. If counter = 5 add a space</span><br /><span style="font-family: verdana, geneva; font-size: small; color: #e54219;"> # and reset counter to zero</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    n = n + 1</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    if (n==5):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">        cipher = cipher + ‘ ‘</span><br /><span style="font-family: verdana, geneva; font-size: small;"><span style="font-family: 'courier new', courier;">        n=0</span> </span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">Here’s a new, and simpler, way of displaying the string ‘cipher’ in the Shell, and you will notice it is much faster than printing the letters one by one:</span><br /> <br /><span style="font-family: 'courier new', courier; font-size: small;">print (cipher)</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">When you run the complete enciphering program (<a href="index.php/python/14-appendix">Appendix</a> 2) you will get the ciphertext:</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">ACRMQ IFNFU DQFQZ OMOHM HIHAD TFKQZ OPHEE FTQZO KRDDA DIQAN OAMHU AENPH EEHDN HPEOH KPHEE QZHQC HWDFQ YONOD AON</span><br /> <br /> <br /><span style="font-family: verdana, geneva; font-size: small;">There we are, our first program is complete. You now have a simple but effective enciphering tool.</span></p> <p style="text-align: center;"><span style="font-family: verdana, geneva; font-size: small;"><a href="index.php/python/15-enter-message">PREVIOUS</a>          <a href="index.php/python/8-deciphering">NEXT</a></span></p></div> <div class="feed-description"><h1 style="text-align: center;"><span style="font-family: verdana, geneva; font-size: small;"> <span style="font-size: medium;">Making a program to encipher by substitution</span></span></h1> <h3 style="text-align: left;"><span style="font-family: verdana, geneva; font-size: small; color: #000080;"> </span></h3> <p><span style="font-family: verdana, geneva; font-size: small;">We have just used two of the most important basic routines in python:</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">1. repetition:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">for x in range(y):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    do something</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">2. conditional testing. We used above</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">i<span style="font-family: 'courier new', courier;">f (message[j] &gt;='a' and message[j]&lt;='z'):</span></span></p> <p><span style="font-family: verdana, geneva; font-size: small;">which means “if message[j] is greater than or equal to ‘a’ and if message[j] is less than or equal to ‘z’ “</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">Here are some other examples:</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">(a)   if x is greater than y    if ( x &gt; y):</span><br /><span style="font-family: verdana, geneva; font-size: small;">(b)   if x equals y               if (x==y):</span><br /><span style="font-family: verdana, geneva; font-size: small;">(c)   if x is less than y        if (x&lt;y):</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">x and y stand for numerical variables. If they stand for letters then they must be enclosed in inverted commas, as in the program.</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">You might like to make a few trial programs of your own with these routines to get the hang of it. For example this will print out 0 to 9:</span><br /> <br /><span style="font-family: 'courier new', courier; font-size: small;">for k in range(10):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    (print k,end="")</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">The little program below will print the letters of the alphabet. The computer recognizes characters as a numbered list where 'A' is at 65, 'B' at 66, ‘C’ at 67 .. and Z at 90. The line below instructs: "for the variable ‘m’ starting at 65, ending at 90 and moving forward by 1 print out the relevant character".</span><br /> <br /><span style="font-family: 'courier new', courier; font-size: small;">for m in range(65,91,1):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    (print chr(m),end="")</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">Similarly the lower case letters are numbered from 97 for 'a' to 122 for 'z'</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">Here's a little conditional program to try. If 'x' is less than 5, it will print 'less', otherwise it will print 'more'.</span><br /> <br /><span style="font-family: 'courier new', courier; font-size: small;">for x in range (10):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    if(x&lt;5):</span><br /><span style="font-family: 'courier new', courier; font-size: small;"> print ("less ",end="")</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    else:</span><br /><span style="font-family: 'courier new', courier; font-size: small;"> print ("more ",end="")</span><br /><br /></p> <h3><span style="font-family: verdana, geneva; font-size: small; color: #000080;">An enciphering program</span></h3> <p><span style="font-family: verdana, geneva; font-size: small;">Enciphering is done with a key which often is an easy-to-remenber word like HYPNOTIZABLE.   The rest of the alphabet is then added on, in order: HYPNOTIZABLECDFGJKMQRSUVWX. </span></p> <p><span style="font-family: verdana, geneva; font-size: small;">This string, that I will call ‘key’, represents the cipher letters. It is then set against a plain alphabet to represent the plain letters, like this:</span><br /> <br /><span style="font-family: 'courier new', courier; font-size: small;">key:      HYPNOTIZABLECDFGJKMQRSUVWX</span><br /><span style="font-family: 'courier new', courier; font-size: small;">alphabet: abcdefghijklmnopqrstuvwxyz</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">So plain 'a' will be enciphered to 'H', plain 'b' to 'Y' and so on.</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">Following this method for the first 13 letters of ‘plain’</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">i m u s t g o d o w n t o</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">we get cipher letters as below:</span><br /><span style="font-family: verdana, geneva; font-size: small;">ACRMQ IFNFU DQF</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">To mimic this process, the computer must</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">-start with an empty string called cipher</span><br /><span style="font-family: verdana, geneva; font-size: small;"> -take the first plain letter 'i'</span><br /><span style="font-family: verdana, geneva; font-size: small;"> -go through the alphabet, letter by letter, until it finds 'i'</span><br /><span style="font-family: verdana, geneva; font-size: small;"> -note the position of 'i' and get the cipher letter in the same position, which is 'A'</span><br /><span style="font-family: verdana, geneva; font-size: small;"> -add 'A' to the string 'cipher'</span><br /><span style="font-family: verdana, geneva; font-size: small;"> -repeat this process for all the other letters in the message.</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">The string 'cipher' will then hold the encrypted message.</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">Python provides a command to go through ‘alphabet’ and find the position of a letter. To find the position of ‘i’:</span><br /> <br /><span style="font-family: 'courier new', courier; font-size: small;">position=alphabet.index(‘i’)</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">Using this command we write the following programming lines to encipher ‘plain’:</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small; color: #e54219;">#...declare ‘cipher’ as an empty string…</span><br /><span style="font-family: 'courier new', courier; font-size: small;">cipher = ‘’</span><br /><span style="font-family: 'courier new', courier; font-size: small;">key = 'HYPNOTIZABLECDFGJKMQRSUVWX'</span><br /><span style="font-family: 'courier new', courier; font-size: small;">alphabet='abcdefghijklmnopqrstuvwxyz'</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small; color: #e54219;">#..for each plain letter in a string ‘length’ long..</span><br /><span style="font-family: 'courier new', courier; font-size: small;">for j in range(length):</span><br /><span style="font-family: verdana, geneva; font-size: small;"><span style="color: #e54219;"> #....find position of plain letter in alphabet</span> </span><br /><span style="font-family: 'courier new', courier; font-size: small;"> position=key.index(plain[j])</span><br /><span style="font-family: verdana, geneva; font-size: small; color: #e54219;"> #...add corresponding key letter to ‘cipher’</span><br /><span style="font-family: 'courier new', courier; font-size: small;"> cipher = cipher + key[position]</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">It's usual to print out ciphertext in 5-letter blocks. This is done by adding a few more lines of program code to put a space in the string after every 5 letters:</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small; color: #e54219;">#...declare the new string ‘cipher’...</span><br /><span style="font-family: 'courier new', courier; font-size: small;">cipher = ''</span><br /><span style="font-family: verdana, geneva; font-size: small; color: #e54219;">#.. declare the variable ‘n’, to be used as a counter</span><br /><span style="font-family: verdana, geneva; font-size: small;"><span style="font-family: 'courier new', courier;">n = 0</span> </span><br /><span style="font-family: verdana, geneva; font-size: small; color: #e54219;">#..for each plain letter up to ‘length’..</span><br /><span style="font-family: 'courier new', courier; font-size: small;">for j in range(length):</span><br /><span style="font-family: verdana, geneva; font-size: small;"><span style="color: #e54219;"> #...find position of plain letter in alphabet</span> </span><br /><span style="font-family: verdana, geneva; font-size: small;"><span style="font-family: verdana, geneva;">     </span><span style="font-family: 'courier new', courier;">position=key.index(plain[j])</span></span><br /><span style="font-family: verdana, geneva; font-size: small; color: #e54219;"> #...add corresponding key letter to ‘cipher’</span><br /><span style="font-family: verdana, geneva; font-size: small;">     <span style="font-family: 'courier new', courier;">cipher = cipher + key[position]</span></span><br /><span style="font-family: verdana, geneva; font-size: small; color: #e54219;"> #...increment counter. If counter = 5 add a space</span><br /><span style="font-family: verdana, geneva; font-size: small; color: #e54219;"> # and reset counter to zero</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    n = n + 1</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    if (n==5):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">        cipher = cipher + ‘ ‘</span><br /><span style="font-family: verdana, geneva; font-size: small;"><span style="font-family: 'courier new', courier;">        n=0</span> </span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">Here’s a new, and simpler, way of displaying the string ‘cipher’ in the Shell, and you will notice it is much faster than printing the letters one by one:</span><br /> <br /><span style="font-family: 'courier new', courier; font-size: small;">print (cipher)</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">When you run the complete enciphering program (<a href="index.php/python/14-appendix">Appendix</a> 2) you will get the ciphertext:</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">ACRMQ IFNFU DQFQZ OMOHM HIHAD TFKQZ OPHEE FTQZO KRDDA DIQAN OAMHU AENPH EEHDN HPEOH KPHEE QZHQC HWDFQ YONOD AON</span><br /> <br /> <br /><span style="font-family: verdana, geneva; font-size: small;">There we are, our first program is complete. You now have a simple but effective enciphering tool.</span></p> <p style="text-align: center;"><span style="font-family: verdana, geneva; font-size: small;"><a href="index.php/python/15-enter-message">PREVIOUS</a>          <a href="index.php/python/8-deciphering">NEXT</a></span></p></div> Deciphering 2012-07-14T10:04:47+00:00 2012-07-14T10:04:47+00:00 http://www.cryptoden.com/index.php/python/8-deciphering Super User mikejcowan@me.com <div class="feed-description"><h1 style="text-align: center;"><span style="font-family: verdana, geneva; font-size: medium;">Making a program to decipher by substitution.</span></h1> <p><span style="font-family: verdana, geneva; font-size: small;">Deciphering is the reverse of enciphering, so we can use the same programming techniques that we've already learned. The design of the program is called an algorithm, and it's useful to write this out before starting to program. </span></p> <p><br /><span style="font-family: verdana, geneva; font-size: small;">You begin the program with declarations of the cipher and also ‘plain’, ‘key’ and ‘alphabet’ like this:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">cipher=’’’ACRMQ IFNFU DQFQZ OMOHM HIHAD TFKQZ OPHEE FTQZO KRDDA DIQAN OAMHU AENPH EEHDN HPEOH KPHEE QZHQC HWDFQ YONOD AON’’’</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">key = 'HYPNOTIZABLECDFGJKMQRSUVWX'</span><br /><span style="font-family: 'courier new', courier; font-size: small;">alphabet = 'abcdefghijklmnopqrstuvwxyz'</span><br /><span style="font-family: 'courier new', courier; font-size: small;">plain = ‘’</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">Now the algorithm:</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">    remove spaces from ciphertext</span><br /><span style="font-family: verdana, geneva; font-size: small;">    take each character in ciphertext:</span><br /><span style="font-family: verdana, geneva; font-size: small;">        if it is not a space:</span><br /><span style="font-family: verdana, geneva; font-size: small;">            find its position in the key</span><br /><span style="font-family: verdana, geneva; font-size: small;">            find letter in same position in alphabet</span><br /><span style="font-family: verdana, geneva; font-size: small;">            add alphabet letter to the string ‘plain’</span><br /><span style="font-family: verdana, geneva; font-size: small;">    print out ‘plain’ in Python Shell</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">The full program is shown in <a href="index.php/python/14-appendix">Appendix</a> 3.  When you run the program of course you get back the plaintext, but without spaces at the end of each word:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">imustgodowntotheseasagainforthecalloftherunningtideisawildcallandaclearcallthatmaynotbedenied</span></p> <p style="text-align: center;"><span style="font-family: 'courier new', courier; font-size: small;"><a href="index.php/python/7-enciphering">PREVIOUS</a>          <a href="index.php/python/10-keyboard-input">NEXT</a></span></p></div> <div class="feed-description"><h1 style="text-align: center;"><span style="font-family: verdana, geneva; font-size: medium;">Making a program to decipher by substitution.</span></h1> <p><span style="font-family: verdana, geneva; font-size: small;">Deciphering is the reverse of enciphering, so we can use the same programming techniques that we've already learned. The design of the program is called an algorithm, and it's useful to write this out before starting to program. </span></p> <p><br /><span style="font-family: verdana, geneva; font-size: small;">You begin the program with declarations of the cipher and also ‘plain’, ‘key’ and ‘alphabet’ like this:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">cipher=’’’ACRMQ IFNFU DQFQZ OMOHM HIHAD TFKQZ OPHEE FTQZO KRDDA DIQAN OAMHU AENPH EEHDN HPEOH KPHEE QZHQC HWDFQ YONOD AON’’’</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">key = 'HYPNOTIZABLECDFGJKMQRSUVWX'</span><br /><span style="font-family: 'courier new', courier; font-size: small;">alphabet = 'abcdefghijklmnopqrstuvwxyz'</span><br /><span style="font-family: 'courier new', courier; font-size: small;">plain = ‘’</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">Now the algorithm:</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">    remove spaces from ciphertext</span><br /><span style="font-family: verdana, geneva; font-size: small;">    take each character in ciphertext:</span><br /><span style="font-family: verdana, geneva; font-size: small;">        if it is not a space:</span><br /><span style="font-family: verdana, geneva; font-size: small;">            find its position in the key</span><br /><span style="font-family: verdana, geneva; font-size: small;">            find letter in same position in alphabet</span><br /><span style="font-family: verdana, geneva; font-size: small;">            add alphabet letter to the string ‘plain’</span><br /><span style="font-family: verdana, geneva; font-size: small;">    print out ‘plain’ in Python Shell</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">The full program is shown in <a href="index.php/python/14-appendix">Appendix</a> 3.  When you run the program of course you get back the plaintext, but without spaces at the end of each word:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">imustgodowntotheseasagainforthecalloftherunningtideisawildcallandaclearcallthatmaynotbedenied</span></p> <p style="text-align: center;"><span style="font-family: 'courier new', courier; font-size: small;"><a href="index.php/python/7-enciphering">PREVIOUS</a>          <a href="index.php/python/10-keyboard-input">NEXT</a></span></p></div> Keyboard Input 2012-07-14T10:18:30+00:00 2012-07-14T10:18:30+00:00 http://www.cryptoden.com/index.php/python/10-keyboard-input Super User mikejcowan@me.com <div class="feed-description"><h1 style="text-align: center;"><span style="font-family: verdana, geneva; font-size: medium;">Input data to the program from the keyboard.</span></h1> <p><span style="font-family: verdana, geneva; font-size: small;">Another way of inputting data is straight from the keyboard after starting the program. This is convenient for short strings like the keyword ‘HYPNOTIZABLE’. In this case the program uses the python command 'raw_input()', which stops the program running and asks for the keyword. After you’ve typed it and hit the Return key, the program gets going again.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">Here's an example that stops the program, prints in the Shell the message ”Enter the key &amp; press Return”. The program then waits while you type the key, and then prints it out in the Shell.</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">key = input("Type the key &amp; press Return "); </span><br /><span style="font-family: 'courier new', courier; font-size: small;">for j in range(len(key)):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    print (key[j],)</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">It would be a good idea to check for mistakes, such as typing a punctuation mark or a number rather than a key letter, and repeating the input request if a mistake is found.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">This step of repeating a section of program is done using the python command ‘while()’ at the outset. So if I have a number ‘x’ and I want to keep adding 1 to it until it reaches 5, my program would be:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">x=0;</span><br /><span style="font-family: 'courier new', courier; font-size: small;">while(x&lt;5):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    x=x+1</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    print x,</span><br /><span style="font-family: 'courier new', courier; font-size: small;">print “all done”</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">The program will keep doing the indented section, incrementing ‘x’ and printing it, until ‘x’ gets to 5 when it will print “all done”. Try it out for yourself.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">Now, coming back to our inputting 'HYPNOTIZABLE', we want to ask for the key, check it’s OK and, if it’s not, repeat the request:</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">    program sets a variable ‘got_key’ = zero</span><br /><span style="font-family: verdana, geneva; font-size: small;">    program asks for keyword</span><br /><span style="font-family: verdana, geneva; font-size: small;">    human inputs keyword</span><br /><span style="font-family: verdana, geneva; font-size: small;">    program puts got_key = 1</span><br /><span style="font-family: verdana, geneva; font-size: small;">    program looks at each character of keyword:</span><br /><span style="font-family: verdana, geneva; font-size: small;">        if the character is lowercase, make it upper case </span><br /><span style="font-family: verdana, geneva; font-size: small;">        if the character is not A to Z make got_key=0</span><br /><span style="font-family: verdana, geneva; font-size: small;">    if got_key=0 go back to ‘program asks for keyword’</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">These program lines carry out the task:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">got_key=0</span><br /><span style="font-family: 'courier new', courier; font-size: small;">while got_key==0:</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    key = input("Enter the key &amp; press Return ");</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    key=key.upper()</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    got_key=1</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    for j in range(len(key)):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">        if(key[j]&lt;'A' or key[j]&gt;'Z'):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">            got_key=0</span><br /><span style="font-family: 'courier new', courier; font-size: small;">print(key)</span></p> <p style="text-align: center;"><span style="font-family: 'courier new', courier; font-size: small;"><a href="index.php/python/8-deciphering">PREVIOUS</a>          <a href="index.php/python/9-input-output">NEXT</a></span></p></div> <div class="feed-description"><h1 style="text-align: center;"><span style="font-family: verdana, geneva; font-size: medium;">Input data to the program from the keyboard.</span></h1> <p><span style="font-family: verdana, geneva; font-size: small;">Another way of inputting data is straight from the keyboard after starting the program. This is convenient for short strings like the keyword ‘HYPNOTIZABLE’. In this case the program uses the python command 'raw_input()', which stops the program running and asks for the keyword. After you’ve typed it and hit the Return key, the program gets going again.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">Here's an example that stops the program, prints in the Shell the message ”Enter the key &amp; press Return”. The program then waits while you type the key, and then prints it out in the Shell.</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">key = input("Type the key &amp; press Return "); </span><br /><span style="font-family: 'courier new', courier; font-size: small;">for j in range(len(key)):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    print (key[j],)</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">It would be a good idea to check for mistakes, such as typing a punctuation mark or a number rather than a key letter, and repeating the input request if a mistake is found.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">This step of repeating a section of program is done using the python command ‘while()’ at the outset. So if I have a number ‘x’ and I want to keep adding 1 to it until it reaches 5, my program would be:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">x=0;</span><br /><span style="font-family: 'courier new', courier; font-size: small;">while(x&lt;5):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    x=x+1</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    print x,</span><br /><span style="font-family: 'courier new', courier; font-size: small;">print “all done”</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">The program will keep doing the indented section, incrementing ‘x’ and printing it, until ‘x’ gets to 5 when it will print “all done”. Try it out for yourself.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">Now, coming back to our inputting 'HYPNOTIZABLE', we want to ask for the key, check it’s OK and, if it’s not, repeat the request:</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">    program sets a variable ‘got_key’ = zero</span><br /><span style="font-family: verdana, geneva; font-size: small;">    program asks for keyword</span><br /><span style="font-family: verdana, geneva; font-size: small;">    human inputs keyword</span><br /><span style="font-family: verdana, geneva; font-size: small;">    program puts got_key = 1</span><br /><span style="font-family: verdana, geneva; font-size: small;">    program looks at each character of keyword:</span><br /><span style="font-family: verdana, geneva; font-size: small;">        if the character is lowercase, make it upper case </span><br /><span style="font-family: verdana, geneva; font-size: small;">        if the character is not A to Z make got_key=0</span><br /><span style="font-family: verdana, geneva; font-size: small;">    if got_key=0 go back to ‘program asks for keyword’</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">These program lines carry out the task:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">got_key=0</span><br /><span style="font-family: 'courier new', courier; font-size: small;">while got_key==0:</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    key = input("Enter the key &amp; press Return ");</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    key=key.upper()</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    got_key=1</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    for j in range(len(key)):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">        if(key[j]&lt;'A' or key[j]&gt;'Z'):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">            got_key=0</span><br /><span style="font-family: 'courier new', courier; font-size: small;">print(key)</span></p> <p style="text-align: center;"><span style="font-family: 'courier new', courier; font-size: small;"><a href="index.php/python/8-deciphering">PREVIOUS</a>          <a href="index.php/python/9-input-output">NEXT</a></span></p></div> File Input/Output 2012-07-14T10:10:36+00:00 2012-07-14T10:10:36+00:00 http://www.cryptoden.com/index.php/python/9-input-output Super User mikejcowan@me.com <div class="feed-description"><h1 style="text-align: center;"><span style="font-size: medium;">Input data to the program from file &amp; save data to file.</span></h1> <p><span style="font-family: verdana, geneva; font-size: small;">Most programs need some sort of input. Our enciphering program needed the message and the key. So far I have made these inputs by pasting or typing them into the program.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">An alternative is to paste the text into an ordinary text file and make the program read the file. This method avoids a possible corruption of the program when typing in the message.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">So, for example, we paste, or type, a message into a file called ‘plaintext.txt’ located in the same directory as our program. Then the following program lines will open the file and read the contents into a string called 'message' and then print out the message.</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">filename = 'plaintext.txt'</span><br /><span style="font-family: 'courier new', courier; font-size: small;">fileid = open(filename,”r”) #...’r’ is for “read”</span><br /><span style="font-family: 'courier new', courier; font-size: small;">message=fileid.read()</span><br /><span style="font-family: 'courier new', courier; font-size: small;">fileid.close()</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">The same structure is used for saving the cipher created by the program. Below I've demonstrated this with a pretend piece of ciphertext.</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">ciphertext ='ABCDE FGHIJ KLMNO PQRST UVWXY Z'</span><br /><span style="font-family: 'courier new', courier; font-size: small;">filename = "cipher.txt"</span><br /><span style="font-family: 'courier new', courier; font-size: small;">fileid = open(filename,"w") #...”w” is for “write”</span><br /><span style="font-family: 'courier new', courier; font-size: small;">fileid.write(ciphertext)</span><br /><span style="font-family: 'courier new', courier; font-size: small;">fileid.close()</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">If you type this little program into Idle|File|New Window and press F5 to run it, you will find a new file 'cipher.txt' has been created in your directory and that it includes the pretend ciphertext.</span></p> <p style="text-align: center;"><span style="font-family: verdana, geneva; font-size: small;"><a href="index.php/python/10-keyboard-input">PREVIOUS</a>          <a href="index.php/python/11-key-completion">NEXT</a></span></p></div> <div class="feed-description"><h1 style="text-align: center;"><span style="font-size: medium;">Input data to the program from file &amp; save data to file.</span></h1> <p><span style="font-family: verdana, geneva; font-size: small;">Most programs need some sort of input. Our enciphering program needed the message and the key. So far I have made these inputs by pasting or typing them into the program.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">An alternative is to paste the text into an ordinary text file and make the program read the file. This method avoids a possible corruption of the program when typing in the message.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">So, for example, we paste, or type, a message into a file called ‘plaintext.txt’ located in the same directory as our program. Then the following program lines will open the file and read the contents into a string called 'message' and then print out the message.</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">filename = 'plaintext.txt'</span><br /><span style="font-family: 'courier new', courier; font-size: small;">fileid = open(filename,”r”) #...’r’ is for “read”</span><br /><span style="font-family: 'courier new', courier; font-size: small;">message=fileid.read()</span><br /><span style="font-family: 'courier new', courier; font-size: small;">fileid.close()</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">The same structure is used for saving the cipher created by the program. Below I've demonstrated this with a pretend piece of ciphertext.</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">ciphertext ='ABCDE FGHIJ KLMNO PQRST UVWXY Z'</span><br /><span style="font-family: 'courier new', courier; font-size: small;">filename = "cipher.txt"</span><br /><span style="font-family: 'courier new', courier; font-size: small;">fileid = open(filename,"w") #...”w” is for “write”</span><br /><span style="font-family: 'courier new', courier; font-size: small;">fileid.write(ciphertext)</span><br /><span style="font-family: 'courier new', courier; font-size: small;">fileid.close()</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">If you type this little program into Idle|File|New Window and press F5 to run it, you will find a new file 'cipher.txt' has been created in your directory and that it includes the pretend ciphertext.</span></p> <p style="text-align: center;"><span style="font-family: verdana, geneva; font-size: small;"><a href="index.php/python/10-keyboard-input">PREVIOUS</a>          <a href="index.php/python/11-key-completion">NEXT</a></span></p></div> Key completion 2012-07-14T10:33:57+00:00 2012-07-14T10:33:57+00:00 http://www.cryptoden.com/index.php/python/11-key-completion Super User mikejcowan@me.com <div class="feed-description"><h1 style="text-align: center;"><span style="font-family: verdana, geneva; font-size: small;">The program adds the rest of the alphabet to the key.</span></h1> <p><span style="font-family: verdana, geneva; font-size: small;">After inputting the keyword we need to add on the rest of the alphabet to form the full key (as described in the Enciphering chapter). The algorithm for this task is:</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">    take each letter of the alphabet from the string called alphabet:</span><br /><span style="font-family: verdana, geneva; font-size: small;">    convert it to upper case</span><br /><span style="font-family: verdana, geneva; font-size: small;">    find out whether this letter is present in ‘key’</span><br /><span style="font-family: verdana, geneva; font-size: small;">        if it is not present then add it to ‘key’</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">Here is the program code that you might like to try:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">alphabet = 'abcdefghijklmnopqrstuvwxyz'</span><br /><span style="font-family: 'courier new', courier; font-size: small;">key = 'HYPNOTIZABLE'</span><br /><span style="font-family: 'courier new', courier; font-size: small;">length_key=len(key)</span><br /><span style="font-family: 'courier new', courier; font-size: small;">for j in range(26): </span><br /><span style="font-family: 'courier new', courier; font-size: small;">    present=0</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    for k in range(length_key):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">        if(alphabet[j].upper()==key[k]):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">            present=1</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    if(present==0):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">        key=key+alphabet[j].upper()</span><br /><span style="font-family: 'courier new', courier; font-size: small;">print (key)</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">What is going on here? First of all, each letter of the alphabet is chosen and made into upper case and the variable 'present' is set to zero. Then the alphabet letter is compared with each letter in the keyword. If there is a match the variable 'present' is set to 1. After all keyword letters have been scanned, if 'present' is still zero then the alphabet letter is added to the key.</span></p> <p style="text-align: center;"><span style="font-family: verdana, geneva; font-size: small;"><a href="index.php/python/9-input-output">PREVIOUS</a>          <a href="index.php/python/12-functions">NEXT</a></span></p></div> <div class="feed-description"><h1 style="text-align: center;"><span style="font-family: verdana, geneva; font-size: small;">The program adds the rest of the alphabet to the key.</span></h1> <p><span style="font-family: verdana, geneva; font-size: small;">After inputting the keyword we need to add on the rest of the alphabet to form the full key (as described in the Enciphering chapter). The algorithm for this task is:</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">    take each letter of the alphabet from the string called alphabet:</span><br /><span style="font-family: verdana, geneva; font-size: small;">    convert it to upper case</span><br /><span style="font-family: verdana, geneva; font-size: small;">    find out whether this letter is present in ‘key’</span><br /><span style="font-family: verdana, geneva; font-size: small;">        if it is not present then add it to ‘key’</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">Here is the program code that you might like to try:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">alphabet = 'abcdefghijklmnopqrstuvwxyz'</span><br /><span style="font-family: 'courier new', courier; font-size: small;">key = 'HYPNOTIZABLE'</span><br /><span style="font-family: 'courier new', courier; font-size: small;">length_key=len(key)</span><br /><span style="font-family: 'courier new', courier; font-size: small;">for j in range(26): </span><br /><span style="font-family: 'courier new', courier; font-size: small;">    present=0</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    for k in range(length_key):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">        if(alphabet[j].upper()==key[k]):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">            present=1</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    if(present==0):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">        key=key+alphabet[j].upper()</span><br /><span style="font-family: 'courier new', courier; font-size: small;">print (key)</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">What is going on here? First of all, each letter of the alphabet is chosen and made into upper case and the variable 'present' is set to zero. Then the alphabet letter is compared with each letter in the keyword. If there is a match the variable 'present' is set to 1. After all keyword letters have been scanned, if 'present' is still zero then the alphabet letter is added to the key.</span></p> <p style="text-align: center;"><span style="font-family: verdana, geneva; font-size: small;"><a href="index.php/python/9-input-output">PREVIOUS</a>          <a href="index.php/python/12-functions">NEXT</a></span></p></div> Functions 2012-07-14T10:44:33+00:00 2012-07-14T10:44:33+00:00 http://www.cryptoden.com/index.php/python/12-functions Super User mikejcowan@me.com <div class="feed-description"><h1 style="text-align: center;"><span style="font-family: verdana, geneva; font-size: medium;">Functions</span></h1> <p><span style="font-family: verdana, geneva; font-size: small;">The complete program for getting the message from a file ‘message.txt’, inputting the key, extending the key, enciphering and saving the ciphertext to a file ‘cipher.txt’ is given in the Appendix. You will perhaps feel that the program is getting a bit long to follow, and would be quite difficult to understand if you came back to it after a few weeks. </span><br /><span style="font-family: verdana, geneva; font-size: small;">Python has a way of simplifying simplify a program by breaking it up into functional parts, or functions as they are called in programming. The main part of the program is simply the name of these functions like:</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">    get message from file</span><br /><span style="font-family: verdana, geneva; font-size: small;">    remove punctuation and spaces</span><br /><span style="font-family: verdana, geneva; font-size: small;">    get the key from the keyboard</span><br /><span style="font-family: verdana, geneva; font-size: small;">    encipher</span><br /><span style="font-family: verdana, geneva; font-size: small;">    save the cipher to file</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">I think you’d agree that this is very easy to understand.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">The functions themselves, each with its program code, are put before the main part of the program. This code is much easier to understand because the function name explains what the code is doing.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">It is possible to send data (strings and variables) to a function and to get data back. Data is sent to a function by placing it at the end of the function name, enclosed in brackets. Data is received back from a function into the string or variable that preceded the function call. For example, to get the string called ‘message’ from a file named ‘plaintext.txt’ we could write this line in the main part of the program:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">message = get_message(plaintext.txt)</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">Notice we must join together any words in the name of the function with a ‘_’.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">Above the main part of the menu we write the program code for the function. We begin with a definition of its name and, in brackets, the data sent from main and finally a semi-colon. Note that the name of the data can be different in the function and in the main part of the program. We indent the rest of the function’s code by 4 spaces, showing it all belongs to the function. Finally we end with the ‘return’ command, followed with data to be returned to main in brackets:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">def get_message(filename):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    message=’’</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    fileid = open(filename,'r') #...’r’ is for “read”</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    message=fileid.read()</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    fileid.close()</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    return(message)</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">You can send to a function, or receive back, more than one piece of data. So to encipher we can write the command in main:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">cipher = encipher(plain,key)</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">This will send both strings ‘plain’ and ‘key’ to the function. The code for the function, again placed before main, will be:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">def encipher(plain,key):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    n = 0</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    length=len(plain)</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    cipher=’’</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    for j in range(length):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">        position=alphabet.index(plain[j])</span><br /><span style="font-family: 'courier new', courier; font-size: small;">        cipher = cipher + key[position]</span><br /><span style="font-family: 'courier new', courier; font-size: small;">        n = n + 1</span><br /><span style="font-family: 'courier new', courier; font-size: small;">        if (n==5):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">            cipher = cipher + ' '</span><br /><span style="font-family: 'courier new', courier; font-size: small;">            n=0</span><br /><span style="font-family: 'courier new', courier; font-size: small;">     return(cipher)</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">Note that the final line sends back the string ‘cipher’ to main.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">Using these functions we can rewrite the program so that it’s easier to follow. The main part is simply:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">alphabet = 'abcdefghijklmnopqrstuvwxyz'</span><br /><span style="font-family: 'courier new', courier; font-size: small;">message= get_message('plaintext.txt')</span><br /><span style="font-family: 'courier new', courier; font-size: small;">plain=remove_spaces(message)</span><br /><span style="font-family: 'courier new', courier; font-size: small;">key=get_key()</span><br /><span style="font-family: 'courier new', courier; font-size: small;">key=extend_key(key)</span><br /><span style="font-family: 'courier new', courier; font-size: small;">cipher=encipher(plain,key)</span><br /><span style="font-family: 'courier new', courier; font-size: small;">save_ciphertext(‘cipher.txt’)</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">and the functions are written above main -- as shown in the Appendix.</span></p> <p style="text-align: center;"><span style="font-family: verdana, geneva; font-size: small;"><a href="index.php/python/11-key-completion">PREVIOUS</a>          <a href="index.php/python/13-conclusion">NEXT</a></span></p></div> <div class="feed-description"><h1 style="text-align: center;"><span style="font-family: verdana, geneva; font-size: medium;">Functions</span></h1> <p><span style="font-family: verdana, geneva; font-size: small;">The complete program for getting the message from a file ‘message.txt’, inputting the key, extending the key, enciphering and saving the ciphertext to a file ‘cipher.txt’ is given in the Appendix. You will perhaps feel that the program is getting a bit long to follow, and would be quite difficult to understand if you came back to it after a few weeks. </span><br /><span style="font-family: verdana, geneva; font-size: small;">Python has a way of simplifying simplify a program by breaking it up into functional parts, or functions as they are called in programming. The main part of the program is simply the name of these functions like:</span><br /> <br /><span style="font-family: verdana, geneva; font-size: small;">    get message from file</span><br /><span style="font-family: verdana, geneva; font-size: small;">    remove punctuation and spaces</span><br /><span style="font-family: verdana, geneva; font-size: small;">    get the key from the keyboard</span><br /><span style="font-family: verdana, geneva; font-size: small;">    encipher</span><br /><span style="font-family: verdana, geneva; font-size: small;">    save the cipher to file</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">I think you’d agree that this is very easy to understand.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">The functions themselves, each with its program code, are put before the main part of the program. This code is much easier to understand because the function name explains what the code is doing.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">It is possible to send data (strings and variables) to a function and to get data back. Data is sent to a function by placing it at the end of the function name, enclosed in brackets. Data is received back from a function into the string or variable that preceded the function call. For example, to get the string called ‘message’ from a file named ‘plaintext.txt’ we could write this line in the main part of the program:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">message = get_message(plaintext.txt)</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">Notice we must join together any words in the name of the function with a ‘_’.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">Above the main part of the menu we write the program code for the function. We begin with a definition of its name and, in brackets, the data sent from main and finally a semi-colon. Note that the name of the data can be different in the function and in the main part of the program. We indent the rest of the function’s code by 4 spaces, showing it all belongs to the function. Finally we end with the ‘return’ command, followed with data to be returned to main in brackets:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">def get_message(filename):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    message=’’</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    fileid = open(filename,'r') #...’r’ is for “read”</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    message=fileid.read()</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    fileid.close()</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    return(message)</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">You can send to a function, or receive back, more than one piece of data. So to encipher we can write the command in main:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">cipher = encipher(plain,key)</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">This will send both strings ‘plain’ and ‘key’ to the function. The code for the function, again placed before main, will be:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">def encipher(plain,key):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    n = 0</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    length=len(plain)</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    cipher=’’</span><br /><span style="font-family: 'courier new', courier; font-size: small;">    for j in range(length):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">        position=alphabet.index(plain[j])</span><br /><span style="font-family: 'courier new', courier; font-size: small;">        cipher = cipher + key[position]</span><br /><span style="font-family: 'courier new', courier; font-size: small;">        n = n + 1</span><br /><span style="font-family: 'courier new', courier; font-size: small;">        if (n==5):</span><br /><span style="font-family: 'courier new', courier; font-size: small;">            cipher = cipher + ' '</span><br /><span style="font-family: 'courier new', courier; font-size: small;">            n=0</span><br /><span style="font-family: 'courier new', courier; font-size: small;">     return(cipher)</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">Note that the final line sends back the string ‘cipher’ to main.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">Using these functions we can rewrite the program so that it’s easier to follow. The main part is simply:</span></p> <p><span style="font-family: 'courier new', courier; font-size: small;">alphabet = 'abcdefghijklmnopqrstuvwxyz'</span><br /><span style="font-family: 'courier new', courier; font-size: small;">message= get_message('plaintext.txt')</span><br /><span style="font-family: 'courier new', courier; font-size: small;">plain=remove_spaces(message)</span><br /><span style="font-family: 'courier new', courier; font-size: small;">key=get_key()</span><br /><span style="font-family: 'courier new', courier; font-size: small;">key=extend_key(key)</span><br /><span style="font-family: 'courier new', courier; font-size: small;">cipher=encipher(plain,key)</span><br /><span style="font-family: 'courier new', courier; font-size: small;">save_ciphertext(‘cipher.txt’)</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">and the functions are written above main -- as shown in the Appendix.</span></p> <p style="text-align: center;"><span style="font-family: verdana, geneva; font-size: small;"><a href="index.php/python/11-key-completion">PREVIOUS</a>          <a href="index.php/python/13-conclusion">NEXT</a></span></p></div> python hillclimber 2013-02-07T07:33:24+00:00 2013-02-07T07:33:24+00:00 http://www.cryptoden.com/index.php/python/47-python-hillclimber Super User mikejcowan@me.com <div class="feed-description"><h3 style="text-align: center;"><span style="font-family: verdana, geneva; font-size: small;">A Python program to solve a cipher</span></h3> <p><span style="font-family: verdana, geneva; font-size: small;">This section demonstrates how a Python program can be used to solve a cryptogram, in this case a monoalphabetic substitution cipher. (There is a similar section <a href="index.php/solvers/using-javascript/48-using-javascript">using javascrip</a>t here). </span></p> <p>The ciphertext to be solved is:<br /><span style="font-family: verdana, geneva; font-size: small;"><span style="font-family: 'courier new', courier;">OJNPZ FDWXJ UHDNI JGKFD QDPDQ JODHI XIFJK DSBZW NBQZH HBIZD VIDFF DHQIJ HSBQB JHHJF JHMDN NDLRB NDSUB ODEHJ UPDTD NXQAB HM</span></span></p> <p><span style="font-family: verdana, geneva; font-size: small;">The plaintext solution is:</span></p> <p><span style="font-family: verdana, geneva; font-size: small;"><span style="font-family: 'courier new', courier;">forsalebyownercompletesetofencyclopediabritannica</span></span><br /><span style="font-family: 'courier new', courier; font-size: small;">excellentconditionnolongerrequiredwifeknowseverything</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">using this Key:</span><br /><span style="font-family: 'courier new', courier; font-size: small;">Plaintext : abcdefghijklmnopqrstuvwxyz</span><br /><span style="font-family: 'courier new', courier; font-size: small;">Ciphertext: ZWISDOMABCEFGHJKLNPQRTUVXY</span></p> <p><br /><span style="font-family: verdana, geneva; font-size: small;">If you are not familiar with the Hillclimbing algorithm, you may want to first read <a href="index.php/algorithms/churn-algorithm/17-hillclimbing">this section </a></span></p> <p><span style="font-family: verdana, geneva; font-size: small; line-height: 1.3em;"><strong>Python Program</strong>. You can download the <a href="images/examples/hillclimb_py.doc">Python program here</a>. It runs in the latest version of Python (3.2.3) You will also need to download a table of<a href="images/examples/TetraLog_hank.txt"> log tetragram frequencies here</a> to do the scoring. The program and table need to be placed in the same folder.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">The program works like this:</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">1. load the table of log tetragraphs from the file "TetraLog_hank.doc"</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">2. remove the spaces from the ciphertext</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">3. make a random mixed alphabet and use it to decipher and score</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">4. swap every combintion of letters in the mixed alphabet, each time deciphering and scoring. Keep the mixed alphabet only if the score improves.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">5. repeat (4) for as long as the score improves. Otherwise return to (3)</span></p></div> <div class="feed-description"><h3 style="text-align: center;"><span style="font-family: verdana, geneva; font-size: small;">A Python program to solve a cipher</span></h3> <p><span style="font-family: verdana, geneva; font-size: small;">This section demonstrates how a Python program can be used to solve a cryptogram, in this case a monoalphabetic substitution cipher. (There is a similar section <a href="index.php/solvers/using-javascript/48-using-javascript">using javascrip</a>t here). </span></p> <p>The ciphertext to be solved is:<br /><span style="font-family: verdana, geneva; font-size: small;"><span style="font-family: 'courier new', courier;">OJNPZ FDWXJ UHDNI JGKFD QDPDQ JODHI XIFJK DSBZW NBQZH HBIZD VIDFF DHQIJ HSBQB JHHJF JHMDN NDLRB NDSUB ODEHJ UPDTD NXQAB HM</span></span></p> <p><span style="font-family: verdana, geneva; font-size: small;">The plaintext solution is:</span></p> <p><span style="font-family: verdana, geneva; font-size: small;"><span style="font-family: 'courier new', courier;">forsalebyownercompletesetofencyclopediabritannica</span></span><br /><span style="font-family: 'courier new', courier; font-size: small;">excellentconditionnolongerrequiredwifeknowseverything</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">using this Key:</span><br /><span style="font-family: 'courier new', courier; font-size: small;">Plaintext : abcdefghijklmnopqrstuvwxyz</span><br /><span style="font-family: 'courier new', courier; font-size: small;">Ciphertext: ZWISDOMABCEFGHJKLNPQRTUVXY</span></p> <p><br /><span style="font-family: verdana, geneva; font-size: small;">If you are not familiar with the Hillclimbing algorithm, you may want to first read <a href="index.php/algorithms/churn-algorithm/17-hillclimbing">this section </a></span></p> <p><span style="font-family: verdana, geneva; font-size: small; line-height: 1.3em;"><strong>Python Program</strong>. You can download the <a href="images/examples/hillclimb_py.doc">Python program here</a>. It runs in the latest version of Python (3.2.3) You will also need to download a table of<a href="images/examples/TetraLog_hank.txt"> log tetragram frequencies here</a> to do the scoring. The program and table need to be placed in the same folder.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">The program works like this:</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">1. load the table of log tetragraphs from the file "TetraLog_hank.doc"</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">2. remove the spaces from the ciphertext</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">3. make a random mixed alphabet and use it to decipher and score</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">4. swap every combintion of letters in the mixed alphabet, each time deciphering and scoring. Keep the mixed alphabet only if the score improves.</span></p> <p><span style="font-family: verdana, geneva; font-size: small;">5. repeat (4) for as long as the score improves. Otherwise return to (3)</span></p></div>