des加密,url编码,url解码,des解密 DES加解密及Wrong key size错误处理

des加密,url编码,url解码,des解密 DES加解密及Wrong key size错误处理

package com.example.core.mydemo.des;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.SecureRandom;

/**
 原始字符串 = {"phone":"1391111","username":"张三"}
 des加密 = 6bdoWaZe5426HaBBl2WdWDVm0iCy84Sl+O2lPiUvveozAnV1Scn6Tw==
 url编码 = 6bdoWaZe5426HaBBl2WdWDVm0iCy84Sl%2BO2lPiUvveozAnV1Scn6Tw%3D%3D
 url解码 = 6bdoWaZe5426HaBBl2WdWDVm0iCy84Sl%2BO2lPiUvveozAnV1Scn6Tw%3D%3D
 des解密 = {"phone":"1391111","username":"张三"}
 */
public class DesUtls {
    private static String CHARSETNAME="UTF-8";

    /**
     * DES加解密及Wrong key size错误处理
     * @param key
     * @return
     * @throws UnsupportedEncodingException
     */
    private static byte[] getKeyBytes(String key) throws UnsupportedEncodingException {
        byte[] keyBytes = key.getBytes(CHARSETNAME);
        if (keyBytes.length < 8) {
            byte[] bytes = new byte[8];
            System.arraycopy(keyBytes, 0, bytes, 0, keyBytes.length);
            keyBytes = bytes;
        }
        return keyBytes;
    }

    public static String getDESStr(String str, String encryptKey, String type, String charset) throws Exception {
        SecureRandom random = new SecureRandom();
//        DESKeySpec desKey = new DESKeySpec(encryptKey.getBytes());
        DESKeySpec desKey = new DESKeySpec(getKeyBytes(encryptKey));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey securekey = keyFactory.generateSecret(desKey);
        Cipher cipher = Cipher.getInstance("DES");

        //加密
        if ("ENCRYPT".equals(type)) {
            //警告: BASE64Decoder是内部专用 API, 可能会在未来发行版中删除
//            BASE64Encoder base64encoder = new BASE64Encoder();
            cipher.init(1, securekey, random);
//            return base64encoder.encode(cipher.doFinal(str.getBytes(charset)));
            return Base64Encoder.encode(cipher.doFinal(str.getBytes(charset)));
        }else if ("DECRYPT".equals(type)) {
            //警告: BASE64Decoder是内部专用 API, 可能会在未来发行版中删除
//        BASE64Decoder base64decoder = new BASE64Decoder();
//        byte[] encodeByte = base64decoder.decodeBuffer(str);
            byte[] encodeByte = Base64Encoder.decode(str.getBytes());
            cipher.init(2, securekey, random);
            byte[] decoder = cipher.doFinal(encodeByte);
            return new String(decoder, charset);
        }
        return "type error";
    }

    public static void main(String[] args) {
        String encryptKey = "1";
        String type = "ENCRYPT";
        String charset = "UTF-8";
        String str = "{\"phone\":\"1391111\",\"username\":\"张三\"}";

        System.out.println("原始字符串 = " + str);
        try {
            //des加密
            String DESStr= DesUtls.getDESStr(str,encryptKey,type,charset);
            System.out.println("des加密 = " + DESStr);
            //url编码
            DESStr = URLEncoder.encode(DESStr,"UTF-8");
            System.out.println("url编码 = " + DESStr);

            //url解码
            String s = URLDecoder.decode(DESStr,"UTF-8");
            System.out.println("url解码 = " + DESStr);
            //javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
            //des解密
            System.out.println("des解密 = " + getDESStr(s,encryptKey,"DECRYPT","UTF-8"));
            //ENCRYPT(des加密)  encode(url编码)  >> decode(url解码)  encrypt(des解密)

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}



package com.example.core.mydemo.des;

import org.apache.commons.codec.binary.Base64;

public class Base64Encoder {
    /**
     * @param bytes
     * @return
     */
    public static byte[] decode(final byte[] bytes) {
        return Base64.decodeBase64(bytes);
    }

    /**
     * 二进制数据编码为BASE64字符串
     *
     * @param bytes
     * @return
     * @throws Exception
     */
    public static String encode(final byte[] bytes) {
        return new String(Base64.encodeBase64(bytes));
    }
}

 

posted on 2023-12-20 22:50  oktokeep  阅读(79)  评论(1编辑  收藏  举报