java 局域網生成外網短鏈接

一、ShortUrlHelper.java

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import java.io.IOException;
import java.net.URLEncoder;
import java.security.*;
public class ShortUrlHelper {
private final static String DES = "DES";
    private final static String ENCODE = "GBK";


    /**
     * Description 根據鍵值進行加密
     * @param data 待加密數據
     * @param key 密鑰
     * @return
     * @throws Exception
     */
    public static String encrypt(String data, String key) throws Exception {
        byte[] bt = encrypt(data.getBytes(ENCODE), key.getBytes(ENCODE));
        String strs = new BASE64Encoder().encode(bt);
        strs = strs.replaceAll("\n","72Fn");
        strs = strs.replaceAll("\r","82Fr");
        return strs;
    }

    /**
     * 根據鍵值進行解密
     * @param data 待解密數據
     * @param key    密鑰
     * @return
     * @throws IOException
     * @throws Exception
     */
    public static String decrypt(String data, String key) throws IOException,Exception {
        if (data == null)
            return null;
        data = URLEncoder.encode(data,"UTF-8");;
        data = data.replace("%2F","/").replace("%3D","=").replace("72Fn","\n").replace("82Fr","\r");
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] buf = decoder.decodeBuffer(data);
        byte[] bt = decrypt(buf, key.getBytes(ENCODE));
        return new String(bt, ENCODE);
    }

    /**
     * Description 根據鍵值進行加密
     *
     * @param data
     * @param key
     *            加密鍵byte數組
     * @return
     * @throws Exception
     */
    private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        // 生成一個可信任的隨機數源
        SecureRandom sr = new SecureRandom();
        // 從原始密鑰數據創建DESKeySpec對象
        DESKeySpec dks = new DESKeySpec(key);
        // 創建一個密鑰工廠,然後用它把DESKeySpec轉換成SecretKey對象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
        // Cipher對象實際完成加密操作
        Cipher cipher = Cipher.getInstance(DES);
        // 用密鑰初始化Cipher對象
        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);

        return cipher.doFinal(data);
    }

    /**
     * Description 根據鍵值進行解密
     *
     * @param data
     * @param key 加密鍵byte數組
     * @return
     * @throws Exception
     */
    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        // 生成一個可信任的隨機數源
        SecureRandom sr = new SecureRandom();
        // 從原始密鑰數據創建DESKeySpec對象
        DESKeySpec dks = new DESKeySpec(key);
        // 創建一個密鑰工廠,然後用它把DESKeySpec轉換成SecretKey對象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
        // Cipher對象實際完成解密操作
        Cipher cipher = Cipher.getInstance(DES);
        // 用密鑰初始化Cipher對象
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);

        return cipher.doFinal(data);
    }
}

二、外網

外網接口需要 

URLEncoder.encode(json,"utf-8"); 然後在解密
ShortUrlHelper.decrypt(json,"祕鑰");

以上就詮釋了 內網生成短鏈接,用手機掃碼對外網進行訪問。可能會出現 網絡符號,所以要進行 URLEncoder

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章