Copy Link

Add to Bookmark

Report

# xine-1.016

` `

/-----------------------------\

| Xine - issue #1 - Phile 016 |

\-----------------------------/

A little introduction to cryptography by b0z0

---------------------------------------------

This is only a little introduction to the simplest method of encryption.

All the methods described above may not be used to protect your important

data. This text is only intended to give to the novice a general overview

of the classic encryption methods, which are also the base of the current

encryption algorithms. This methods are very simple, but i haven't already

seen an article talking about them, so i decided to collect a little

of material and write something about this :)

Substitution cipher

-------------------

The substitution cipher basically works in this way:

First of all let's fix a permutation of our alphabet

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

P E Y D X U W B Z T J M Q I R N V O L K G S H A C F

The rule of the substitution cipher is very simple: we must just change

every character of the plain text with the correspondant permutation

described by the second line of our example. All "A"s will became "P"s,

all "B"s will became "E"s, "C"s will became "Y"s and so on.

So if the phrase that we would to encrypt was for example (spaces are

ignored!)

BILL GATES SUCKS

the encrypted message would be

EZMM WPKXL LGYJL

The decryption of this message is very simple. We must just use the

inverse permutation, which is obtained overturning the direct permutation:

P E Y D X U W B Z T J M Q I R N V O L K G S H A C F

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

Note that the english alphabet has 26 characters, so the possible

permutations are 26! (26*25*24*23*22*....*2*1), which is a quite big

number :)

Transposition cipher

--------------------

Also the transposition cipher is based on a permutation, but in this

cipher there we must select a period P, which is also knows as the period

of the transposition. The plain text is then divided in pieces which

lenght is equal to P and each character of this pieces is changed with

the appropriate permutation that we have created before. So, if the

selected P was 5 let's select a permutation of integers from 1 to P:

1 2 3 4 5

3 1 5 4 2

Now let's say that our phrase to encrypt (spaces are ignored) is:

YO COME ON AND HACK

And now let's divide the phrase in pieces of five (again, spaces are

ignored :) ). To divide the parts we will use a "/":

YO COM/E ON AN/D HACK

and now let's mix the letters as we decided before. The result is

CY MOON EN AOA DKCH

Also here to decrypt the we must just use the inverse permutation on the

P-long parts:

3 1 5 4 2

1 2 3 4 5

Additive cipher

---------------

To work with this type of cipher we must imagine that the alphabet is

such an interrupted chain, when after the last Z there is again the first

A and so on. The additive cipher is very simple and is based on a rotation

of the alphabet. Let's see an example

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

After a rotation of 4 to right it becomes

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

Now to encrypt the message we must just change each character with the

correspondant rotated one. So HELLO THERE will became DAHHK PDANA. As you

can see the rotations that we can made are from 0 (plain text) to 25, not

a lot of possibilityes really. An example of the additive cipher is the

Caezar cipher, which was basically an additive cipher with the fixed

rotation of 3 to the left. To decrypt a message encrypted with the

additive cipher we must just know the number of the executed rotations.

Vigenere cipher

---------------

The Vigenere cipher is based on a secret word, let's say VIRUS. Let's

suppouse that we are going to crypt this phrase:

MICROSUCK REALLY SUCK

(VIRUSVIRU SVIRUS VIRU)

The second line, where the secret word is repeated, is used for the

encryption. Using the Vigenere table (look at the Appendix) we will

encrypt each letter using the right row correspondant to the current

character of our secret word. So the first M will be encrypted using the

row V, so the M will became a H, the I using the row I will became a Q,

the C a T and so on. The encrypted message will be (space are ignored):

HQTLGNCTE JZICFQ NCTE

To decrypt the message we can just look into the Vigenere table in the

column corresponding the key character and find the character for which

the result is our encrypted letter.

Grate cipher

------------

This is a very simple method of encryption. The plain text is written in

two lines, going up and down. So the phrase REALLY COOL PHILES (ignoring

spaces) is encrypted writing a character in the first row, then one in

the second, then again one in the first, again one in the second and so

on. Like this:

R A L C O P I E

E L Y O L H L S

And the resulting encrypted phrase is RALCOPIEELYOLHLS.

Column transposition cipher

---------------------------

The plain text is written in part of a deteminated lenght, any part in

a new row, as explained below.

(The phrase is WELCOME TO THE IKX and the selected period is 3)

W E L

C O M

E T O

T H E

I K X

And now we must select a permutation between 1 and the selected number

(in our example 3). Let's say 3 2 1. In the final encrypted output the

colums are written horizontally in the specified order of the permutation.

So the third column will be written first, then the second and then the

first. The result that follows is LMOEX EOTHK WCETI.

Adding key cipher

-----------------

In this example we will need a secret key of lenght n. Let's define that

the first character of the key is named k(0), the second k(1) and the last

k(n-1). The same will be for the phrase that we want to encrypt. The first

character will be called p(0), the second p(1) and so on. The encryption

method is simple:

r(i) = [ p(i) + k(i mod n) ] mod 26

r(i) is the resulting i letter. Of course in this method we assign to

each letter a number starting with 0. So A will have the value of 0, B of

1 and up to Z of 25. And now let's see an example:

SPACES ARE OF COURSE IGNORED

KINDAK IND AK INDAKI NDAKIND

the first phrase is in plain text, and in the second phrase we put the

secret word (KINDA) so all the decrypted text will be covered. Now let's

see how this works. Let's calculate the first letter:

r(0) = [ p(0) + k(0 mod 5) ] mod 26

r(0) = [ p(0) + k(0) ] mod 26

r(0) = [ "S" + "K" ] mod 26

r(0) = [ 18 + 10 ] mod 26

r(0) = 28 mod 26

r(0) = 2

r(0) = "C"

and so on. the second letter will be a X (15 + 8), then a N (0 + 13) and

so on. The resulting message is

CXNFEC IFH OP KBXRCM VJNYZRG

Appendix

--------

Vigenere table

--------------

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

C program to encrypt with the Vigenere cipher

---------------------------------------------

/*

Compile it with GCC:

gcc vig.c -o vigenere

Execute it:

vigenere < input_file

Where the first line in the input_file must contain the key (which

length is minor of MAXKEYLENGTH) and the other lines may contain the plain

text that you would like to encrypt. Note that this sample program only

handles lowercase inputs.

*/

#include <stdio.h>

#define MAXKEYLENGTH 20 /* maximum length of the key */

char key[MAXKEYLENGTH+1]; /* encipherment key */

int keylength = 0; /* length of the used key */

FILE *fp; /* set to stdin if interactive, else file */

void getkeyfromfile(void)

{

char *tmp = key; /* pointer to key array */

for (keylength=0; keylength < MAXKEYLENGTH; keylength++)

if ((key[keylength]= getc(fp)) == '\n') break;

}

int encipher(int i) /* key position */

{

int tmp; /* for cipher char calculation */

char currentchar; /* current charachter */

currentchar = getc(fp);

if (currentchar >= 'a' && currentchar <= 'z')

{

tmp = (currentchar + key[i] - 2*'a') % 26;

while (tmp < 0) tmp += 26;

tmp += 'a';

}

else

tmp = currentchar; /* if not alphabetic return unchanged */

return(tmp);

}

void vigenere(void)

{

int current, i=0; /* cipher character */

while (!feof(fp))

{

current = encipher(i); /* generate encrypted character */

if (current < 'a' || current > 'z')

{

putchar(' '); /* do a space */

continue; /* if not an alphabetic lowercase */

}

if (i == keylength-1) /* end of the key? */

i=0;

else

i++;

putchar(current);

}

putchar('\n');

}

void main(void)

{

fp = stdin;

getkeyfromfile();

vigenere();

}