threedesutil.java

来自「cwbbs 云网论坛源码」· Java 代码 · 共 75 行

JAVA
75
字号
package cn.js.fan.security;import java.security.*;import javax.crypto.*;import javax.crypto.spec.SecretKeySpec;public class ThreeDesUtil {     private static final String Algorithm = "DESede";      public ThreeDesUtil() {                Security.addProvider(new com.sun.crypto.provider.SunJCE());    }    public static String encrypt2hex(String key, String content) {        byte[] r = encryptMode(key.getBytes(), content.getBytes());        return SecurityUtil.byte2hex(r);    }    public static String decrypthexstr(String key, String hexStr) {        byte[] r = decryptMode(key.getBytes(), SecurityUtil.hexstr2byte(hexStr));        if (r!=null)            return new String(r);        else            return "";    }            public static byte[] encryptMode(byte[] keybyte, byte[] src) {        try {                        SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);                        Cipher c1 = Cipher.getInstance(Algorithm);            c1.init(Cipher.ENCRYPT_MODE, deskey);            return c1.doFinal(src);        } catch (java.security.NoSuchAlgorithmException e1) {            e1.printStackTrace();        } catch (javax.crypto.NoSuchPaddingException e2) {            e2.printStackTrace();        } catch (java.lang.Exception e3) {            e3.printStackTrace();        }        return null;    }            public static byte[] decryptMode(byte[] keybyte, byte[] src) {        try {                        SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);                        Cipher c1 = Cipher.getInstance(Algorithm);            c1.init(Cipher.DECRYPT_MODE, deskey);            return c1.doFinal(src);        } catch (java.security.NoSuchAlgorithmException e1) {            e1.printStackTrace();        } catch (javax.crypto.NoSuchPaddingException e2) {            e2.printStackTrace();        } catch (java.lang.Exception e3) {            e3.printStackTrace();        }        return null;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?