Java DES 加解密("DES/ECB/PKCS1Padding")

private static final Cipher DES_CIPHER;

static {
    try {
        DES_CIPHER = Cipher.getInstance("DES/ECB/PKCS1Padding");
    } 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(IcbcConstant.ENCODING_GBK), "DES"));
        byte[] encryptedData = DES_CIPHER.doFinal(encryptString.getBytes(IcbcConstant.ENCODING_GBK));
        return new String(encryptedData, IcbcConstant.ENCODING_GBK);
    } 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(IcbcConstant.ENCODING_GBK), "DES"));
        byte decryptedData[] = DES_CIPHER.doFinal(decryptString.getBytes(IcbcConstant.ENCODING_GBK));
        return new String(decryptedData, IcbcConstant.ENCODING_GBK);
    } catch (Throwable e) {
        throw Throwables.propagate(e);
    }
}

  

posted @ 2018-03-12 11:06  FrankYou  阅读(3755)  评论(1编辑  收藏  举报