¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.lf.server.helper; |
| | | |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | |
| | | import javax.crypto.Cipher; |
| | | import javax.crypto.spec.IvParameterSpec; |
| | | import javax.crypto.spec.SecretKeySpec; |
| | | import java.nio.charset.Charset; |
| | | |
| | | /** |
| | | * 许å¯å¸®å©ç±» |
| | | * @author WWW |
| | | */ |
| | | public class LicHelper { |
| | | private String a; |
| | | private String b; |
| | | private static String c; |
| | | private static String d; |
| | | private static String e; |
| | | private static final Logger f; |
| | | |
| | | static { |
| | | c = "utf-8"; |
| | | d = "AES"; |
| | | e = "AES/CBC/PKCS5Padding"; |
| | | f = LoggerFactory.getLogger(LicHelper.class); |
| | | } |
| | | |
| | | private IvParameterSpec a() { |
| | | try { |
| | | return new IvParameterSpec(this.b.getBytes(c)); |
| | | } catch (Exception ex) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | public final String a(final String s) { |
| | | String str = null; |
| | | try { |
| | | // final Cipher instance |
| | | // (instance = Cipher.getInstance(e)).init(1, new SecretKeySpec(this.a.getBytes(c), d), this.a()) |
| | | // str = a(instance.doFinal(s.getBytes(Charset.forName(c)))) |
| | | |
| | | Cipher instance = Cipher.getInstance(e); |
| | | IvParameterSpec ips = this.a(); |
| | | SecretKeySpec sks = new SecretKeySpec(this.a.getBytes(c), d); |
| | | instance.init(1, sks, ips); |
| | | |
| | | byte[] bytes = s.getBytes(Charset.forName(c)); |
| | | str = a(instance.doFinal(bytes)); |
| | | } catch (Exception ex) { |
| | | System.out.println(ex.getStackTrace()); |
| | | } |
| | | return str; |
| | | } |
| | | |
| | | public final String b(final String s) { |
| | | String s2 = null; |
| | | try { |
| | | final Cipher instance; |
| | | (instance = Cipher.getInstance(e)).init(2, new SecretKeySpec(this.a.getBytes(c), d), this.a()); |
| | | s2 = new String(instance.doFinal(this.e(s)), c); |
| | | } catch (Exception ex) { |
| | | String str = ex.getMessage(); |
| | | } |
| | | return s2; |
| | | } |
| | | |
| | | private static String a(final byte[] array) { |
| | | final StringBuilder sb = new StringBuilder(); |
| | | for (int i = 0; i < array.length; ++i) { |
| | | final String hexString; |
| | | if ((hexString = Integer.toHexString(array[i] & 0xFF)).length() < 2) { |
| | | sb.append("0"); |
| | | } |
| | | sb.append(hexString); |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | |
| | | private byte[] e(String upperCase) { |
| | | final int n = (upperCase = upperCase.toUpperCase()).length() / 2; |
| | | final char[] charArray = upperCase.toCharArray(); |
| | | final byte[] array = new byte[n]; |
| | | for (int i = 0; i < n; ++i) { |
| | | final int n2 = i << 1; |
| | | array[i] = (byte) ((byte) "0123456789ABCDEF".indexOf(charArray[n2]) << 4 | (byte) "0123456789ABCDEF".indexOf(charArray[n2 + 1])); |
| | | } |
| | | return array; |
| | | } |
| | | |
| | | public final void c(String string) { |
| | | final String substring = (string = string).substring(2, 5); |
| | | final StringBuilder sb; |
| | | (sb = new StringBuilder(string)).replace(2, 5, string.substring(7, 10)); |
| | | sb.replace(7, 10, substring); |
| | | string = sb.toString(); |
| | | this.a = string; |
| | | } |
| | | |
| | | public final void d(final String b) { |
| | | this.b = b; |
| | | } |
| | | } |