# Working with Matasano Cryptopals

I always wanted to start writing my first blog post about something that i always cared and appreciate on the field of Computer Science. Cryptography has been for more than thousand years a science that was used on hiding or making the information unreadable in times of war and peace.

I would like to put out of the scope history for a moment, there are a plenty of usefull books and articles that everyone can read about the role that Cryptography played in history. Today we are going to write a simple script to encrypt and decrypt data on ruby.

When i first start learning cryptography, i was used to write simple scripts with ruby, python or perl, because of the many features they have with regular expressions. Basically they make things simpler. Many of books that talk about cryptography explain in theory the right implementation of how to use the algorithm on real applications. Some of the books that i would suggest everyone to read are books like Applied Cryptography: Protocols, Algorithms and Source Code in C from Bruce Schneier, The Code Book: The Secret History of Codes and Code-breaking by Simon Singh, Cryptography Engineering: Design Principles and Practical Applications by Niels Ferguson and more others.

After googling, i found some usefull material on crypto challenges from matasano called Cryptopals. I would suggest everyone that wants to learn even programming to start with the challenges, you will not regret it! There are some other crypto challenges too, like those from NSA but the Matasano Challenges are focused more in real-time applications.

In the following example i use the Ruby programming language. Lets take the first set and take a look to the challenge 5, Repeating XOR. I picked this challenge because is simple and it can be easy to implement to an application. `The example below doesn't make an applications secure!!!`. This challenge will only help us just to learn how to use the most simple crypto method to encrypt some text.

So, let’s start! First we create a simple file, which will have our plain data stored.

Now lets put some text on the new file.

Now, basically we have the plaintext that it will be encrypted using repeating XOR, but what that means? First of all Repeating XOR is just a very small piece of the puzzle on encrypting data. To encrypt the plaintext we will need a key, which it will be the same for encrypting and decrypting the data. That means we have to do with a Symmetric-key algorithm.

From programming aproach what we need to do is:

#### Encrypt

• Encrypt data
• Output data to another file

#### Decrypt

• Decrypt data
• Output data to another file

## Ruby!!

Ruby is an amazing programming language, you can do a lot of stuff, save a lot of time, and has great features and modules for basically everything. I would suggest even Perl programming language for the challenges, because of the amazing features of regular expressions.

To make the script more helpfull for the user we need to create a usage method.

Next we need to write a method to read the content of the file and an other one to write the encrypted or decrypted data to an output file. So here they are.

We use four arguments.

• key
• encrypt / decrypt
This is one of the more simple examples of encrypting data. But let’s check what happens when someone use a different password, so instead of `password123` we add `password1234`.
All what we get is the encrypted data which is encrypted once again with a different password. Repeating XOR can be a really good example on how someone can encrypt some data just for fun. As from the security prespective repeating XOR in this example is not `secure` to be used.