· 7 years ago · Jun 06, 2018, 11:10 AM
1package test;
2
3import javax.crypto.Cipher;
4import javax.crypto.SecretKey;
5import javax.crypto.spec.IvParameterSpec;
6import javax.crypto.spec.SecretKeySpec;
7import java.lang.reflect.Array;
8import java.security.MessageDigest;
9import java.util.Base64;
10import java.security.MessageDigest;
11import java.util.Arrays;
12
13import javax.crypto.Cipher;
14import javax.crypto.SecretKey;
15import javax.crypto.spec.IvParameterSpec;
16import javax.crypto.spec.SecretKeySpec;
17import java.util.Base64;
18
19public class TestEncrypt {
20
21 public static void main(String[] args) throws Exception {
22 System.out.println("Cipher test");
23 String text = "{\"function\":\"GetBalance\"}";
24
25 String codedtext = null;
26 String decodedtext = null;
27 try {
28 codedtext = encrypt(text, "t6twy5-S11_75=5qy42ER6Y84r84494p");
29 decodedtext = decrypt(codedtext, "t6twy5-S11_75=5qy42ER6Y84r84494p");
30 } catch (Exception e) {
31 e.printStackTrace();
32 }
33
34 System.out.println(codedtext); // this is a byte array, you'll just see a reference to an array
35 System.out.println(decodedtext); // This correctly shows "kyle boon"
36
37 String response = "dQxMxR89iSTbEMUIYrpsn7xLfw/dgvsrcYhNEAoIE87Xch80CsC971gwhK2CiAyyYBKhLqzaMxoEd2wAGXATkwmBQpGVt+RVXPV2k95smqNoeVzV0RIoimKT9n3mJThOvOzvnV10cwetxWUuKRhL9BgzKlmIIMWGUz1nujd5FXI=";
38 System.out.println(decrypt(response, "t6twy5-S11_75=5qy42ER6Y84r84494p"));
39 }
40
41 public static String encrypt(String message, String key) throws Exception {
42 final MessageDigest md = MessageDigest.getInstance("md5");
43 final byte[] digestOfPassword = md.digest(key.getBytes("utf-8"));
44 final byte[] keyBytes = new byte[digestOfPassword.length + 8];
45 for (int i = 0; i< keyBytes.length; i++) {
46 int j = (i >= digestOfPassword.length ? i - digestOfPassword.length : i);
47 keyBytes[i] = digestOfPassword[j];
48 }
49
50 final SecretKey skey = new SecretKeySpec(keyBytes, "DESede");
51 final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
52 final Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
53 cipher.init(Cipher.ENCRYPT_MODE, skey);
54
55 final byte[] plainTextBytes = message.getBytes("utf-8");
56 final byte[] cipherText = cipher.doFinal(plainTextBytes);
57 final String encodedCipherText = Base64.getEncoder().encodeToString(cipherText);
58
59 return encodedCipherText;
60 }
61
62
63 public static String decrypt(String base64Msg, String key) throws Exception {
64 final MessageDigest md = MessageDigest.getInstance("md5");
65 final byte[] digestOfPassword = md.digest(key.getBytes("utf-8"));
66 final byte[] keyBytes = new byte[digestOfPassword.length + 8];
67 for (int i = 0; i < keyBytes.length; i++) {
68 int j = (i >= digestOfPassword.length ? i - digestOfPassword.length : i);
69 keyBytes[i] = digestOfPassword[j];
70 }
71
72 final SecretKey skey = new SecretKeySpec(keyBytes, "DESede");
73 final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
74 final Cipher decipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
75 decipher.init(Cipher.DECRYPT_MODE, skey);
76
77 // final byte[] encData = new
78 // sun.misc.BASE64Decoder().decodeBuffer(message);
79 final byte[] plainText = decipher.doFinal(Base64.getDecoder().decode(base64Msg));
80
81 return new String(plainText, "UTF-8");
82 }
83}