· 7 years ago · Jun 10, 2018, 10:32 AM
1import java.io.*;
2import java.security.*;
3import java.security.spec.PKCS8EncodedKeySpec;
4import java.security.spec.X509EncodedKeySpec;
5import javax.crypto.*;
6import javax.crypto.spec.DESKeySpec;
7import sun.misc.BASE64Decoder;
8import sun.misc.BASE64Encoder;
9
10public class Test
11{
12 String name = "RSA";
13
14 public static void main(String[] args)
15 {
16 String NewKey = "TossFjqLR1PHZ9JWsGnvG9gDzcSwPJ3D7rk9UQEnmLhB+Z+fAfQEdGLm7Zuur7om1i/iFCdg9lmPoEWeWzX3BZAPnMM8EiUtfUIJq7wX1C+6KjSrYbhtia2Ii2+iQsARkH5UxbEPf/389FowAZkhTpZMG4R23dgzNOGHq7ThTeA=";
17 String Key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD+aEJZI54hOGM4mQrj/CqE866oAyEkQ4hwWa/EUT+5IJjo67W8CU/2ds0s9fljODdNjodzSIYyT6jcBubXYZ1YNJfP0GtDq7S9hzBF9KngQ5bl+eafpM4G4/KAORxMzLl4eqltGVfk+shgaE4Jr0H0DOz9JGLy5loayMuLGW1R6QIDAQAB";
18 String License = "";
19 /* BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
20 try
21 {
22 System.out.print("Enter Your Key : ");
23 License = br.readLine();
24 }
25 catch (IOException ioe)
26 {
27 System.out.println("IO error trying to read Key");
28 System.exit(1);
29 }
30 System.out.println("You have entered : " + License);
31 */
32 String Contact = "CONTRACT_NUMBER:2009-84906|USER_EMAIL:f4fahmed@gmail.com|STATUS:active|EXPIRATION:2009-03-21|SUBSCRIPTION:Trial (30 days): MySQL Enterprise Platinum NMAS|LICENSES:32";
33 boolean farrukh = new Test().verify(Contact, NewKey, Key);
34 System.out.println("Result : " + farrukh);
35
36 }
37
38 public boolean verify(String text, String base64sig, String base64PubKey)
39 {
40 try
41 {
42 Signature sig = Signature.getInstance((new StringBuilder()).append("SHA1with").append(name).toString());
43 sig.initVerify((PublicKey)fromBase64(base64PubKey));
44 sig.update(text.getBytes("UTF-8"));
45 return sig.verify((new BASE64Decoder()).decodeBuffer(base64sig));
46 }
47 catch(Exception e)
48 {
49 throw new RuntimeException(e);
50 }
51 }
52
53 public PublicKey generatePublicKey(byte x509encoded[])
54 {
55 try
56 {
57 KeyFactory keyFactory = KeyFactory.getInstance(name);
58 java.security.spec.KeySpec keySpec = new X509EncodedKeySpec(x509encoded);
59 return keyFactory.generatePublic(keySpec);
60 }
61 catch(Exception e)
62 {
63 throw new RuntimeException(e);
64 }
65 }
66
67
68 public Key fromBase64(String base64encoded)
69 {
70 byte encodedBytes[] = base64decode(base64encoded);
71 return generateKey(encodedBytes);
72 }
73
74 private byte[] base64decode(String base64encoded)
75 {
76 try
77 {
78 return (new BASE64Decoder()).decodeBuffer(base64encoded);
79 }
80 catch(IOException e)
81 {
82 throw new RuntimeException(e);
83 }
84 }
85
86 public Key generateKey(byte encoded[])
87 {
88 try
89 {
90 return generateSecretKey(encoded);
91 }
92 catch(Exception e) { }
93 try
94 {
95 return generatePrivateKey(encoded);
96 }
97 catch(Exception e)
98 {
99 return generatePublicKey(encoded);
100 }
101 }
102
103 public PrivateKey generatePrivateKey(byte pkcs8encoded[])
104 {
105 try
106 {
107 KeyFactory keyFactory = KeyFactory.getInstance(name);
108 java.security.spec.KeySpec keySpec = new PKCS8EncodedKeySpec(pkcs8encoded);
109 return keyFactory.generatePrivate(keySpec);
110 }
111 catch(Exception e)
112 {
113 throw new RuntimeException(e);
114 }
115 }
116
117 public SecretKey generateSecretKey(byte rawEncoded[])
118 {
119 try
120 {
121 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(name);
122 java.security.spec.KeySpec desKeySpec = new DESKeySpec(rawEncoded);
123 return keyFactory.generateSecret(desKeySpec);
124 }
125 catch(Exception e)
126 {
127 throw new RuntimeException(e);
128 }
129 }
130
131}
132
133class Algorithm
134{
135
136 public Algorithm(String name)
137 {
138 this.name = name;
139 }
140
141 public String toString()
142 {
143 return name;
144 }
145
146 public KeyPairGenerator keyPairGenerator()
147 {
148 try
149 {
150 return KeyPairGenerator.getInstance(name);
151 }
152 catch(NoSuchAlgorithmException e)
153 {
154 throw new RuntimeException(e);
155 }
156 }
157
158 public KeyGenerator keyGenerator()
159 {
160 try
161 {
162 return KeyGenerator.getInstance(name);
163 }
164 catch(NoSuchAlgorithmException e)
165 {
166 throw new RuntimeException(e);
167 }
168 }
169
170 public Key generateKey(byte encoded[])
171 {
172 try
173 {
174 return generateSecretKey(encoded);
175 }
176 catch(Exception e) { }
177 try
178 {
179 return generatePrivateKey(encoded);
180 }
181 catch(Exception e)
182 {
183 return generatePublicKey(encoded);
184 }
185 }
186
187 public PrivateKey generatePrivateKey(byte pkcs8encoded[])
188 {
189 try
190 {
191 KeyFactory keyFactory = KeyFactory.getInstance(name);
192 java.security.spec.KeySpec keySpec = new PKCS8EncodedKeySpec(pkcs8encoded);
193 return keyFactory.generatePrivate(keySpec);
194 }
195 catch(Exception e)
196 {
197 throw new RuntimeException(e);
198 }
199 }
200
201 public PublicKey generatePublicKey(byte x509encoded[])
202 {
203 try
204 {
205 KeyFactory keyFactory = KeyFactory.getInstance(name);
206 java.security.spec.KeySpec keySpec = new X509EncodedKeySpec(x509encoded);
207 return keyFactory.generatePublic(keySpec);
208 }
209 catch(Exception e)
210 {
211 throw new RuntimeException(e);
212 }
213 }
214
215 public SecretKey generateSecretKey(byte rawEncoded[])
216 {
217 try
218 {
219 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(name);
220 java.security.spec.KeySpec desKeySpec = new DESKeySpec(rawEncoded);
221 return keyFactory.generateSecret(desKeySpec);
222 }
223 catch(Exception e)
224 {
225 throw new RuntimeException(e);
226 }
227 }
228
229 public boolean verify(String text, String base64sig, String base64PubKey)
230 {
231 try
232 {
233 Signature sig = Signature.getInstance((new StringBuilder()).append("SHA1with").append(name).toString());
234 sig.initVerify((PublicKey)fromBase64(base64PubKey));
235 sig.update(text.getBytes("UTF-8"));
236 return sig.verify((new BASE64Decoder()).decodeBuffer(base64sig));
237 }
238 catch(Exception e)
239 {
240 throw new RuntimeException(e);
241 }
242 }
243
244 public Key fromFile(File keyFile)
245 {
246 return null;//fromBase64(Files.asString(keyFile));
247 }
248
249 public Key fromBase64(String base64encoded)
250 {
251 byte encodedBytes[] = base64decode(base64encoded);
252 return generateKey(encodedBytes);
253 }
254
255 private byte[] base64decode(String base64encoded)
256 {
257 try
258 {
259 return (new BASE64Decoder()).decodeBuffer(base64encoded);
260 }
261 catch(IOException e)
262 {
263 throw new RuntimeException(e);
264 }
265 }
266
267 public static void toFile(Key key, File file)
268 {
269 String base64encoded = (new BASE64Encoder()).encode(key.getEncoded());
270 //File.writeToFile(file, base64encoded);
271 }
272
273 public static String b64sha1(String input)
274 {
275 return (new BASE64Encoder()).encode(sha1(input));
276 }
277
278 public static byte[] sha1(String input)
279 {
280 try
281 {
282 byte inBytes[] = input.getBytes("UTF-8");
283 return MessageDigest.getInstance("SHA-1").digest(inBytes);
284 }
285 catch(Exception e)
286 {
287 throw new RuntimeException(e);
288 }
289 }
290
291 public static final Algorithm RSA = new Algorithm("RSA");
292 public static final Algorithm DSA = new Algorithm("DSA");
293 public static final Algorithm DES = new Algorithm("DES");
294 private String name;
295
296}
297/*
298MySQL Key File Content
299----------------------
300<?xml version="1.0"?>
301<ACCOUNTS>
302<ACCOUNT>
303 <CONTRACT_NUMBER>2006-315048</CONTRACT_NUMBER>
304 <USER_EMAIL>klufa@lanius.cz</USER_EMAIL>
305 <STATUS>active</STATUS>
306 <EXPIRATION>2007-12-05</EXPIRATION>
307 <SUBSCRIPTION>MySQL Enterprise Silver</SUBSCRIPTION>
308 <LICENSES>1</LICENSES>
309 <SIGNATURE>9DpxyHSbHp5Y0yj8KnmoZQz9VsWeOUBq86RcdUw8dOdrr/B/IgU9t6JtKIEmgNmozbyNU/kt2838G67XzViple6XOtTHFsVXvxGRpFuGXRaV8tEkcMRmWyZiztG+dqrUW+Qa15kdoxw1AKygZX6f7jOWqPnOQdnpIL8v16hWZKY=</SIGNATURE>
310</ACCOUNT>
311</ACCOUNTS>
312*/