Understanding Basic Cryptography with Java: Lesson 101

Writing this article to understand the basic of encryption with Java as our programming and implementation language.

Encryption in cryptography means, using algorithm and key encrypt the content. First, we will be discussing basic encryption techniques before going for advanced ones.

Symmetric Encryption, in this we have a single key to encrypt or decrypt the content. We will first learn about the most basic encryption which is prone to attacks and we will also learn how they evolve.

We will start with the initial encryption method and then move to advance one, and we will see how we can implement the same in Java.

1: The Caesar Cipher

This is one of the basic ciphers which was used in the early days. This cipher was worked on a letter instead of bits. In this cipher generally, the content or text was moved with a certain position. Let's see an example

in the above image, if you will see A will get encrypted to D, which means the letter is moving by 3 positions. Let's see how we can code this in Java.

Now the encryption got a bit stronger. We will see encryption techniques one after another and with time each algorithm will bring more security to the table.

2: Vigenere Cipher

In vigenere cipher is used to encrypt alphabetic text with the key. Like in Caesar cipher where the alphabets are shifted with a fixed position, in vigenere cipher it gets shifted with the key.

For example, the key is DUH, where the first letter will be shifted by 3 which is 3 letters after A, second will be shifted by 20 which is 20 letters after A and third letter will be shifted by 7 which is 7 letters after A.

Let's see the Java implementation

Let's look into the next algorithm

Caesar and Vigenere cipher is not strong cipher they are prone to many attacks like frequency analysis or many more. One time pad cipher is a very secure cipher, it has random key K, that is of the same length as P and produce a ciphertext C, defined as

C = P XOR K

Example 0 XOR 0 = 0 , 0 XOR 1 = 1, 1 XOR 0 = 1, 1 XOR 1 = 0

but the problem with one-time pad cipher is the key has to of the same length of content and for every content, we have to generate a new random key.

Lets see an example

Encryption :

C = P XOR K = 01101101 XOR 10110100 = 11011001

Decryption :

P = C XOR K = 11011001 XOR 10110100 = 01101101

We looked symmetric key previously, let's look about the next type of keys

Asymmetric Encryption

In asymmetric encryption, there is two types of keys, one to encrypt the data and another to decrypt. The key which is used for encryption is called a public key and is generally considered as publicly available. The decryption key must remain secret and is called the private key.

Let's look into next type of cryptography and with each algorithm, the encryption is getting stronger.

Authenticated Encryption

Authenticated encryption is a type of symmetric encryption that returns an authentication tag in addition to a ciphertext.

AE(K,P) = (C,T)

K = Key, P= Plain text, C= Cipher, T=Authentication tag

Authentication tag T is a short string that’s impossible to guess without the key. Decryption takes K, C, and T and returns the plain text P only if it verifies that T is a valid tag for the plaintext — Cipher text pair.

Authenticated Encryption with Associated Data

Authenticated Encryption with Associated Data(AEAD) is an extension of AE that takes the clear text and unencrypted data and uses it to generate the authentication tag AEAD (K, P, A) = (C, T)

According to Wikipedia Nonce is “In cryptography, a nonce is an arbitrary number that can be used just once in a cryptographic communication”. Generally nonce helps in replay attacks.

We will see an example of this encryption in the next articles.

See you in the next article.!

Aspiring Scientist