Java加密参考
<pre><code class="language-java">```java
public class LicenseManagement {
private static License licenseCache = null;
private static final String KEY = &quot;d@IGETL9X*J1fc6c&quot;;
private static final String IV = &quot;HteS1cA6956gNtQG&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(&quot;\\|&quot;);
String appCode = texts[0];
authLicense = AESUtils.encrypt(texts[0]+&quot;&amp;_split_&amp;&quot;+deviceID+&quot;&amp;_split_&amp;&quot;+model, texts[1], texts[2]);
licenseObj.setAuthCode(appCode);
licenseObj.setLicense(authLicense);
licenseCache = licenseObj;
}catch(Exception ex) {
throw new LicenseParseException(&quot;Invalid license.&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(&quot;com.sun.org.apache.xerces.internal.impl.dv.util.Base64&quot;);
Method mainMethod = clazz.getMethod(&quot;decode&quot;, String.class);
mainMethod.setAccessible(true);
retObj = mainMethod.invoke(null, input);
result = new String((byte[]) retObj, &quot;UTF-8&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(&quot;AES/CBC/PKCS5Padding&quot;);
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), &quot;AES&quot;);
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivParam);
encrypted = cipher.doFinal(text.getBytes(&quot;UTF-8&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(&quot;AES/CBC/PKCS5Padding&quot;);
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), &quot;AES&quot;);
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivParam);
decrypted = cipher.doFinal(hexStringToBytes(code.toLowerCase()));
return new String(decrypted, &quot;UTF-8&quot;);
}
catch (Exception e) {
return null;
}
}
public static String bytesToHexString(byte[] src){
StringBuilder stringBuilder = new StringBuilder(&quot;&quot;);
if (src == null || src.length &lt;= 0) {
return null;
}
for (int i = 0; i &lt; src.length; i++) {
int v = src[i] &amp; 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() &lt; 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals(&quot;&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 &lt; length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) &lt;&lt; 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
private static byte charToByte(char c) {
return (byte) &quot;0123456789ABCDEF&quot;.indexOf(c);
}
}
</code></pre>
<pre><code></code></pre>