Java实现加密

DES加密示例

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

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.security.Key;

public class DESUtil {
    // 加密算法
    private static final String ALGORITHM = "DES";
    // 编码方式
    private static final Charset CHARSET = Charset.forName("UTF-8");
    // 加密/解密算法-工作模式-填充模式
    private static final String CIPHER_ALGORITHM = "DES/CBC/PKCS5Padding";

    /**
     * 根据desKey, 生成secretKey
     * @param desKey
     * @return
     * @throws Exception
     */
    public static Key generateKey(String desKey) throws Exception {
        DESKeySpec dks = new DESKeySpec(desKey.getBytes(CHARSET));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        return keyFactory.generateSecret(dks);
    }

    /**
     * base64编码, 生成secretKey字符串
     * 如果desKey是固定的,那么可以提前算好secretKey字符串,不用每次都生成
     * @param desKey
     * @return
     * @throws Exception
     */
    public static String generateKeyStr(String desKey) throws Exception {
        DESKeySpec dks = new DESKeySpec(desKey.getBytes(CHARSET));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        SecretKey secretKey = keyFactory.generateSecret(dks);
        byte[] encoded = secretKey.getEncoded();
        return Base64.encodeBase64String(encoded);  // 将密钥base64编码
    }

    /**
     * 加密
     * @param data 要加密的数据
     * @param secret (base64)secretKey
     * @param ivParam 偏移量
     * @return
     */
    public static String encrypt(String data, String secret, String ivParam){
        if (data == null)
            return null;
        try {
            byte[] desKeyBytes = Base64.decodeBase64(secret);
            SecretKeySpec secretKey = new SecretKeySpec(desKeyBytes, ALGORITHM);

//            Key secretKey = generateKey();
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(ivParam.getBytes(CHARSET));
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);

            byte[] bytes = cipher.doFinal(data.getBytes(CHARSET));
            return Base64.encodeBase64String(bytes);
        } catch (Exception e) {
            e.printStackTrace();
            return data;
        }
    }

    /**
     * 解密
     * @param data 要解密的密文
     * @param secret (base64)secretKey
     * @param ivParam 偏移量
     * @return
     */
    public static String decrypt(String data, String secret, String ivParam){
        if (data == null)
            return null;
        try {
            byte[] desKeyBytes = Base64.decodeBase64(secret);
            SecretKeySpec secretKey = new SecretKeySpec(desKeyBytes, ALGORITHM);

//            Key secretKey = generateKey();
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(ivParam.getBytes(CHARSET));
            cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);

            byte[] bytes = Base64.decodeBase64(data.getBytes(CHARSET));
            return new String(cipher.doFinal(bytes), CHARSET);
        } catch (Exception e) {
            e.printStackTrace();
            return data;
        }
    }
}


其他文档:
Java实现加密

你可能感兴趣的:(Java实现加密)