3DES加解密-KTDes3Tool

/**
 * 数据流加密解密
 *@author LIUWEN276
 */
public class KTDes3Tool {
    private static final String ALGORITHM = "DESede"; //定义 加密算法,可用 DES,DESede,Blowfish        //keybyte为加密密钥,长度为24字节    //src为被加密的数据缓冲区(源)    

    /**
     * 设置数据流为加密模式
     * @param secretKey
     * @param outputStream
     * @return
     */
    public static CipherOutputStream encryptMode(SecretKey secretKey, OutputStream outputStream) {
        try {
            //加密            
            Cipher c1 = Cipher.getInstance(ALGORITHM);
            c1.init(Cipher.ENCRYPT_MODE, secretKey);
            return new CipherOutputStream(outputStream, c1);
        } catch (java.security.NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        } catch (javax.crypto.NoSuchPaddingException e2) {
            e2.printStackTrace();
        } catch (java.lang.Exception e3) {
            e3.printStackTrace();
        }
        return null;
    }

    /**
     * 设置数据流为加密模式
     * @param des3Key
     * @param outputStream
     * @return
     */
    public static CipherOutputStream encryptMode(String des3Key, OutputStream outputStream) {
        //生成密钥
        SecretKey secretKey = new SecretKeySpec(des3Key.getBytes(), ALGORITHM);
        return encryptMode(secretKey, outputStream);

    }

    /**
     * 设置数据流为解密模式
     * @param des3Key
     * @param inputStream
     * @return
     */
    public static CipherInputStream decryptMode(String des3Key, InputStream inputStream) {
        //生成密钥          
        SecretKey deskey = new SecretKeySpec(des3Key.getBytes(), ALGORITHM); //解密         
        return decryptMode(deskey, inputStream);
    }

    /**
     * 设置数据流为解密模式
     * @param secretKey
     * @param inputStream
     * @return
     */
    public static CipherInputStream decryptMode(SecretKey secretKey, InputStream inputStream) {
        try {
            Cipher c1 = Cipher.getInstance(ALGORITHM);
            c1.init(Cipher.DECRYPT_MODE, secretKey);
            return new CipherInputStream(inputStream, c1);
        } catch (InvalidKeyException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值