3DES_CBC加密算法

2023-01-31 04:01:24 des
private static final String AlGorithm = "DESede";    //3DES算法  
      
     private static byte[] ivs = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }; 
     private static IvParameterSpec iv = new IvParameterSpec(ivs);
     
        public static byte[] encryptMode(byte[] src, byte[] key) {
        try {
            System.out.println("没到8bytes:" + Util.byteArrayToHexString(src));
            SecreTKEy deskey = new SecretKeySpec(key, Algorithm); // 生成密钥21
            Cipher c1 = Cipher.getInstance("DESede/CBC/NoPadding"); // 实例化负责加密/解密的Cipher工具类22
            c1.init(Cipher.ENCRYPT_MODE, deskey, iv); // 初始化为加密模式23
            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[] src, byte[] key) {
        try {
            SecretKey deskey = new SecretKeySpec(key, Algorithm);
            Cipher c1 = Cipher.getInstance("DESede/CBC/NoPadding");
            c1.init(Cipher.DECRYPT_MODE, deskey, iv); // 初始化为解密模式44
            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;
    }


相关文章