· 9 years ago · Dec 12, 2016, 05:42 PM
1private static byte[] encryptPassword(byte[] buff) throws UnsupportedEncodingException, UnsupportedOperationException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, InvalidParameterSpecException, ProviderException, InvalidAlgorithmParameterException {
2
3 JsonReader jr = new JsonReader(new InputStreamReader(new ByteArrayInputStream(buff), "UTF-8"));
4 JsonParser parser = new JsonParser();
5 JsonElement data = parser.parse(jr);
6 byte[] bt = null;
7
8 if (data.isJsonObject()) {
9
10 JsonObject json = data.getAsJsonObject();
11
12 if (json.has("msg")) {
13
14 JsonElement cmd = json.get("msg");
15 String msg = cmd.getAsString();
16 cmd = json.get("dst");
17 dst = cmd.getAsString();
18
19 Random r = new SecureRandom();
20 byte[] salt = new byte[20];
21 r.nextBytes(salt);
22 char[] password = pass1.toCharArray();
23
24 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
25 KeySpec spec = new PBEKeySpec(password, salt, 65536, 128);
26 SecretKey tmp = factory.generateSecret(spec);
27 SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
28
29 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
30 cipher.init(Cipher.ENCRYPT_MODE, secret);
31 AlgorithmParameters params = cipher.getParameters();
32 byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
33 byte[] ciphertext = cipher.doFinal(msg.getBytes("UTF-8"));
34 byte[] secretArray = secret.getEncoded();
35
36 //encriptação da chave
37 //byte[] b = encryptPwd(secretArray);
38 String saltBase64 = Base64.encode(salt);
39 String cipherBase64 = Base64.encode(ciphertext);
40 String secretBase64 = Base64.encode(secretArray);
41 String ivBase64 = Base64.encode(iv);
42 json.remove("msg");
43 json.addProperty("msg", cipherBase64);
44 json.addProperty("key", secretBase64);
45 json.addProperty("iv", ivBase64);
46 json.addProperty("salt", saltBase64);
47 json.addProperty("type", 0);
48 bt = json.toString().getBytes();
49 return bt;
50 }
51 }
52 return buff;
53 }
54
55 private static byte[] decryptPassword(byte[] dados) throws UnsupportedEncodingException, UnsupportedOperationException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, InvalidParameterSpecException, ProviderException, InvalidAlgorithmParameterException {
56
57 JsonReader jr = new JsonReader(new InputStreamReader(new ByteArrayInputStream(dados), "UTF-8"));
58 JsonParser parser = new JsonParser();
59 JsonElement data = parser.parse(jr);
60 byte[] bt = null;
61
62 if (data.isJsonObject()) {
63 JsonObject json = data.getAsJsonObject();
64 if (json.has("msg")) {
65 JsonElement cmd = json.get("msg");
66 String msg = cmd.getAsString();
67 cmd = json.get("iv");
68
69 String iv = cmd.getAsString();
70 cmd = json.get("salt");
71 String saltEnc = cmd.getAsString();
72 byte[] saltDec = Base64.decode(saltEnc);
73 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
74 char[] password = pass1.toCharArray();
75 KeySpec spec = new PBEKeySpec(password, saltDec, 65536, 128);
76 SecretKey tmp = factory.generateSecret(spec);
77 SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
78
79// SecretKeySpec sks = new SecretKeySpec(Base64.decode(secret), "AES");
80 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
81 cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(Base64.decode(iv)));
82 String plaintext = new String(cipher.doFinal(Base64.decode(msg)), "UTF-8");
83 json.remove("msg");
84 json.addProperty("msg", plaintext);
85 bt = json.toString().getBytes();
86 return bt;
87 }
88 }
89 return dados;
90 }