· 5 years ago · Feb 26, 2020, 04:20 AM
1//DES
2
3import javax.crypto.Cipher;
4import javax.crypto.KeyGenerator;
5import javax.crypto.SecretKey;
6import java.util.*;
7public class Des {
8public static void main(String[] args) throws Exception {
9Scanner sc=new Scanner(System.in);
10KeyGenerator keygenerator=KeyGenerator.getInstance("DES");
11SecretKey key = keygenerator.generateKey();
12//ENCRYPTION
13Cipher encCipher = Cipher.getInstance("DES");
14encCipher.init(Cipher.ENCRYPT_MODE, key);
15System.out.println("Enter text to be encrypted:: ");
16String strPlain = sc.nextLine();
17byte[] plainBytes = strPlain.getBytes();
18byte[] encBytes = encCipher.doFinal(plainBytes);
19System.out.println("Encoded text:: "+new String(encBytes));
20//DECRYPTION
21Cipher decCipher = Cipher.getInstance("DES");
22decCipher.init(Cipher.DECRYPT_MODE, key);
23byte[] decBytes = decCipher.doFinal(encBytes);
24System.out.println("\nDecoded text:: "+new String(decBytes));
25}
26}