Aes and Des
Aes and Des
Advanced Encryption Standard (AES) is a specification for the encryption of electronic data
established by the U.S National Institute of Standards and Technology (NIST) in 2001. AES
is widely used today as it is a much stronger than DES and triple DES despite being harder to
implement.
Points to remember
That means it takes 128 bits as input and outputs 128 bits of encrypted cipher text as output.
AES relies on substitution-permutation network principle which means it is performed using
a series of linked operations which involves replacing and shuffling of the input data.
PROGRAM:
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;
import java.util.*;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
DES ALGORITHM:
Data Encryption Standard (DES) is a block cipher with a 56-bit key length that has played a
significant role in data security. Data encryption standard (DES) has been found vulnerable to
very powerful attacks therefore, the popularity of DES has been found slightly on the decline.
DES is a block cipher and encrypts data in blocks of size of 64 bits each, which means 64 bits
of plain text go as the input to DES, which produces 64 bits of ciphertext. The same
algorithm and key are used for encryption and decryption, with minor differences. The key
length is 56 bits.
The discarding of every 8th bit of the key produces a 56-bit key from the original 64-bit key.
DES is based on the two fundamental attributes of cryptography: substitution (also called
confusion) and transposition (also called diffusion). DES consists of 16 steps, each of which
is called a round. Each round performs the steps of substitution and transposition. Let us now
discuss the broad-level steps in DES.
In the first step, the 64-bit plain text block is handed over to an initial Permutation (IP)
function.
Next, the initial permutation (IP) produces two halves of the permuted block; saying
Left Plain Text (LPT) and Right Plain Text (RPT).
Now each LPT and RPT go through 16 rounds of the encryption process.
In the end, LPT and RPT are rejoined and a Final Permutation (FP) is performed on
the combined block
We already know that the DES process uses a 56-bit key, which is obtained by eliminating all
the bits present in every 8th position in a 64-bit key. In this step, a 48-bit key is generated.
The 56-bit key is split into two equal halves and depending upon the number of rounds the
bits are shifted to the left in a circular fashion.
Due to this, all the bits in the key are rearranged again. We can observe that some of the bits
get eliminated during the shifting process, producing a 48-bit key. This process is known as
compression permutation.
Let's consider an RPT of the 32-bit size that is created in the IP stage. In this step, it is
expanded from 32-bit to 48-bit. The RPT of 32-bit size is broken down into 8 chunks of 4 bits
each and extra two bits are added to every chunk, later on, the bits are permutated among
themselves leading to 48-bit data. An XOR function is applied in between the 48-bit key
obtained from step 1 and the 48-bit expanded RPT.
PROGRAM:
import java.util.*;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.io.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
myCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
byte[] myEncryptedBytes=myCipher.doFinal(myMessage);
myCipher.init(Cipher.DECRYPT_MODE, myDesKey);
byte[] myDecryptedBytes=myCipher.doFinal(myEncryptedBytes);
OUTPUT: