玩瞳文档中心


Java加密参考

<pre><code class="language-java">```java public class LicenseManagement { private static License licenseCache = null; private static final String KEY = &amp;quot;d@IGETL9X*J1fc6c&amp;quot;; private static final String IV = &amp;quot;HteS1cA6956gNtQG&amp;quot;; // license: 找商务提供的app静态license数据 // deviceID: 设备唯一标识,设备id或设备SN可以表示机器唯一编码即可,同一设备id只能同时有一个token,如混合会导致错乱或不通 // model: 设备id对应的机器型号 public static License parse(String license, String deviceID, String model) throws LicenseParseException{ if(licenseCache != null) { return licenseCache; } License licenseObj = new License(); try { String authLicense = Base64Utils.decodeBase64(license); authLicense = AESUtils.decrypt(authLicense, KEY, IV); String[] texts = authLicense.split(&amp;quot;\\|&amp;quot;); String appCode = texts[0]; authLicense = AESUtils.encrypt(texts[0]+&amp;quot;&amp;amp;_split_&amp;amp;&amp;quot;+deviceID+&amp;quot;&amp;amp;_split_&amp;amp;&amp;quot;+model, texts[1], texts[2]); licenseObj.setAuthCode(appCode); licenseObj.setLicense(authLicense); licenseCache = licenseObj; }catch(Exception ex) { throw new LicenseParseException(&amp;quot;Invalid license.&amp;quot;); } return licenseObj; } }</code></pre> <h5>工具类 Base64Utils</h5> <pre><code class="language-java">package com.wantong.auth.util; import java.lang.reflect.Method; public class Base64Utils { /*** * decode by Base64 */ public static String decodeBase64(String input) { Class clazz; Object retObj = null; String result = null; try { clazz = Class.forName(&amp;quot;com.sun.org.apache.xerces.internal.impl.dv.util.Base64&amp;quot;); Method mainMethod = clazz.getMethod(&amp;quot;decode&amp;quot;, String.class); mainMethod.setAccessible(true); retObj = mainMethod.invoke(null, input); result = new String((byte[]) retObj, &amp;quot;UTF-8&amp;quot;); } catch (Exception e) { e.printStackTrace(); } return result; } } </code></pre> <h5>工具类 AESUtils</h5> <pre><code>package com.wantong.auth.util; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class AESUtils { public static String encrypt(String text, String key, String iv) { byte[] encrypted = null; try { IvParameterSpec ivParam = new IvParameterSpec(iv.getBytes()); Cipher cipher = Cipher.getInstance(&amp;quot;AES/CBC/PKCS5Padding&amp;quot;); SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), &amp;quot;AES&amp;quot;); cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivParam); encrypted = cipher.doFinal(text.getBytes(&amp;quot;UTF-8&amp;quot;)); } catch (Exception e) { return null; } return bytesToHexString(encrypted).toUpperCase(); } public static String decrypt(String code, String key, String iv) { byte[] decrypted = null; try { IvParameterSpec ivParam = new IvParameterSpec(iv.getBytes()); Cipher cipher = Cipher.getInstance(&amp;quot;AES/CBC/PKCS5Padding&amp;quot;); SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), &amp;quot;AES&amp;quot;); cipher.init(Cipher.DECRYPT_MODE, keyspec, ivParam); decrypted = cipher.doFinal(hexStringToBytes(code.toLowerCase())); return new String(decrypted, &amp;quot;UTF-8&amp;quot;); } catch (Exception e) { return null; } } public static String bytesToHexString(byte[] src){ StringBuilder stringBuilder = new StringBuilder(&amp;quot;&amp;quot;); if (src == null || src.length &amp;lt;= 0) { return null; } for (int i = 0; i &amp;lt; src.length; i++) { int v = src[i] &amp;amp; 0xFF; String hv = Integer.toHexString(v); if (hv.length() &amp;lt; 2) { stringBuilder.append(0); } stringBuilder.append(hv); } return stringBuilder.toString(); } public static byte[] hexStringToBytes(String hexString) { if (hexString == null || hexString.equals(&amp;quot;&amp;quot;)) { return null; } hexString = hexString.toUpperCase(); int length = hexString.length() / 2; char[] hexChars = hexString.toCharArray(); byte[] d = new byte[length]; for (int i = 0; i &amp;lt; length; i++) { int pos = i * 2; d[i] = (byte) (charToByte(hexChars[pos]) &amp;lt;&amp;lt; 4 | charToByte(hexChars[pos + 1])); } return d; } private static byte charToByte(char c) { return (byte) &amp;quot;0123456789ABCDEF&amp;quot;.indexOf(c); } } </code></pre> <pre><code></code></pre>

页面列表

ITEM_HTML