码迷,mamicode.com
首页 > 编程语言 > 详细

Java DESede 加解密("DESede/ECB/PKCS5Padding")

时间:2018-02-01 17:48:07      阅读:1485      评论:0      收藏:0      [点我收藏+]

标签:throw   substring   code   tin   ati   bytes   cas   owa   mode   

 

private static final Cipher DES_CIPHER;


static {
    try {
        DES_CIPHER = Cipher.getInstance("DESede/ECB/PKCS5Padding");
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
        throw Throwables.propagate(e);
    }
}

public static String encryptDES(String encryptString, String encryptKey) {
    try {
        DES_CIPHER.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(IcbcBindConstant.ENCODING_GBK), "DESede"));
        byte[] encryptedData = DES_CIPHER.doFinal(encryptString.getBytes(IcbcBindConstant.ENCODING_GBK));
        String hexData = Hex.encodeHexString(encryptedData).toUpperCase();
        return StringUtils.leftPad(String.valueOf(hexData.length()), 6, 0) + hexData;
    } catch (Throwable e) {
        throw Throwables.propagate(e);
    }
}

public static String decryptDES(String decryptString, String decryptKey) {
    try {
        DES_CIPHER.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(IcbcBindConstant.ENCODING_GBK), "DESede"));
        byte[] decryptedData = DES_CIPHER.doFinal(Hex.decodeHex(decryptString.substring(6).toCharArray()));
        return new String(decryptedData, IcbcBindConstant.ENCODING_GBK);
    } catch (Throwable e) {
        throw Throwables.propagate(e);
    }
}

 

Java DESede 加解密("DESede/ECB/PKCS5Padding")

标签:throw   substring   code   tin   ati   bytes   cas   owa   mode   

原文地址:https://www.cnblogs.com/frankyou/p/8398729.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!