Android大文件加密传输3

410 阅读2分钟

这是我参与8月更文挑战的第9天,活动详情查看:8月更文挑战

  • 方案五:先对文件进行加密,然后传输,服务端成功接收文件后再对文件进行解密

对文件进行加密解密代码如下:

public class FileDesUtil {
    //秘钥算法
    private static final String KEY_ALGORITHM = "DES";
    //加密算法:algorithm/mode/padding 算法/工作模式/填充模式
    private static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding";
    private static final byte[] KEY = {56, 57, 58, 59, 60, 61, 62, 63};//DES 秘钥长度必须是8 位或以上

    /**
     * 文件进行加密并保存加密后的文件到指定目录
     *
     * @param fromFile 要加密的文件 如c:/test/待加密文件.txt
     * @param toFile   加密后存放的文件 如c:/加密后文件.txt
     */
    public static void encrypt(String fromFilePath, String toFilePath) {
        KLog.i("encrypting...");

        File fromFile = new File(fromFilePath);
        if (!fromFile.exists()) {
            KLog.e("to be encrypt file no exist!");
            return;
        }
        File toFile = getFile(toFilePath);

        SecretKey secretKey = new SecretKeySpec(KEY, KEY_ALGORITHM);
        InputStream is = null;
        OutputStream out = null;
        CipherInputStream cis = null;
        try {
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            is = new FileInputStream(fromFile);
            out = new FileOutputStream(toFile);
            cis = new CipherInputStream(is, cipher);
            byte[] buffer = new byte[1024];
            int r;
            while ((r = cis.read(buffer)) > 0) {
                out.write(buffer, 0, r);
            }
        } catch (Exception e) {
            KLog.e(e.toString());
        } finally {
            try {
                if (cis != null) {
                    cis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        KLog.i("encrypt completed");
    }

    @NonNull
    private static File getFile(String filePath) {
        File fromFile = new File(filePath);
        if (!fromFile.getParentFile().exists()) {
            fromFile.getParentFile().mkdirs();
        }
        return fromFile;
    }

    /**
     * 文件进行解密并保存解密后的文件到指定目录
     *
     * @param fromFilePath 已加密的文件 如c:/加密后文件.txt
     * @param toFilePath   解密后存放的文件 如c:/ test/解密后文件.txt
     */
    public static void decrypt(String fromFilePath, String toFilePath) {
        KLog.i("decrypting...");

        File fromFile = new File(fromFilePath);
        if (!fromFile.exists()) {
            KLog.e("to be decrypt file no exist!");
            return;
        }
        File toFile = getFile(toFilePath);

        SecretKey secretKey = new SecretKeySpec(KEY, KEY_ALGORITHM);

        InputStream is = null;
        OutputStream out = null;
        CipherOutputStream cos = null;
        try {
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            is = new FileInputStream(fromFile);
            out = new FileOutputStream(toFile);
            cos = new CipherOutputStream(out, cipher);
            byte[] buffer = new byte[1024];
            int r;
            while ((r = is.read(buffer)) >= 0) {
                cos.write(buffer, 0, r);
            }
        } catch (Exception e) {
            KLog.e(e.toString());
        } finally {
            try {
                if (cos != null) {
                    cos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        KLog.i("decrypt completed");
    }
}

使用如上这个方案就完美的绕开了上面提到的一些问题,成功的实现了使用Socket对文件进行加密传输。

总结

对于任何技术的使用,底层原理的理解还是很有必要的。不然遇到问题很容易就是一头雾水不知道Why!接下来准备看一下《图解加密技术》和《图解TCP/IP》这两本书,以便加深对密码学和Socket底层原理的理解。