欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java DES加密解密

程序员文章站 2022-03-12 19:37:45
...
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

public class DesUtils
{
    private String desKEY;
    public DesUtils() {
    }

    public String getDesKEY() {
        return desKEY;
    }

    public void setDesKEY(String desKEY) {
        this.desKEY = desKEY;
    }

    public String encrypt(String message) throws Exception {
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        DESKeySpec desKeySpec = new DESKeySpec(desKEY.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        javax.crypto.SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        IvParameterSpec iv = new IvParameterSpec(desKEY.getBytes("UTF-8"));
        cipher.init(1, secretKey, iv);
        return toHexString(cipher.doFinal(message.getBytes("UTF-8")));
    }

    public String decrypt(String message) throws Exception {
        byte bytesrc[] = convertHexString(message);
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        DESKeySpec desKeySpec = new DESKeySpec(desKEY.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        javax.crypto.SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        IvParameterSpec iv = new IvParameterSpec(desKEY.getBytes("UTF-8"));
        cipher.init(2, secretKey, iv);
        byte retByte[] = cipher.doFinal(bytesrc);
        return new String(retByte);
    }

    private byte[] convertHexString(String ss) {
        byte digest[] = new byte[ss.length() / 2];
        for(int i = 0; i < digest.length; i++) {
            String byteString = ss.substring(2 * i, 2 * i + 2);
            int byteValue = Integer.parseInt(byteString, 16);
            digest[i] = (byte)byteValue;
        }

        return digest;
    }

    private String toHexString(byte b[]) {
        StringBuffer hexString = new StringBuffer();
        for(int i = 0; i < b.length; i++) {
            String plainText = Integer.toHexString(0xff & b[i]);
            if(plainText.length() < 2)
                plainText = (new StringBuilder("0")).append(plainText).toString();
            hexString.append(plainText);
        }

        return hexString.toString();
    }
}