Wednesday, May 6, 2015

Really getting stated with Python








Well, looks like my decision in starting again AND with Python was the right one. I am loving it.
As my first post with 'hands on' will be a very simple code taken from an excellent material I found on the Internet: "Hacking Secret Ciphers with Python with"
 Despite the pretentious title, the material if extremely newbie and very well written. I am expecting to learn a lot about coding and security.
 After explain many concepts about Python, the author presents a very simple code:

#Getting starting with Hacking ciphers
message = 'This message intend to be inverted'
inverted = ''
i = len(message) -1
while i>=0:
    inverted = inverted + message[i]
    i = i -1
   print(inverted)

As the book talks about cryptography and Cipher, this little program aims to perform a very simple way to add a very little security layer at the information. We are just inverting letters inside the message. For sure this is not considered to be a form of security. But, the idea here is start handling code in order to understand its utility in getting security information through the network.

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
detrevni eb ot dnetni egassem sihT
>>> 

I'll try to explain this simple code. Actually to me even this code is not that simple. In short, what it does is at first the variable named message receive the value 'This message intend to be inverted'.
Every time you create a variable, you are actually reserving a memory space and putting there a value. In this case, the value is a "String". In Python, string can be written inside simple quote or double quote.
 If I put something like print(message) I'll retrieve the value in the memory and print will show the phrase.
 Moving down to the code, we have another variable named inverted. This variable has no value. We have just allocated a space in the memory and let it empty.The idea is use this empty slot in conjunction with the variable message in order for invert the phrase.
 Moving down, we can see a new variable name "i". The i's value will be a Python's function named len(). This function has the ability to return an integer representing how many characters there are in the string.
 To prove it, we can change our code:

#Getting starting with Hacking ciphers
message = 'This message intend to be inverted'
inverted = ' '
i = len(message) -1
print(i)
while i>=0:
    inverted = inverted + message[i]
    i = i -1
print(inverted)

We just inserted a print(i) after i = len(message) -1

The output can be seen bellow:

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
33
detrevni eb ot dnetni egassem sihT
>>> 
First, it shows how many characters has the string. Then, it shows the string inverted.

We can do one more thing:

#Getting starting with Hacking ciphers
message = 'This message intend to be inverted'
inverted = ' '
i = len(message) -1
print(i)
while i>=0:
    inverted = inverted + message[i]
    i = i -1
    print(message)
print(inverted)

>>> ================================ RESTART ================================
>>> 
33
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
This message intend to be inverted
detrevni eb ot dnetni egassem sihT
>>> 
I just add a print after  i = i -1 and we can see  the code inserting the phrase in the memory as much times as the amount of character in the string.

We can also discriminate which character is in a specific position:

#Getting starting with Hacking ciphers
message = 'This message intend to be inverted'
inverted = ' '
i = len(message) -1
print(i)
print(message[11])
while i>=0:
    inverted = inverted + message[i]
    i = i -1
print(inverted)


>>> ================================ RESTART ================================
>>> 
33
e
detrevni eb ot dnetni egassem sihT
>>> 

We are showing how many characters there are in the whole string and which character is in the position 11 of the memory. In this case the character "e".


The last one is far more interesting:

#Getting starting with Hacking ciphers
message = 'This message intend to be inverted'
inverted = ''
i = len(message) -1
print(i)
print(message[11])
while i>=0:
    inverted = inverted + message[i]
    print(i, message[i], inverted)
    i = i -1
print(inverted)

This produce the following output:

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
33
e
33 d d
32 e de
31 t det
30 r detr
29 e detre
28 v detrev
27 n detrevn
26 i detrevni
25   detrevni 
24 e detrevni e
23 b detrevni eb
22   detrevni eb 
21 o detrevni eb o
20 t detrevni eb ot
19   detrevni eb ot 
18 d detrevni eb ot d
17 n detrevni eb ot dn
16 e detrevni eb ot dne
15 t detrevni eb ot dnet
14 n detrevni eb ot dnetn
13 i detrevni eb ot dnetni
12   detrevni eb ot dnetni 
11 e detrevni eb ot dnetni e
10 g detrevni eb ot dnetni eg
9 a detrevni eb ot dnetni ega
8 s detrevni eb ot dnetni egas
7 s detrevni eb ot dnetni egass
6 e detrevni eb ot dnetni egasse
5 m detrevni eb ot dnetni egassem
4   detrevni eb ot dnetni egassem 
3 s detrevni eb ot dnetni egassem s
2 i detrevni eb ot dnetni egassem si
1 h detrevni eb ot dnetni egassem sih
0 T detrevni eb ot dnetni egassem sihT
detrevni eb ot dnetni egassem sihT
>>> 

It prints the total number of character, the eleven character and each memory position with its respective value.

And this is it. This simple code is able to read the string, count how many characters it has, decrease it one by one and put each value in the memory and show it inverted.
 According to "while", "i" must be greater than or equal to zero. This is the trigger to the code stop the process and show the message.
 The result is quite simplistic but the idea is great. In a so simple code we can see lots of concepts surrounding this amazing programming language.





No comments:

Post a Comment