## Adventures in Cryptography with Python – XOR Cipher

XOR cipher is a simple additive encryption technique in itself but is used commonly in other encryption techniques. The truth table for XOR cipher is as below. If the bits are same then the result is 0 and if the bits are different then the result is 1.

Bit 1 Operation Bit 2 Result
0 0 0
1 0 1
0 0 1
1 1 0

Let’s take an example. We would encrypt Sun using the key 01010010 repeatedly .

```Encryption
Text          |     S    |     u     |     n       |
ACII Code     |    083   |    117    |    110      |
Binary        | 01010011 |  01110101 |  01101110   |
Key           | 01010010 |  01010010 |  01010010   |
Cipher        | 00000001 |  00100111 |  00111100   |

Now if we XOR the cipher with the same key we will get back the out original text.

Decryption
Cipher        | 00000001 |  00100111 |  00111100   |
Key           | 01010010 |  01010010 |  01010010   |
Output        | 01010011 |  01110101 |  01101110   |
ACII Code     |    083   |    117    |     110     |
Text          |     S    |     u     |      n      |
```

This encryption we just did was not very secure because used the same key over and over again. To make our encryption more secure we should use a unique key and not the one which is repetitive in nature. A good technique that could be used is One-time Pad. This makes the encryption much more secure to the brute force attack.

# XOR encryption and decryption

The encryption and decryption using XOR has the same code. A python implementation for the same is below:

```input_str = raw_input("Enter the cipher text or plain text: ")
key = raw_input("Enter the key for encryption or decryption: ")
no_of_itr = len(input_str)
output_str = ""

for i in range(no_of_itr):
current = input_str[i]
current_key = key[i%len(key)]
output_str += chr(ord(current) ^ ord(current_key))

print "Here's the output: ", output_str
```

And here’s a sample run

The entire source code for this post can be found at https://github.com/abhishuk85/cryptography-plays

Any questions, comments or feedback are most welcome.

## Adventures in Cryptography with Python – ROT13 ROT13 is a letter substitution cipher and a special case of Caesar Cipher where each character in the plain text is shifted exactly 13 places. If you are not aware of Caesar Cipher then look at Caesar Cipher. For example the cipher for SUN becomes FHA.

The cool thing about this technique is that if we do a ROT13 on the cipher text then we get back the plain text since each letter in the text is shifted by 13 places. For example when we do a ROT13 on FHA we get back SUN

# ROT13 Encoder and Decoder

The encoder and Decoder for ROT13 is the same because there is no special logic during decoding since the shift for both encoding and decoding is the same. Below is the python code for the implementation of it. The code is pretty much the same as Caesar Cipher with the shift value set to 13 always.

```alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

input_str = raw_input("Enter message that you would like to encrypt/decrypt using ROT13: ")
shift = 13
no_of_itr = len(input_str)
output_str = ""

for i in range(no_of_itr):
current = input_str[i]
location = alphabets.find(current)
if location < 0:
output_str += input_str[i]
else:
new_location = (location + shift)%26
output_str += alphabets[new_location]

print "Here's the output: ", output_str
```

Here’s a sample run of ROT13

The entire source code for this post can be found at https://github.com/abhishuk85/cryptography-plays

Any questions, comments or feedback are most welcome.

## Adventures in Cryptography with Python – Base64

Base64 is a binary to text encoding technique rather than an encryption technique but I thought it made sense to cover it in this series because it is widely used especially for transmitting the data over the wire. The reason being the set of characters selected for this encoding is a subset of most common characters in all encoding and printable characters.

Here is the Base64 index table:

Index Char Index Char Index Char Index Char 0 `A` 16 `Q` 32 `g` 48 `w` 1 `B` 17 `R` 33 `h` 49 `x` 2 `C` 18 `S` 34 `i` 50 `y` 3 `D` 19 `T` 35 `j` 51 `z` 4 `E` 20 `U` 36 `k` 52 `0` 5 `F` 21 `V` 37 `l` 53 `1` 6 `G` 22 `W` 38 `m` 54 `2` 7 `H` 23 `X` 39 `n` 55 `3` 8 `I` 24 `Y` 40 `o` 56 `4` 9 `J` 25 `Z` 41 `p` 57 `5` 10 `K` 26 `a` 42 `q` 58 `6` 11 `L` 27 `b` 43 `r` 59 `7` 12 `M` 28 `c` 44 `s` 60 `8` 13 `N` 29 `d` 45 `t` 61 `9` 14 `O` 30 `e` 46 `u` 62 `+` 15 `P` 31 `f` 47 `v` 63 `/`

The conversion of a string into Base64 happens by taking the 8-bit binary equivalent of the alphabets and then slicing it into 6-bit unit since the maximum value in the Base64 is 2^6 and then using the index table like above binary would be represented. Lets take an example of string Sun and see how it would be represented in Base64

```Text          |     S    |     u     |     n       |
ACII Code     |    083   |    117    |    110      |
Binary        | 01010011 |  01110101 |  01101110   |
6-bit         | 010100 | 110111 | 010101 | 101110  |
Base64 Index  |   20   |    55  |   21   |   46    |
Base64 encoded|    U   |    3   |   V    |    u    |
```

We can verify this by converting the string with Python

```>>> "Sun".encode("base64")
'U3Vu\n'
```

The newline character that we see at the end of the output is ignored. Whether we decode the string with or without the we would still get the same string back

```>>> "U3Vu\n".decode("base64")
'Sun'
>>> "U3Vu".decode("base64")
'Sun'
```

The length of characters in the output has to be a multiple of 4. If it is not the case then the output is appended with either one or two “=” to make it so. For example when we convert Earth to Base64 we this in action

```>>> "Earth".encode("base64")
'RWFydGg=\n'
```

# Base64 Encoder

Sometimes for various reasons the strings are Base64 encoded multiple times and you might have noticed by now this increases the length of the output. The base64 encoder that I wrote using the one builtin with Python takes the number of times you would like to encode your string. The code is pretty straightforward.

```input_str = raw_input("Enter the string that you like to be base64 encoded:")
times = int(raw_input("How deep do you want it encoded:"))

output_str = input_str

for i in range(times):
output_str = output_str.encode("base64")

print "Encoded string: ", output_str
```

And here is a sample run

# Base64 Decoder

This a where it gets a little bit trickier since while decoding I assume that I am not aware of the number of times the text was encoded. I created a base sting that contains all the valid characters in Base64 encoded strings and then take the input as base64 encoded string

```base_64_encoding_characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="

input_str = raw_input("Enter the base64 encoded string that you would like to decode: ")
```

With the string to be decoded in hand we go into a while loop and run in it until we have a potential candidate for the original string. The basic logic is to try and decode the string and if fails to decode then append an “=” to its end and try again and also increase the error count in the process. We repeat this twice and keep going until we have a string that cannot be decoded.

```while error_count < 3:
input_str, is_end = ValidateAndSplit(input_str.replace('\n',''))

if is_end == True:
break;
try:
temp = input_str.decode("base64")
input_str = temp
output_str = temp
depth = depth + 1
error_count = 0
print input_str
except binascii.Error as err:
error_count = error_count + 1
input_str = input_str + "="

print "Potential decoded string: ", output_str, "\nWith depth: ", depth
```

The ValidateAndSplit method basically tries to remove unnecessary charters from the string to make sure we don’t down a bad path and also tells us when potentially we have reached the end of our search

```def ValidateAndSplit(input_str):
is_end = False
n = len(input_str)
if n < 1:
is_end = True
return input_str, is_end

for i in range(n):
c = input_str[i]
location = base_64_encoding_characters.find(c)
if location < 0 and c == " ":
is_end = True
break
elif location < 0:
data = input_str.split(c, 1)
input_str = data
break

return input_str, is_end
```

Here’s a sample run of this decoder with the same base64 string that we encoded before 10 times

The problem with the current approach is that if we might over decode the string that are one word only. One fix to that could be reaching out to reach out to an online dictionary and see that we have found a valid word.

The entire source code for this post can be found at https://github.com/abhishuk85/cryptography-plays

Any questions, comments or feedback are most welcome.

## Adventures in Cryptography with Python – Caesar Cipher

If you have ever been interested in cryptography or started to learn about it then there is a high probability that you would have come across Caesar Cipher. In case you haven’t, here’s how wikipedia explains it

In cryptography, a Caesar cipher, also known as Caesar’s cipher, the shift cipherCaesar’s code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by AE would become B, and so on. The method is named after Julius Caesar, who used it in his private correspondence. In this post and further posts this series we would implement these encryption and decryption in Python.

Note – You would need Python installed on your system to run the code in this post

# Caesar Cipher Encoder

This part of the algorithm is fairly straight forward. The code below asks the plain text that you like to encrypt using the Caesar Cipher and the number of alphabets that you would like to be shifted.

Then it calculates the number of characters in the input string as this will be used for the number of iterations that need to be done to encrypt everything (one character at a time)

During the iteration it uses the alphabets string as its base for shifting the characters skipping the characters that are not part of this base string to keep the structure of the input intact

Finally it outputs the ciphertext for you to use.

```alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

input_str = raw_input("Enter message that you would like to encrypt using caesar cipher: ")
shift = int(raw_input("Enter a shift value:"))

no_of_itr = len(input_str)
output_str = ""

for i in range(no_of_itr):
current = input_str[i]
loc = alphabets.find(current)
if location < 0:
output_str += input_str[i]
else:
newLocation = (location + shift)%26
output_str += alphabets[newLoc]

print "ciphertext: ", output_str
```

I am assuming that you have either already have python installed on your system or you would do it now. To encode your message using Caesar Cipher save the above code in a .py file and then int the command prompt navigate the folder where the file saved and run the command similar to below:

```D:\>python caesar_encoder.py
```

# Caesar Cipher Decoder

Just like hiding something is simple and finding it is difficult. Similarly encoding is much simpler than decoding it. Now assume that you encoded your message using Caesar Cipher but then you forgot how many character you actually shifted. In this solution we would go through creating a solution for deciphering the ciphertext for all the possible shifts and then using the Oxford dictionary API to check which of these output actually makes any sense.

The initial section of the code is straightforward. We would ask for the ciphertext and your guess of what the shift could be

```alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

input_str = raw_input("Enter cipher to decode: ")
shift = int(raw_input("Enter your guess of shift: "))

n = len(input_str)
output_str = ""
```

Then we would try to decode the ciphertext using your guess and see how good bad it was

```for k in range(n):
c = input_str[k]
location = alphabets.find(c)
if location < 0:
output_str += input_str[k]
else:
new_location = (location - shift)%26
output_str += alphabets[new_location]
data = output_str.split(" ", 20)
base_output_str = output_str
base_score = getScore(data[:20])
```

Hers’s the code for the getScore function. This calculates the scores of the decoded string based on the first 20 words by reaching out to the Oxford dictionary and checking if the decoded string is actually a valid word.

```# This function gets the meaning score of the potential plain text
def getScore(potential_plain_text):
current_score = 0.0
for temp in potential_plain_text:
#Limiting the match of words with 4 alphabets or more makes it less likely for false positive
if len(temp) > 3:
url = base_url + language + query_divider + temp.lower() + prefix_param + limit_param
r = requests.get(url, headers = {'app_id': app_id, 'app_key': app_key})
if json_data['metadata']['total'] > 0 and json_data['results']['score'] > 0:
return current_score
```

Now we loop through all the possible options of character shift and then sort the results based on score

```for i in range(26):
output_str = ""

for j in range(n):
c = input_str[j]
location = alphabets.find(c)
if location < 0:
output_str += input_str[j]
else:
new_location = (location - i)%26
output_str += alphabets[new_location]
data = output_str.split(" ", 20)
score = int(getScore(data[:20]))
results.append(Result(score,i,output_str))

results.sort(key=operator.attrgetter('score'))
results.reverse()

print "Plain text from your guess: ", base_output_str, " - with shift: ", shift, " - the score is", base_score, "\n"

print "Below are the potential plain text sorted by the probability of being correct in descending order!"

for t in results:
print "Potential plain text: ", t.plain_text, " - with shift: ", t.shift, " - the score is", t.score
```

Here’s a sample run

```C:\>python caesar_decoder.py
Enter cipher to decode: WKLV LV PB PHVVDJH
Enter your guess of shift: 3
```

And here’s the a sample output

The entire source code for this post can be found at https://github.com/abhishuk85/cryptography-plays

Any questions, comments or feedback are most welcome.

## So you think you want to be an IOT Geek – Adventures with Photon and Grove – IOT Fan IOT is the next technological wave and we all would be riding it weather we want or not. As a technologist by hobby and profession I like idea of everything I interact with being smarter and it is quickly turning into reality. You are right, we are just getting started and we have a long way to go use the Internet of Things to make everyone’s life’s easier, more comfortable and save money as we still do not fully understand how we would manage a lot of things like interactions, interconnections and most importantly security of these millions of devices across the internet and constantly communicating.

However we will explore more about all of these as we go along this adventure. In this series we will be playing with Photon and Grove components and see where this journey takes us.

Here is what I ordered and will be using in this series

A Particle Photon – \$19 (Photon)

Grove Kit for Photon – \$49 (Grove Shield for Photon)

Grove Mixer Pack V1 – \$29 (Grove Mixer Pack V2)

This would give us enough devices to play with. This is all we need for the entire tutorial however you do not need to order all of this, you could also order individual parts for the adventure you like as well.

If you already have knowledge of electronics than you might get the photon Starter kit and components that you need and use soldering and or jumper wires to make the circuit for your self.

If you are not good with electronics and or just lazy like me then here is the minimum of what you need to play along with the recipes in this series

1 Particle Photon

1 Grove Photon Shield

Individual Grove Components

# Fan with button

## Requirement

• Photon
• Grove Shield for Photon
• Mini Fan
• Button
• Cardboard

Let’s Get Cracking

• Place the Photon on the Grove shield if you have not already done so and connect the USB port to the power.

• Now lets adds the connector wires to port A4 and D2
• Connect the Button to the connector wire on D2
• Connect the Mini Fan converter to connector on A4 and connect the Mini Fan to the 2 wire port on Fan connector
• This is how the final assembly should look like.
• And now we would need to write some code to make this work. To start coding you would either need to login to your cloud IDE (Particle Build) or create a new project in your local particle Dev IDE (Particle Local IDE)

```// -------------------------------------
// Adventure : Start a Fan with a button
// -------------------------------------

#include "application.h"

// name the pins
#define BUTTONPIN D2
#define FANPIN A4

// This routine runs only once upon reset
void setup()
{
pinMode(BUTTONPIN, INPUT);
pinMode(FANPIN, OUTPUT);
}

// This routine loops forever
void loop()
{
if(val == 0) {                                   // if button is pressed
digitalWrite(FANPIN, LOW);                   // let the fan run
}
else {
digitalWrite(FANPIN, HIGH);                 // stop the fan
}
delay(500);
}```
• Now when you press the button the Fan should start spinning.

# Smart Fan – Runs when it’s hot

## Requirement

• Photon
• Grove Shield for Photon
• Mini Fan
• Cardboard
• Temperature Sensor

Lets make this fan smarter. We would use the temperature sensor to tell us the temperature and when its hot the fan will start spinning automatically.

• Remove the connection of the button and add the temperature sensor at A0 in the Grove Shield
• This is how the final assembly would look like
```// ------------------------------------------------
// Adventure: Start a Fan when it’s hot
// ------------------------------------------------
#include "application.h"
#include <math.h>

// name the pins
#define MOTORPIN A4
#define TEMPPIN A0

double volts = 0.0;
int getrand(int min,int max);
double tempC = 0.0;
double tempF = 0.0;

// This routine runs only once upon reset
void setup()
{
pinMode(MOTORPIN, OUTPUT);
pinMode(TEMPPIN, INPUT);
}

// This routine loops forever
void loop()
{
int B = 3975;
float resistance=(float)(4095-analogValue)*10000/analogValue;               // get the resistance of the sensor
tempC=1/(log(resistance/10000)/B+1/298.15)-273.15;                          // temprature in celcius
tempF = tempC * 9.0 / 5.0 + 32.0;                                           // temprature in farenhiet
Serial.println(tempF);

if(tempF > 75) {                                 // if it's hot
digitalWrite(MOTORPIN, HIGH);                   // let the fan run
}
else {
digitalWrite(MOTORPIN, LOW);                 // stop the fan
}
delay(5000);
}
```

# Run Fan through internet or when it’s hot

In the this adventure we will make this fan even smarter by able to receive commands through the internet so that we could start it through a browser.

Lets change our code to include a local variable and also expose a cloud function which would internally call the function to control the fan. Also we will alter the condition for turning on and off the fan to include the command given tough internet.

```// ------------------------------------------------
// Adventure: Start a Fan when it’s hot
// ------------------------------------------------
#include "application.h"
#include <math.h>
// name the pins
#define MOTORPIN A4
#define TEMPPIN A0

double volts = 0.0;
double tempC = 0.0;
double tempF = 0.0;
String fanCommand = "";
int fanState = -1;

// This routine runs only once upon reset
void setup()
{
pinMode(MOTORPIN, OUTPUT);
pinMode(TEMPPIN, INPUT);
Particle.function("fan", fanController);
}

// This routine loops forever
void loop()
{
int B = 3975;
float resistance=(float)(4095-analogValue)*10000/analogValue;               // get the resistance of the sensor
tempC=1/(log(resistance/10000)/B+1/298.15)-273.15;                          // temprature in celcius
tempF = tempC * 9.0 / 5.0 + 32.0;                                           // temprature in farenhiet
Serial.println(tempF);

if(tempF > 80 || fanCommand.equalsIgnoreCase("on")) {                                 // if it's hot
digitalWrite(MOTORPIN, HIGH);                   // let the fan run
}
else {
digitalWrite(MOTORPIN, LOW);                 // stop the fan
}
delay(5000);
}

int fanController(String command) {
Serial.println(command);
fanCommand = command;

if (command.equalsIgnoreCase("on")) {
digitalWrite(MOTORPIN, HIGH);                 // let the fan run
fanState = 1;
}
else if (command.equalsIgnoreCase("off")) {
digitalWrite(MOTORPIN, LOW);                 // stop the fan
fanState = 0;
} else {
digitalWrite(MOTORPIN, LOW);                 // stop the fan
}
fanState = -1;
return fanState;
}
```

Now let us create a simple html page to turn on and off our Fan

```<html>
<title>Fan Controller</title>
<script>
var accessToken = "Your device acess token";
var deviceID = "You device Id";
var urlLED = "https://api.spark.io/v1/devices/" + deviceID + "/fan";

function callbackFanOn(data, status) {
if (status == "success") {
\$('#fan-gif').attr('src','images/FanRunning.gif');
} else {
alert("There was a problem turning on the fan.");
}
}

function callbackFanOff(data, status) {
if (status == "success") {
\$('#fan-gif').attr('src','images/FanStopped.gif');
} else {
alert("There was a problem turning off the fan.");
}
}

function switchOn(){
\$.post(urlLED, {params: "on", access_token: accessToken }, callbackFanOn);

}

function switchOff(){
\$.post(urlLED, {params: "off", access_token: accessToken }, callbackFanOff);
}
</script>

<body>
<div style="align:center;">
<input type="button" onClick="switchOn()" value="ON"/>
<input type="button" onClick="switchOff()" value="OFF"/>
<br>
<img id="fan-gif" src="" >
</div>
</body>
</html>
```

Hope you had fun doing the project. Any questions, comments and feedback are most welcome.