0% found this document useful (0 votes)
3 views

week 4

The document is a Java program that demonstrates the use of the DES (Data Encryption Standard) algorithm for encrypting and decrypting a confidential message. It generates a secret key, encrypts a predefined message, and then decrypts it back to its original form. The program outputs the original message, the encrypted data, and the decrypted message to the console.

Uploaded by

22641a0303
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

week 4

The document is a Java program that demonstrates the use of the DES (Data Encryption Standard) algorithm for encrypting and decrypting a confidential message. It generates a secret key, encrypts a predefined message, and then decrypts it back to its original form. The program outputs the original message, the encrypted data, and the decrypted message to the console.

Uploaded by

22641a0303
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

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;

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

//String we want to encrypt


String message="This is a confidential message.";
byte[] myMessage =message.getBytes(); //string to byte array as DES works
on bytes

//If you want to use your own key


// SecretKeyFactory MyKeyFactory = SecretKeyFactory.getInstance("DES");
// String Password = "My Password";
// byte[] mybyte =Password.getBytes();
// DESKeySpec myMaterial = new DESKeySpec(mybyte);
// SecretKey myDESKey = MyKeyFactory.generateSecret(myMaterial);

//Generating Key
KeyGenerator Mygenerator = KeyGenerator.getInstance("DES");
SecretKey myDesKey = Mygenerator.generateKey();

//initializing crypto algorithm


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

//setting encryption mode


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

//setting decryption mode


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

//print message in byte format


//System.out.println(Arrays.toString(myEncryptedBytes));
//System.out.println(Arrays.toString(myDecryptedBytes));

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);
}
}

You might also like