All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kaizen4j.common.encrypt.DESede Maven / Gradle / Ivy

There is a newer version: 1.3.8.RELEASE
Show newest version
package org.kaizen4j.common.encrypt;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

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

import com.google.common.base.Throwables;

/**
 * DESede加密类
 */
public final class DESede {

    // 定义加密算法
    private static final String ALGORITHM = "DESede";

    private DESede() {}

    /**
     * 加密函数
     *
     * @param input 待加密字符串
     * @param keyString 密钥字符串
     * @return 加密后的字符串
     *
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     * @throws NoSuchPaddingException
     * @throws InvalidKeyException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     */
    public static String encrypt(String input, String keyString) {
        try {
            SecretKey deskey = new SecretKeySpec(make3DesKey(keyString), ALGORITHM); // 生成密钥
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, deskey); // 初始化为加密模式

            return Base64.encodeBase64String(cipher.doFinal(input.getBytes(UTF_8)));
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException | NoSuchPaddingException
                | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 解密函数
     *
     * @param input 待解密字符串
     * @param keyString 密钥字符串
     * @return 解密后的字符串
     *
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     * @throws NoSuchPaddingException
     * @throws InvalidKeyException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     */
    public static String decrypt(String input, String keyString) {
        try {
            SecretKey deskey = new SecretKeySpec(make3DesKey(keyString), ALGORITHM);
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, deskey); // 初始化为解密模式

            return new String(cipher.doFinal(Base64.decodeBase64(input)));
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException | NoSuchPaddingException
                | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据字符串生成密钥字节数组
     *
     * @param keyString 密钥字符串
     * @return 生成的密钥字节数组
     *
     * @throws UnsupportedEncodingException
     */
    private static byte[] make3DesKey(String keyString) throws UnsupportedEncodingException {
        byte[] keys = new byte[24];
        byte[] temps = keyString.getBytes(UTF_8);

        if (keys.length > temps.length) {
            System.arraycopy(temps, 0, keys, 0, temps.length);
        } else {
            System.arraycopy(temps, 0, keys, 0, keys.length);
        }
        return keys;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy