Java使用Cipher类实现加解密

加密

	public static byte[] encrypt(byte[] msgBuf, byte[] keyBuf, byte[] iv) {
        Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
		DESedeKeySpec keySpec = new DESedeKeySpec(keyBuf);
        Key tripleDesKey = getKeyFactory().generateSecret(keySpec);
        cipher.init(Cipher.ENCRYPT_MODE, tripleDesKey, new IvParameterSpec(iv));
        byte[] encrypted = cipher.doFinal(msgBuf);
        return encrypted;
    }

解密

    public static byte[] decrypt(byte[] encryptedMsgBuf, byte[] keyBuf, byte[] iv) {
        Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
		DESedeKeySpec keySpec = new DESedeKeySpec(keyBuf);
        Key tripleDesKey = getKeyFactory().generateSecret(keySpec);
        cipher.init(Cipher.DECRYPT_MODE, tripleDesKey, new IvParameterSpec(iv));
        byte[] decrypted = cipher.doFinal(encryptedMsgBuf);
        return decrypted;
    }

你可能感兴趣的:(D5-加解密,C1-Java)