0% found this document useful (0 votes)
38 views5 pages

Aes and Des

The document discusses the Advanced Encryption Standard (AES) and Data Encryption Standard (DES) algorithms, highlighting their encryption processes, key sizes, and structural differences. AES is a block cipher that encrypts data in 128-bit blocks using key sizes of 128, 192, or 256 bits, while DES is an older block cipher that operates on 64-bit blocks with a 56-bit key. The document also includes example Java programs demonstrating the implementation of both AES and DES encryption and decryption.

Uploaded by

saishivanie13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views5 pages

Aes and Des

The document discusses the Advanced Encryption Standard (AES) and Data Encryption Standard (DES) algorithms, highlighting their encryption processes, key sizes, and structural differences. AES is a block cipher that encrypts data in 128-bit blocks using key sizes of 128, 192, or 256 bits, while DES is an older block cipher that operates on 64-bit blocks with a 56-bit key. The document also includes example Java programs demonstrating the implementation of both AES and DES encryption and decryption.

Uploaded by

saishivanie13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

AES ALGORITHM:

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

 AES is a block cipher.

 The key size can be 128/192/256 bits.

 Encrypts data in blocks of 128 bits each.

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.

Working of the cipher :


AES performs operations on bytes of data rather than in bits. Since the block size is 128 bits,
the cipher processes 128 bits (or 16 bytes) of the input data at a time.

The number of rounds depends on the key length as follows :

128 bit key – 10 rounds

192 bit key – 12 rounds

256 bit key – 14 rounds

Creation of Round keys :


A Key Schedule algorithm is used to calculate all the round keys from the key. So the initial
key is used to create many different round keys which will be used in the corresponding
round of the encryption.

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;

public class AES {

private static SecretKeySpec secretKey;


private static byte[] key;

public static void setKey(final String myKey) {


MessageDigest sha = null;
try {
key = myKey.getBytes("UTF-8");
sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key, 16);
secretKey = new SecretKeySpec(key, "AES");
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
e.printStackTrace();
}
}

public static String encrypt(final String strToEncrypt, final String secret) {


try {
setKey(secret);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return Base64.getEncoder()
.encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
} catch (Exception e) {
System.out.println("Error while encrypting: " + e.toString());
}
return null;
}

public static String decrypt(final String strToDecrypt, final String secret) {


try {
setKey(secret);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return new String(cipher.doFinal(Base64.getDecoder()
.decode(strToDecrypt)));
} catch (Exception e) {
System.out.println("Error while decrypting: " + e.toString());
}
return null;
}

public static void main(final String[] args) {


final String secretKey = "ssshhhhhhhhhhh!!!!";
Scanner sc = new Scanner(System.in);
System.out.print("Enter Original String: ");
String originalString = sc.nextLine();
String encryptedString = AES.encrypt(originalString, secretKey) ;
String decryptedString = AES.decrypt(encryptedString, secretKey) ;

System.out.println("Original String: "+originalString);


System.out.println("Encrypted String: "+encryptedString);
System.out.println("Decrypted String: "+decryptedString);
}
}
OUTPUT:

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.

 The initial permutation is performed on plain text.

 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

 The result of this process produces 64-bit ciphertext.


Step 1: Key Transformation

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.

Step 2: Expansion 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;

public class DES{


public static void main(String[] args) throws IOException, NoSuchAlgorithmException,
InvalidKeyException, InvalidKeySpecException, NoSuchPaddingException,
IllegalBlockSizeException, BadPaddingException {

String message="This is a confidential message.";


byte[] myMessage =message.getBytes(); //string to byte array as DES works on bytes

KeyGenerator Mygenerator = KeyGenerator.getInstance("DES");


SecretKey myDesKey = Mygenerator.generateKey();

Cipher myCipher = Cipher.getInstance("DES");

myCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
byte[] myEncryptedBytes=myCipher.doFinal(myMessage);

myCipher.init(Cipher.DECRYPT_MODE, myDesKey);
byte[] myDecryptedBytes=myCipher.doFinal(myEncryptedBytes);

String encrypteddata=new String(myEncryptedBytes);


String decrypteddata=new String(myDecryptedBytes);

System.out.println("Message : "+ message);


System.out.println("Encrypted - "+ encrypteddata);
System.out.println("Decrypted Message - "+ decrypteddata);
}
}

OUTPUT:

You might also like