0% found this document useful (0 votes)
27 views2 pages

Solution:-: "Message Encryption Using DES Algorithm - " "DES"

This Java code demonstrates encryption and decryption of a message entered by the user using DES algorithm. The code generates a DES key, encrypts the user-entered message, and then decrypts the encrypted message.

Uploaded by

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

Solution:-: "Message Encryption Using DES Algorithm - " "DES"

This Java code demonstrates encryption and decryption of a message entered by the user using DES algorithm. The code generates a DES key, encrypts the user-entered message, and then decrypts the encrypted message.

Uploaded by

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

import java.security.

InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
public class Myclass {
public static void main(String[] argv) {
try {
System.out.println("Message Encryption Using DES Algorithm\n-------");
KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
SecretKey myDesKey = keygenerator.generateKey();
Cipher desCipher;
desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
System.out.println("Enter the Original Message");
Scanner sc = new Scanner(System.in);
String message = sc.nextLine();
sc.close();
byte[] text = message.getBytes();
System.out.println("Message : " + new String(text));
System.out.println("Message [Byte Format] : " + text);
byte[] textEncrypted = desCipher.doFinal(text);
System.out.println("Encrypted Message: " + textEncrypted);
desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
byte[] textDecrypted = desCipher.doFinal(textEncrypted);
System.out.println("Decrypted Message: " + new String(textDecrypted));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
}
}

Solution :-

You might also like