4. Write a Java program to implement the DES algorithm logic.
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.util.Base64;
public class DESExample {
public static void main(String[] args) throws Exception {
// Step 1: Create DES Key
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
SecretKey secretKey = keyGen.generateKey();
// Step 2: Create Cipher object
Cipher cipher = Cipher.getInstance("DES");
// Plain text
String plainText = "HELLODES";
System.out.println("Plain Text: " + plainText);
// Step 3: Encryption
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);
System.out.println("Encrypted Text: " + encryptedText);
// Step 4: Decryption
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
String decryptedText = new String(decryptedBytes);
System.out.println("Decrypted Text: " + decryptedText);
}
}
Output:
Plain Text: HELLODES
Encrypted Text: pZK3XyMZ8kE=
Decrypted Text: HELLODES