-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncrytDecrypt.java
83 lines (77 loc) · 2.51 KB
/
EncrytDecrypt.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
/*import java.io.BufferedReader;
import java.io.InputStreamReader;*/
import java.util.ArrayList;
import java.util.List;
class EncryptDecrypt {
private static final String UNICODE_FORMAT = "UTF-8";
public static void main(String[] args) throws Exception
{
/*BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
/*String data = bufferedReader.readLine();
/*System.out.println("Original: "+data);
EncryptDecrypt encryptDecrypt = new EncryptDecrypt();
System.out.println(encryptDecrypt.encrypt(data));*/
}
public List<key> encrypt(String DATA)
{
try
{
SecretKey secrKey = generateKey("AES");
Cipher cipher = Cipher.getInstance("AES");
byte[] encryptedData = encryptString(DATA, secrKey,cipher);
String encryptedString = new String(encryptedData);
List<key> list = new ArrayList<key>();
list.add(new key(encryptedString, cipher, secrKey, encryptedData));
return list;
}
catch (Exception e)
{
List<key> list = new ArrayList<key>();
list.add(new key(e));
return list;
}
}
public static SecretKey generateKey(String encryptionType)
{
try
{
KeyGenerator keyGenerator = KeyGenerator.getInstance(encryptionType);
SecretKey secretKey = keyGenerator.generateKey();
return secretKey;
}
catch (Exception e)
{
return null;
}
}
public static byte[] encryptString(String dataToEncrypt, SecretKey secretKey, Cipher cipher)
{
try {
byte[] bytes = dataToEncrypt.getBytes(UNICODE_FORMAT);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] textEncrypted = cipher.doFinal(bytes);
return textEncrypted;
}
catch (Exception e)
{
return null;
}
}
public static String decryptString(byte[] dataToDecrypt, SecretKey secretKey, Cipher cipher)
{
try {
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] textDecrypted = cipher.doFinal(dataToDecrypt);
String result = new String(textDecrypted);
return result;
}
catch (Exception e)
{
System.out.println(e);
return null;
}
}
}