# Simple Vigenere cipher in Python

See: - Part 2/3 - Part 3/3

I am currently reading "The code book" by Simon Singh, and he just described how the Vigenere cipher works... I am not coding any Python lately, so I have decided to implement it (real quick), not using any algorithm but manually, as someone would have done 300 years ago, preparing a Vigenere square, and then looking up the values in the table.

The Python code is pretty simple:

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57``` ```#!/usr/bin/env python # Simple Vigenere cipher implementation in Python import string mykey="WHITE" input_text="en un lugar de la mancha de cuyo nombre no quiero acordarme" ciphertext = [] matrix = [] encryption_tuple= [] row = 0 control = 0 # Alphabet used as reference source = string.ascii_uppercase # Creating the Vigenere Square. A 26x26 matrix. # In the example provided by the book, instead of using the regular alphabet as reference # we shift the items, so the column used as reference doesn't start in A, but in B for row in range(len(source)): matrix.append([ x for i,x in enumerate(source) if i > row ]) for i,x in enumerate(source): if i <= row: matrix[row].append(x) # Creating the tuple based on the letter and key. ie: # ('D', 'W'), ('I', 'H'), ('V', 'I'), ('E', 'T'), ('R', 'E'), ('T', 'W'), ... # In case special characters are not considered, this is cleaner: # import itertools # text=[ x for x in input_text.upper() if x in string.ascii_letters] # encryption_tuple = [(x,y) for x,y in zip(text, itertools.cycle(mykey))] for x,y in enumerate(input_text.upper()): control = 0 if control % len(mykey) == 0 else control if y in string.punctuation or y in string.whitespace: encryption_tuple.append((y,y)) else: encryption_tuple.append((y,mykey[control])) control += 1 # Each element y in the tuple is the key in the alphabet matrix for x,y in encryption_tuple: if source.find(x) == -1: ciphertext.append(x) else: ref_row = matrix[0].index(y) ciphertext.append(matrix[ref_row][source.index(x)]) # Print guide print("-> Reference:") print(" " + ' '.join([x for x in source])) # Printing Vigenere square print("-> Square:") for id,i in enumerate(matrix,1): print("{:02d} {}".format(id,' '.join(i))) # Print results print("-> Key: {0}".format(mykey)) print("-> Input text: {0}".format(input_text)) print("-> Output text: {0}".format(''.join(ciphertext))) ```

I stored the code in GITHUB.

In my case, as in the book, I have used "WHITE" as keyword, and the string to cipher is hardcoded in the script (input_text)

The main step is to map the input_text("en un lugar...") with the keyword("WHITE"), so we end up with something like this:

```('E', 'W')
('N', 'H')
(' ', ' ')
('U', 'I')
('N', 'T')
(' ', ' ')
('L', 'E')
('U', 'W')
('G', 'H')
('A', 'I')
('R', 'T')
(' ', ' ')
('D', 'E')
[...]
```
• The keyword will be repeated over and over until input_text string finishes
• For the sake of clarity, I haven't stripped the white spaces or any other punctuation symbol from the input message, but it makes the cipher (even) weaker.

Those tuples are going to be used to look for the letter to replace the actual symbol. For example: In order to cipher "E", we will use the row 22, as is the one starting by "W", therefore, the letter "E" will be replaced by "A". Next letter is "N", and we will use row 7, so it will be replaced by "U", and so on...

Not very likely to replace opengpg, but it works:

```\$ python vigenere_cipher.py
-> Reference:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
-> Square:
01 B C D E F G H I J K L M N O P Q R S T U V W X Y Z A
02 C D E F G H I J K L M N O P Q R S T U V W X Y Z A B
03 D E F G H I J K L M N O P Q R S T U V W X Y Z A B C
04 E F G H I J K L M N O P Q R S T U V W X Y Z A B C D
05 F G H I J K L M N O P Q R S T U V W X Y Z A B C D E
06 G H I J K L M N O P Q R S T U V W X Y Z A B C D E F
07 H I J K L M N O P Q R S T U V W X Y Z A B C D E F G
08 I J K L M N O P Q R S T U V W X Y Z A B C D E F G H
09 J K L M N O P Q R S T U V W X Y Z A B C D E F G H I
10 K L M N O P Q R S T U V W X Y Z A B C D E F G H I J
11 L M N O P Q R S T U V W X Y Z A B C D E F G H I J K
12 M N O P Q R S T U V W X Y Z A B C D E F G H I J K L
13 N O P Q R S T U V W X Y Z A B C D E F G H I J K L M
14 O P Q R S T U V W X Y Z A B C D E F G H I J K L M N
15 P Q R S T U V W X Y Z A B C D E F G H I J K L M N O
16 Q R S T U V W X Y Z A B C D E F G H I J K L M N O P
17 R S T U V W X Y Z A B C D E F G H I J K L M N O P Q
18 S T U V W X Y Z A B C D E F G H I J K L M N O P Q R
19 T U V W X Y Z A B C D E F G H I J K L M N O P Q R S
20 U V W X Y Z A B C D E F G H I J K L M N O P Q R S T
21 V W X Y Z A B C D E F G H I J K L M N O P Q R S T U
22 W X Y Z A B C D E F G H I J K L M N O P Q R S T U V
23 X Y Z A B C D E F G H I J K L M N O P Q R S T U V W
24 Y Z A B C D E F G H I J K L M N O P Q R S T U V W X
25 Z A B C D E F G H I J K L M N O P Q R S T U V W X Y
26 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

-> Key: WHITE
-> Input text: en un lugar de la mancha de cuyo nombre no quiero acordarme
-> Output text: AU CG PQNIK HA SI FEJJPT HA JCRS JVUUVA UW JYELZH EYVZWENTM
```

I'll try to find time to code the decryption part and also to try different ways of implementing it.

By the way, I totally recommend the book!

Later

# SFTP oneliner (as SCP)

Quick note to self:

Secure Copy (scp) (a one line command very easy to include in bash scripts) can be replaced by a similar Secure File Transfer (sftp) command, but the documentation is not very clear...

For instance, this command to transfer a file to a remote host using SCP:

`scp src_file user@remote_host:/remote/path/dst_file`

Can be replaced by this one, using SFTP:

`sftp user@remote_host:/remote/path/ <<< \$'put src_file dst_file'`

Example:

```# sftp root@192.168.1.100:/tmp/ <<< \$'put /tmp/test test_sftp'
Connected to 192.168.1.100.
Changing to: /tmp/
sftp> put /tmp/test test_sftp
/tmp/test                                          100%    0     0.0KB/s   00:00
#
```

The idea is taken from a few StackOverflow threads, all the credit to them... I decided to put it together here, so I can find it easily.

Later

# 2017 resolutions

{% img center /img/goals2017.jpg '2017resolutions' %}

Another entry in my personal online notebook (considering the number of visits of this site, I don't think it should be called blog or website)

We are only 11 day into the new year, so it is not too late for my 2017 resolutions. I'd rather post them here than keep them in a piece of paper on my desktop because, apparently, making goals "public" helps to succeed (or I can be publicly shamed at the end of the year)

Here they go:

1) The classics:

• Work out more often: At least 3 times per week. No matter if it rains or snows.
• Eat (more) healthy: It seems I'll be stuck in Spain for a few months (at least until April), so it will be a good opportunity to prepare some green juices (I bought a blender a couple of years ago, and I think I've turned it on 2 times).
• Read (more) books: Goodreads helps me to track my reading habits... And they are far from impressive, around 12 books per year. It doesn't sound too bad, but considering I don't have family responsibilities, it is not enough. I think I should blame Netflix for this :)
• Study something new: There are countless online courses nowadays available for free. I've already done a couple of them in edX and I'd like to do more. Coursera has some really interesting courses as well.
• Improve my English pronunciation: My English is quite good, but still sounds like a spaniard speaking English.

2) Job related:

• Look for a different role/position: I've been doing pretty much the same stuff for way too long. Time to change. Period.
• Ramp up my python skills: I am kind of stuck with Python. It is enough for my current role (system engineering in a big telco, not development) but I'd say it is not good enough if I want to do something different (more IT related).
• Learn some new languages: Go?
• Ansible: I've been playing with Ansible every now and then for a couple of years, creating some playbooks for myself, but nothing fancy. I don't have the opportunity to use it in my current gig, so I'd need to look for a way to include it in my daily work.

3) Hobby related:

• Fix my home network: 1 ISP router + 1 AP router + 1 openWRT (as firewall) + 1 NAS + several PC/laptops/smartphones/raspberry pi's = a hell of a mess.
• Soldering: I have a bunch of ESP8266 chips laying around in a box. I should put them to good use.
• Photo library housekeeping: I have traveled a lot these past years, and I have tons of photos in my NAS that I have not even seen. It needs a cleanup.
• Post more (and better): I registered this domain back in 2005... And, even though no one reads this (the number of daily page reads is close to zero), I like to post stuff here. It's a good way to keep things available (thanks to github of course). I'd like to post more stuff, and if possible, more interesting.

I'd say this should be enough for 2017.

\\psgonza

# My 2016 in photos

(Pretty big pictures, sorry about that)

Saying hello to another year with some pictures... 2016 is been a good year, both personally and professionally:

• I've been working in Japan for several months again... Best place for living as expat (IMO)

{% img center /img/2016_japan.jpg 'Japan' %}

• April: I never really liked south east Asia that much (too hot for my taste), but I have to say I had a really good time in Vietnam and Cambodia... Looking forward to going back there soon!

{% img center /img/2016_vietnam_cambodia.jpg 'vietnam_cambodia' %}

• July: Raising sun at the top of Mt Fuji

{% img center /img/2016_fujisan.jpg 'fuji' %}

• August: Hawaii is the place to be. Period.

{% img center /img/2016_hawaii.jpg 'hawaii' %}

• December: Vienna (European capital of Xmas) & Salzburg

{% img center /img/2016_austria.jpg 'austria' %}

I hope 2017 is as good as 2016, although I'd like to make some changes...

Happy New Year everyone

\\psgonza

Related: 2015 in photos, 2014 in photos

# My books in 2016

We just said goodbye to 2016, and as in 2015, here goes my Goodreads summary:

{% img center /img/books2016.gif 'goodreads' %}

Unfortunately, I failed my reading challenge... I read 10 of the 13 books in the first half of the year, but I have been stuck with a Python book for months. And podcasts have not helped much :)