Java JDK 实现 DES1 加解密

一, 各种模式的DES-1加密.(直接上代码)

    public final static String DES = "DES";
    public interface DesModeType{
        public final static String DES_MODE_CFB8 = "DES/CFB8/NoPadding"; // 需要 iv
        public final static String DES_MODE_CFB64 = "DES/CFB64/NoPadding"; // 需要 iv
        public final static String DES_MODE_OFB8 = "DES/OFB8/NoPadding"; // 需要 iv
        public final static String DES_MODE_OFB64 = "DES/OFB64/NoPadding"; // 需要 iv
        public final static String DES_MODE_CBC = "DES/CBC/PKCS5Padding"; // 需要 iv
        public final static String DES_MODE_ECB = "DES/ECB/PKCS5Padding";
    }


    public void testDES(){
        Log.d(TAG, "testDES: 测试DES");
        String key = "123456789";
        String content = "JustTestDataForDES";
        try {
            byte[] iv = { 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31};
            String mode = DES_MODE_CFB8;
            Log.d(TAG, "解密数据CFB8 : " + new String(decryptAesBase(encryptAesBase(content.getBytes("utf-8"),key.getBytes("utf-8"),mode,iv),key.getBytes("utf-8"),mode,iv)));
            mode = DES_MODE_CFB64;
            Log.d(TAG, "解密数据CFB64 : " + new String(decryptAesBase(encryptAesBase(content.getBytes("utf-8"),key.getBytes("utf-8"),mode,iv),key.getBytes("utf-8"),mode,iv)));
            mode = DES_MODE_OFB8;
            Log.d(TAG, "解密数据OFB8 : " + new String(decryptAesBase(encryptAesBase(content.getBytes("utf-8"),key.getBytes("utf-8"),mode,iv),key.getBytes("utf-8"),mode,iv)));
            mode = DES_MODE_OFB64;
            Log.d(TAG, "解密数据OFB64 : " + new String(decryptAesBase(encryptAesBase(content.getBytes("utf-8"),key.getBytes("utf-8"),mode,iv),key.getBytes("utf-8"),mode,iv)));
            mode = DES_MODE_CBC;
            Log.d(TAG, "解密数据CBC : " + new String(decryptAesBase(encryptAesBase(content.getBytes("utf-8"),key.getBytes("utf-8"),mode,iv),key.getBytes("utf-8"),mode,iv)));
            mode = DES_MODE_ECB;
            Log.d(TAG, "解密数据ECB : " + new String(decryptAesBase(encryptAesBase(content.getBytes("utf-8"),key.getBytes("utf-8"),mode,iv),key.getBytes("utf-8"),mode,iv)));
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "testDES: 发生异常" );
        }


    }
    /**
     * AES 加密.
     * @param data 待加密数据.
     * @param key  加密键byte数组
     * @param mode 加密模式.
     * @param iv  向量 8 位.
     * @return
     */
    private static byte[] encryptAesBase(byte[] data, byte[] key , String mode , byte[] iv){
        try {
            // 1. 创建秘钥对象.
            DESKeySpec keySpec = new DESKeySpec(key);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
            SecretKey securekey = keyFactory.generateSecret(keySpec);
            // 创建加密对象.
            Cipher cipher = Cipher.getInstance(mode);
            // 初始化加密器.
            if (mode.equals(DES_MODE_ECB)){
                cipher.init(Cipher.ENCRYPT_MODE, securekey);
            }else{
                cipher.init(Cipher.ENCRYPT_MODE, securekey,new IvParameterSpec(iv));
            }
            return cipher.doFinal(data);
        }catch (Exception e){
            return null;
        }
    }


    /**
     * AES 解密
     * @param data 待解密数据.
     * @param key  加密键byte数组
     * @param mode 解密模式.
     * @param iv   向量.
     * @return
     */
    private static byte[] decryptAesBase(byte[] data, byte[] key, String mode, byte[] iv){
        try{
            // 1. 创建秘钥.
            DESKeySpec keySpec = new DESKeySpec(key);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
            SecretKey securekey = keyFactory.generateSecret(keySpec);
            // 2. 创建加密器.
            Cipher cipher = Cipher.getInstance(mode);
            // 3. 初始化加密器.
            if (mode.equals(DES_MODE_ECB)){
                cipher.init(Cipher.DECRYPT_MODE, securekey);
            }else{
                cipher.init(Cipher.DECRYPT_MODE, securekey,new IvParameterSpec(iv));
            }
            return cipher.doFinal(data);
        }catch (Exception e){
            return null;
        }
    }

你可能感兴趣的:(Java JDK 实现 DES1 加解密)