-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDecryptor.java
More file actions
86 lines (67 loc) · 2.75 KB
/
Decryptor.java
File metadata and controls
86 lines (67 loc) · 2.75 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package GeneralPackage;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class Decryptor {
private static SecretKeySpec secretKey;
private static byte[] key;
public static String Decrypt(String strToDecrypt) {
MessageDigest sha = null;
try {
String secret = "secretKey";
key = secret.getBytes("UTF-8");
sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key, 16);
secretKey = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
catch (Exception e) {
System.out.println("Error while decrypting: " + e.toString());
}
return null;
}
/* public static String Encrypt(String strToEncrypt) {
MessageDigest sha = null;
try {
String secret = "secretKey";
key = secret.getBytes("UTF-8");
sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key, 16);
secretKey = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
catch (Exception e) {
System.out.println("Error while encrypting: " + e.toString());
}
return null;
}
public static void main(String[] args) {
String originalString = "enter string to encrypt";
String encryptedString = Decryptor.Encrypt(originalString);
String decryptedString = Decryptor.Decrypt(encryptedString);
System.out.println(originalString);
System.out.println(encryptedString);
}*/
}