Java - DES Encryption and Decryption example

DES (Data Encryption Standard) algorithm is the most commonly used encryption symmetric-key algorithm in the world. For a long time, "cipher generation" was synonymous with DES in many people's minds. Although the Electronic Frontier Foundation recently called, it built a 220,000 machine to try to crack DES encrypted data. DES and its variant "triple data encryption algorithm" will still be widely used in government and banking. AES became the replacement for DES). 
DES processes bits or binary numbers. We know that every four bits make up a hexadecimal number. DES encrypts a set of 64-bit information, which is represented by 16 hexadecimal numbers. DES also uses 64-bit long ciphers to provide encryption. However, every 8 bits in the key are ignored, resulting in a correct key length of 56 bits in DES. However, in any case, one 64-bit block is an eternal DES organization.

Java DES Encryption and Decryption 

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.util.Base64;

class DESExample {

Cipher ecipher;
Cipher dcipher;

DESExample(SecretKey key) throws Exception {
ecipher = Cipher.getInstance("DES");
dcipher = Cipher.getInstance("DES");
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);
}

public String encrypt(String str) throws Exception {
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");

// Encrypt
byte[] enc = ecipher.doFinal(utf8);

// Encode bytes to base64 to get a string

return Base64.getEncoder().encodeToString(enc);

}

public String decrypt(String str) throws Exception {
// Decode base64 to get bytes
byte[] dec = Base64.getDecoder().decode(str);

byte[] utf8 = dcipher.doFinal(dec);

// Decode using utf-8
return new String(utf8, "UTF8");
}

public static void main(String[] argv) throws Exception {
final String secretText = "www.knowledgefactory.net";
System.out.println("SecretText: " + secretText);
SecretKey key = KeyGenerator.getInstance("DES").generateKey();
DESExample encrypter = new DESExample(key);
String encrypted = encrypter.encrypt(secretText);
System.out.println("Encrypted Value: " + encrypted);
String decrypted = encrypter.decrypt(encrypted);
System.out.println("Decrypted: " + decrypted);

}
}

Output:

SecretText: www.knowledgefactory.net 


Encrypted Value: GO9ewpZ5Ehjze1QsNW0cefP1V55DaeNJJG4z0FBbq2g= 


Decrypted: www.knowledgefactory.net 

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Spring Boot + Mockito simple application with 100% code coverage

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete