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

26DES

This Java code defines a DES encryption class with methods to encrypt and decrypt strings. It uses DES symmetric encryption with a hard-coded key, encrypting strings to and from a Base64 encoded format. The main method demonstrates encrypting and then decrypting a string input by the user.
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)
41 views2 pages

26DES

This Java code defines a DES encryption class with methods to encrypt and decrypt strings. It uses DES symmetric encryption with a hard-coded key, encrypting strings to and from a Base64 encoded format. The main method demonstrates encrypting and then decrypting a string input by the user.
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

DES.

java 29-01-2020 09:40

1 import java.util.*;
2 import java.io.BufferedReader;
3 import java.io.InputStreamReader;
4 import java.security.spec.KeySpec;
5 import javax.crypto.Cipher;
6 import javax.crypto.SecretKey;
7 import javax.crypto.SecretKeyFactory;
8 import javax.crypto.spec.DESKeySpec;
9 //import sun.misc.BASE64Decoder;
10 //import sun.misc.BASE64Encoder;
11 import java.util.Base64;
12
13 public class DES {
14 private KeySpec myKeySpec;
15 private SecretKeyFactory mySecretKeyFactory;
16 private Cipher cipher;
17 byte[] keyAsBytes;
18 private String myEncryptionKey;
19 SecretKey key;
20 static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
21 public DES() throws Exception {
22 // TODO code application logic here
23 myEncryptionKey="ThisIsSecretEncryptionKey";
24 keyAsBytes=myEncryptionKey.getBytes();
25 myKeySpec=new DESKeySpec(keyAsBytes);
26 mySecretKeyFactory = SecretKeyFactory.getInstance("DES");
27 cipher = Cipher.getInstance("DES");
28 key = mySecretKeyFactory.generateSecret(myKeySpec);
29 }
30 public String encrypt(String unencryptedString)
31 { String encryptedString = null;
32 try {
33 cipher.init(Cipher.ENCRYPT_MODE, key);
34 byte[] plainText = unencryptedString.getBytes();
35 byte[] encryptedText = cipher.doFinal(plainText);
36
37
38 encryptedString = Base64.getEncoder().encodeToString(encryptedText);
39
40 }
41 catch (Exception e)
42 {
43 e.printStackTrace();
44 }
45 return encryptedString;
46 }
47
48 public String decrypt(String encryptedString)
49 { String decryptedText=null;
50 try {

Page 1 of 2
DES.java 29-01-2020 09:40

51 cipher.init(Cipher.DECRYPT_MODE, key);
52
53 byte[] encryptedText = Base64.getDecoder().decode(encryptedString);
54
55 byte[] plainText = cipher.doFinal(encryptedText);
56 decryptedText=new String(plainText);
57 }
58 catch (Exception e)
59 {
60 e.printStackTrace();
61 }
62 return decryptedText;
63 }
64
65
66 public static void main(String args []) throws Exception
67 {
68 DES myEncryptor= new DES();
69 System.out.print("Enter the string: ");
70 String stringToEncrypt = br.readLine();
71 String encrypted = myEncryptor.encrypt(stringToEncrypt);
72 String decrypted = myEncryptor.decrypt(encrypted);
73 System.out.println("\nString To Encrypt: " +stringToEncrypt);
74 System.out.println("\nEncrypted Value : " +encrypted);
75 System.out.println("\nDecrypted Value : " +decrypted); System.out.println("");
76 }
77 }

Page 2 of 2

You might also like