· 6 years ago · Jun 27, 2019, 05:04 PM
1public static String encrypt(String value, char[] secret) {
2 try {
3 final byte[] bytes = value != null ? value.getBytes(StandardCharsets.UTF_8) : new byte[0];
4 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
5 SecretKey key = keyFactory.generateSecret(new PBEKeySpec(secret));
6 Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
7 pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(IsoGame.$().crossPlatformManager.getCrossPlatformUtilsInstance().getDeviceUniqueIdentifier().getBytes(StandardCharsets.UTF_8), 20));
8 return new String(Base64.encodeBase64(pbeCipher.doFinal(bytes)), StandardCharsets.UTF_8);
9
10 } catch (Exception e) {
11 e.printStackTrace();
12 }
13 return value;
14
15 }
16
17public static String decrypt(String value, char[] secret) {
18 try {
19 final byte[] bytes = value != null ? Base64.decodeBase64(value.getBytes(StandardCharsets.UTF_8)) : new byte[0];
20 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
21 SecretKey key = keyFactory.generateSecret(new PBEKeySpec(secret));
22 Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
23 pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(IsoGame.$().crossPlatformManager.getCrossPlatformUtilsInstance().getDeviceUniqueIdentifier().getBytes(StandardCharsets.UTF_8), 20));
24 return new String(pbeCipher.doFinal(bytes), StandardCharsets.UTF_8);
25
26 } catch (Exception e) {
27 e.printStackTrace();
28 }
29 return value;
30
31 }
32
33pbeCipher.doFinal(bytes)