· 7 years ago · Nov 27, 2018, 12:44 PM
1String password = "s5A[=a";
2
3public String getHashOfPassword(String passwordToHash){
4 String generatedHash = null;
5 try {
6 MessageDigest md = MessageDigest.getInstance("SHA-512");
7 byte[] bytes = md.digest(passwordToHash.getBytes(StandardCharsets.UTF_8));
8 StringBuilder sb = new StringBuilder();
9 for(int i=0; i< bytes.length ;i++){
10 sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
11 }
12 generatedHash = sb.toString();
13 }
14 catch (NoSuchAlgorithmException e){
15
16 e.printStackTrace();
17 }
18 return generatedHash;
19 }
20
21public static byte[] generateSymmkey() throws UnrecoverableKeyException {
22 Key key = null;
23 try {
24 KeyStore keyStore = KeyStore.getInstance("JCEKS");
25 FileInputStream stream = new FileInputStream("C:\Users\AlokK\Documents\keystore\aes-keystore.jck");
26 keyStore.load(stream, "mystorepass".toCharArray());
27 key = keyStore.getKey("jceksaes", "mykeypass".toCharArray());
28 } catch (Exception e) {
29 e.printStackTrace();
30
31 }
32 return key.getEncoded();
33}
34
35public static String encryptWithAESKey(String data, byte[] key)
36 throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
37 BadPaddingException, UnsupportedEncodingException {
38 String encryptedText = null;
39 try {
40 SecretKey secKey = new SecretKeySpec(key, "AES");
41
42 Cipher cipher = Cipher.getInstance("AES");
43
44 cipher.init(Cipher.ENCRYPT_MODE, secKey);
45 byte[] newDataByte = data.getBytes("UTF8");
46 byte[] newData = cipher.doFinal(newDataByte);
47 encryptedText = Base64.getEncoder().encodeToString(newData);
48 } catch (Exception e) {
49 e.printStackTrace();
50
51 }
52 return encryptedText;
53}
54
55public static String decryptWithAESKey(String inputData, byte[] key)
56 throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
57 BadPaddingException, UnsupportedEncodingException {
58 Cipher cipher = Cipher.getInstance("AES");
59 SecretKey secKey = new SecretKeySpec(key, "AES");
60
61 cipher.init(Cipher.DECRYPT_MODE, secKey);
62 byte[] newData = cipher.doFinal((Base64.getDecoder().decode(inputData.getBytes())));
63 return new String(newData, "UTF8");
64
65}
66
67public static void main(String[] args) {
68 byte[] key = null;
69 try {
70 key = generateSymmkey();
71 } catch (UnrecoverableKeyException e2) {
72 // TODO Auto-generated catch block
73 e2.printStackTrace();
74 }
75 String data = "vswvrgwrgrwrgrrgrr";
76
77 // Encrypt Data
78 String encryptedData = null;
79 try {
80 encryptedData = encryptWithAESKey(data, key);
81 } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException
82 | BadPaddingException | UnsupportedEncodingException e1) {
83 // TODO Auto-generated catch block
84 e1.printStackTrace();
85 }
86
87 System.out.println("Encrypted Data : " + encryptedData);
88
89 // Decrypt Data
90 try {
91 System.out.println("Decrypted Data : " + decryptWithAESKey(encryptedData, key));
92 } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException
93 | BadPaddingException | UnsupportedEncodingException e) {
94 // TODO Auto-generated catch block
95 e.printStackTrace();
96 }
97}
98
99Encrypted Data : ASS7VgJ+oFi9opJXg8cA5f9C9uzUYgtVRc2Q7Y/xSPo=
100Decrypted Data : vswvrgwrgrwrgrrgrr