-
Notifications
You must be signed in to change notification settings - Fork 0
/
des_crypto.java
65 lines (41 loc) · 1.58 KB
/
des_crypto.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package Cryptography;
//import java.security.InvalidKeyException;
//import java.security.NoSuchAlgorithmException;
//import java.util.Arrays;
//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.swing.JOptionPane;
public class des_crypto {
public static void main(String args[]) throws Exception
{
des();
}
public static void des()throws Exception
{
System.out.println("\n**** DES Cipher ****\n");
KeyGenerator Mygenerator =KeyGenerator.getInstance("DES");
SecretKey myDesKey = Mygenerator.generateKey();
Cipher desCipher = Cipher.getInstance("DES");
desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
String str=OpenFile.pickFile();
byte[] mybytes = str.getBytes();
String s1=new String(mybytes);
System.out.println("Before Encryption:-");
System.out.println(s1);
byte[] myEncryptedBytes = desCipher.doFinal(mybytes);
s1=new String(myEncryptedBytes);
JOptionPane.showMessageDialog(null,"Encrypted Message "+"\n"+s1);
System.out.println("\nAfter Encryption:-");
System.out.println(s1);
desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
byte[] mydecryptedBytes = desCipher.doFinal(myEncryptedBytes);
String s = new String(mydecryptedBytes);
JOptionPane.showMessageDialog(null,"Decrypted Data "+"\n"+s);
System.out.println("\nAfter Decryption:-");
System.out.println(s);
}
}