Repository navigation
Expand file tree
/
Copy pathdes.java
More file actions
69 lines (69 loc) · 2.12 KB
/
Copy pathdes.java
File metadata and controls
69 lines (69 loc) · 2.12 KB
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
66
67
68
69
package cns;
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
//import sun.misc.BASE64Decoder;
//import sun.misc.BASE64Encoder;
import java.util.Base64;
public class des {
private KeySpec myKeySpec;
private SecretKeyFactory mySecretKeyFactory;
private Cipher cipher;
byte[] keyAsBytes;
private String myEncryptionKey;
SecretKey key;
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public des() throws Exception {
// TODO code application logic here
myEncryptionKey="ThisIsSecretEncryptionKey";
keyAsBytes=myEncryptionKey.getBytes();
myKeySpec=new DESKeySpec(keyAsBytes);
mySecretKeyFactory = SecretKeyFactory.getInstance("DES");
cipher = Cipher.getInstance("DES");
key = mySecretKeyFactory.generateSecret(myKeySpec);
}
public String encrypt(String unencryptedString)
{ String encryptedString = null;
try {
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] plainText = unencryptedString.getBytes();
byte[] encryptedText = cipher.doFinal(plainText);
encryptedString = Base64.getEncoder().encodeToString(encryptedText);
}
catch (Exception e)
{
e.printStackTrace();
}
return encryptedString;
}
public String decrypt(String encryptedString)
{ String decryptedText=null;
try {
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] encryptedText = Base64.getDecoder().decode(encryptedString);
byte[] plainText = cipher.doFinal(encryptedText);
decryptedText=new String(plainText);
}
catch (Exception e)
{
e.printStackTrace();
}
return decryptedText;
}
public static void main(String args []) throws Exception
{
des myEncryptor= new des();
System.out.print("Enter the string: ");
String stringToEncrypt = br.readLine();
String encrypted = myEncryptor.encrypt(stringToEncrypt);
String decrypted = myEncryptor.decrypt(encrypted);
System.out.println("\nString To Encrypt: " +stringToEncrypt);
System.out.println("\nEncrypted Value : " +encrypted);
System.out.println("\nDecrypted Value : " +decrypted); System.out.println("");
}
}