Home » Java Code for DES

Java Code for DES

by Online Tutorials Library

Java Code for DES

Our dependency on the internet is increasing day by day and we share lots of personal information with others. Since our data or personal information is not secure. For this reason, the security of the data become essential for us. We need to keep data confidential, unmodified, and readily available to authorized readers only. We can make secure data by using the DES (Data Encryption Standard) mechanism that can encrypt and decrypt the data. Using the DES algorithm is the most popular way to encrypt and decrypt data. It is a widely used symmetric (encryption and decryption) algorithm in the world.

In this section, we will learn the DES algorithm that is used to generate the ciphertext. Also, we will implement the DES algorithm in a Java program.

Java Code for DES

DES Algorithm

DES stands for Data Encryption Standard. It is a symmetric-key block cipher algorithm used to encrypt and decrypt data. It is developed by the IBM team in early 1970. It accepts the plaintext in 64-bit blocks and changes it into the ciphertext that uses the 64-bit keys to encrypt the data. The algorithm uses the same key to encrypt and decrypt the data.

It is based on LUCIFER (also known as Feistel block cipher algorithm) which is a direct predecessor of the DES algorithm. It is developed by eminent scholar and researcher Horst Feistel at IBM. It provides high security by using a 128-bit key block and a 128-bit block size. The DES algorithm uses the 16 rounds of the Feistel structure. The structure uses a unique key for each round. Finally, in 1976, it was approved by the federal encryption standard.

In 2002, AES (Advanced Encryption Standard) replaced the DES encryption algorithm as the accepted standard. Later in 1995, the advanced version of the DES algorithm was introduced that is known as Triple DES (3DES or TDES). Officially, it is known as Triple Data Encryption Algorithm (TDEA or 3DEA).

TDEA is also a symmetric-key block cipher algorithm that uses the DES cipher algorithm thrice to each data block. Its block size is 64-bits and key sizes are 168, 112, and 56-bits, respectively for the keys 1, 2, and 3. It also uses the DES equivalent rounds i.e. 48. It means 16 rounds for each key.

Java Code for DES

It encrypts the data using the first key (k1), decrypts the data by using the second key (k2) and again encrypts the data by using the third key (k3). Another variant of the algorithm uses only two keys k1 and k3. Where both the keys k1 and k3 are the same. It is used still but considered as a legacy algorithm.

The following figure shows the encryption and decryption using TDEA.

Java Code for DES

Let’s understand the DES algorithm.

Generating Keys

The algorithm performs 16 rounds of encryption and for each round, a unique key is generated. Before moving to the steps, it is important to know that in plaintext the bits are labeled from 1 to 64 where 1 is the most significant bit and 64 is the least significant bit. The process of generating keys are as follows:

1. First, we compress and transpose the given 64-bit key into a 48-bit keys by using the table given below:

2. Separate the result into two equal parts i.e. C and D.

3. The part C and D are left-shifted circularly. For encryption, the 1st, 2nd, 9th, and 16th round is responsible that shifts a bit to the left by 1 bit, circularly. All the rest rounds are shifted to the left by 2-bit circularly.

4. After that, the result is compressed to 48-bits with the help of the following table.

5. The result that we get from step 3 becomes the input for the next round of the key generation.

Encryption Steps of the Algorithm

The algorithm includes the following steps:

  1. The algorithm takes the 64-bit plain text as input.
  2. The text is parsed into a function called the Initial Permutation (IP) function.
  3. The initial permutation (IP) function breaks the plain text into the two halves of the permuted block. These two blocks are known as Left Plain Text (LPT) and Right Plain Text (RPT).
  4. The 16 round encryption process is performed on both blocks LPT and RPT. The encryption process performs the following:
    1. Key Transformation
    2. Expansion Permutation
    3. S-Box Permutation
    4. P-Box Permutation
    5. XOR and Swap
  5. After performing the encryption process, the LPT and RPT block are rejoined. After that, the Final Permutation (FP) is applied to the combined block.
  6. Finally, we get the 64-bit ciphertext of the plaintext.

Java Code for DES

Decryption Step of the Algorithm

For decryption of the ciphertext, we use the same algorithm but in reverse order (step 4) of 16 round keys.

For better understanding of the algorithm, let’s see modes of operation for the DES algorithm.

Modes of Operation For DES

There are the following five modes of operation that can be chosen:

  1. ECB (Electronic Codebook): Each 64-bit block is encrypted and decrypted independently.
  2. CBC (Cipher Block Chaining): In block chaining, each block depends on the previous one and uses an Initialization Vector (IV).
  3. CFB (Cipher Feedback): The ciphertext that we get from the previous step becomes the input for the algorithm. The operation produces the pseudorandom output. The output that we get is XORed with the plaintext and generates the ciphertext for the next operation.
  4. OFB (Output Feedback): It is just like CFB. Except that the encryption algorithm input is the output from the preceding DES.
  5. CTR (Counter): Each plaintext block is XORed with an encrypted counter. After that, the counter is incremented for each subsequent block.

We conclude that the basic steps of the algorithm are:

  • First, we need to generate a secret key by using a KeyGenerator.
  • Create the two Ciphers one for encryption and the other for decryption. Remember that the key must be the same as we have specified in Initialization Vector (IV).
  • At last, write and read the encrypted or decrypted data by using the CipherOutputStream and CipherInputStream.

Let’s implement the DES algorithm in a Java program and see how data is encrypted and decrypted using the algorithm.

DesProgram.java

Before running the above program, you will have to do some changes in the program. First, you will have to create a text file that you want to encrypt. In our case, we have created a file with the name DemoData.txt and written the following text into it. You can write anything.

DemoData.txt

Java Code for DES

Let’s run the above program and see the output.

Output:

Java Code for DES

When we run the above program, it generates the two files encrypteddata.txt and deecrypteddata.txt at the specified location. Let’s see what inside the encrypted and decrypted file.

encrypteddata.txt

Java Code for DES

deecrypteddata.txt

Java Code for DES

We see that data is decrypted into the same text as we had written in the DemoData.txt file.


You may also like