Android常用工具之加密方式

公司中很多文件需要加密,但是每个公司的加密方式也不相同,基础的代码是相同的。

首先定义加密方式:

private final static byte[] rawkey = new byte[]{ };
private final static byte[] iv = new byte[]{};

加密解密过程
//加密 
private static byte[] encrypt(byte[] clear) throws GeneralSecurityException {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(rawkey, "AES"), new IvParameterSpec(iv));
        return cipher.doFinal(clear);
    }
//解密
    private static byte[] decrypt(byte[] encrypted) throws GeneralSecurityException {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(rawkey, "AES"), new IvParameterSpec(iv));
        return cipher.doFinal(encrypted);
    }
//解密
    public static void decrypt(String fileInputPath, String fileOutputPath) throws GeneralSecurityException {
        FileInputStream fis = null;
        FileOutputStream fw = null;
        try {
            fis = new FileInputStream(fileInputPath);
            fw = new FileOutputStream(fileOutputPath);

            byte[] bytes = new byte[fis.available()];
            fis.read(bytes);

            fw.write(decrypt(bytes));
            fw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) fis.close();
                if (fw != null) fw.close();
            } catch (IOException ignored) {
            }
        }
    }
//加密
    public static void encrypt(String fileInputPath, String fileOutputPath) throws GeneralSecurityException {
        FileInputStream fr = null;
        FileOutputStream fos = null;
        try {
            File f = new File(fileInputPath);
            fr = new FileInputStream(f);
            byte[] bytes = new byte[(int) f.length()];
            fr.read(bytes, 0, bytes.length);
            fos = new FileOutputStream(fileOutputPath);
            fos.write(encrypt(bytes));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fr != null) fr.close();
                if (fos != null) fos.close();
            } catch (IOException ignored) {
            }
        }
    }
//解密
    public static String decrypt(String fileInputPath) throws GeneralSecurityException {
        FileInputStream fis = null;
        String ret = null;
        try {
            fis = new FileInputStream(fileInputPath);

            byte[] bytes = new byte[fis.available()];
            fis.read(bytes);
            ret = new String(decrypt(bytes));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) try {
                fis.close();
            } catch (IOException ignored) {
            }
        }
        return ret;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值