· 8 years ago · Dec 28, 2017, 03:42 PM
1
2import sun.misc.BASE64Decoder;
3import sun.misc.BASE64Encoder;
4
5import javax.crypto.*;
6import javax.crypto.spec.PBEKeySpec;
7import javax.crypto.spec.PBEParameterSpec;
8import java.io.IOException;
9import java.security.InvalidAlgorithmParameterException;
10import java.security.InvalidKeyException;
11import java.security.NoSuchAlgorithmException;
12import java.security.spec.AlgorithmParameterSpec;
13import java.security.spec.InvalidKeySpecException;
14import java.security.spec.KeySpec;
15import java.util.HashMap;
16import java.util.Map;
17
18public class Bilanc {
19
20 private static class DesEncrypter {
21 Cipher ecipher;
22 Cipher dcipher;
23 byte[] salt = new byte[]{-87, -101, -56, 50, 86, 53, -29, 3};
24 int iterationCount = 19;
25
26 public DesEncrypter(String passPhrase) {
27 try {
28 KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), this.salt, this.iterationCount);
29 SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
30 this.ecipher = Cipher.getInstance(key.getAlgorithm());
31 this.dcipher = Cipher.getInstance(key.getAlgorithm());
32 AlgorithmParameterSpec paramSpec = new PBEParameterSpec(this.salt, this.iterationCount);
33 this.ecipher.init(1, key, paramSpec);
34 this.dcipher.init(2, key, paramSpec);
35 } catch (InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeySpecException e) {
36 e.printStackTrace();
37 }
38
39 }
40
41 public String encrypt(String str) {
42 try {
43 byte[] utf8 = str.getBytes("UTF8");
44 byte[] enc = this.ecipher.doFinal(utf8);
45 return (new BASE64Encoder()).encode(enc);
46 } catch (BadPaddingException | IllegalBlockSizeException ee) {
47 ee.printStackTrace();
48
49 } catch (IOException e) {
50 e.printStackTrace();
51 }
52
53 return null;
54 }
55
56 public String decrypt(String str) {
57 try {
58 byte[] dec = (new BASE64Decoder()).decodeBuffer(str);
59 byte[] utf8 = this.dcipher.doFinal(dec);
60 return new String(utf8, "UTF8");
61 } catch (BadPaddingException | IllegalBlockSizeException var4) {
62 var4.printStackTrace();
63 } catch (IOException var7) {
64 var7.printStackTrace();
65 }
66
67 return null;
68 }
69
70 public static void main(String[] args) {
71 DesEncrypter tpksolutions = new DesEncrypter("tpksolutions");
72 HashMap<String, String> licenseMap = new HashMap<>();
73
74 licenseMap.put("ClientName", "Alfred shametaj / gjirokaster");
75 licenseMap.put("FullLicense", "FullLicense");
76 licenseMap.put("StartDate", "10/06/2017");
77 licenseMap.put("EndDate", "11/06/2018");
78 licenseMap.put("LicenseType=1", "LicenseType=1");
79 licenseMap.put("MyVersion=3", "MyVersion=3");
80 licenseMap.put("Server", "JPA370H906SUJL");
81 licenseMap.put("JPA370H906SUJL", "JPA370H906SUJL");
82 licenseMap.put("NotificationDays", "10");
83 licenseMap.put("ClientID", "14405");
84 licenseMap.put("BilancPOS", "BilancPOS");
85
86 String licenseText = createLicenseText(licenseMap);
87 String encryptedLicense = tpksolutions.encrypt(licenseText);
88 System.out.println("encryptedLicense = " + encryptedLicense);
89
90 }
91
92 private static String createLicenseText(Map<String, String> licenses) {
93 String buffer = "";
94 for (Map.Entry<String, String> entry : licenses.entrySet()) {
95 String key = entry.getKey();
96 String value = entry.getValue();
97 if (key.equals(value)) {
98 buffer += key;
99 } else {
100 buffer += key + "\t" + value;
101 }
102
103 buffer += "\n";
104 }
105 return buffer;
106 }
107 }
108}