Android大文件加密传输

750 阅读2分钟

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

前言

数据加密,是一门历史悠久的技术,指通过加密算法和加密密钥将明文转变为密文,而解密则是通过解密算法和解密密钥将密文恢复为明文。它的核心是密码学。
数据加密目前仍是计算机系统对信息进行保护的一种最可靠的办法。它利用密码技术对信息进行加密,实现信息隐蔽从而起到保护信息的安全的作用。

项目中使用Socket进行文件传输过程时,需要先进行加密。实现的过程中踏了一些坑,下面对实现过程进行一下总结。

DES加密

由于加密过程中使用的是DES加密算法,下面贴一下DES加密代码:

    //秘钥算法
    private static final String KEY_ALGORITHM = "DES";
    //加密算法:algorithm/mode/padding 算法/工作模式/填充模式
    private static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding";
    //秘钥
    private static final String KEY = "12345678";//DES秘钥长度必须是8位

    public static void main(String args[]) {
        String data = "加密解密";
        KLog.d("加密数据:" + data);
        byte[] encryptData = encrypt(data.getBytes());
        KLog.d("加密后的数据:" + new String(encryptData));
        byte[] decryptData = decrypt(encryptData);
        KLog.d("解密后的数据:" + new String(decryptData));
    }

    public static byte[] encrypt(byte[] data) {
        //初始化秘钥
        SecretKey secretKey = new SecretKeySpec(KEY.getBytes(), KEY_ALGORITHM);

        try {
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] result = cipher.doFinal(data);
            return Base64.getEncoder().encode(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] decrypt(byte[] data) {
        byte[] resultBase64 = Base64.getDecoder().decode(data);
        SecretKey secretKey = new SecretKeySpec(KEY.getBytes(), KEY_ALGORITHM);

        try {
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            byte[] result = cipher.doFinal(resultBase64);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

输出:

加密数据:加密解密
加密后的数据:rt6XE06pElmLZMaVxrbfCQ==
解密后的数据:加密解密

Socket客户端部分代码:

    Socket socket = new Socket(ApiConstants.HOST, ApiConstants.PORT);
    OutputStream outStream = socket.getOutputStream();

    InputStream inStream = socket.getInputStream();
    RandomAccessFile fileOutStream = new RandomAccessFile(file, "r");
    fileOutStream.seek(0);
    byte[] buffer = new byte[1024];
    int len = -1;
    while (((len = fileOutStream.read(buffer)) != -1)) {
        outStream.write(buffer, 0, len);   // 这里进行字节流的传输
    }

    fileOutStream.close();
    outStream.close();
    inStream.close();
    socket.close();

Socket服务端部分代码:

    Socket socket = server.accept();
    InputStream inStream = socket.getInputStream();
    OutputStream outStream = socket.getOutputStream();
    outStream.write(response.getBytes("UTF-8"));
    RandomAccessFile fileOutStream = new RandomAccessFile(file, "rwd");
    fileOutStream.seek(0);
    byte[] buffer = new byte[1024];
    int len;
    while ((len = inStream.read(buffer)) != -1) { // 从字节输入流中读取数据写入到文件中
        fileOutStream.write(buffer, 0, len);
    }

    fileOutStream.close();
    inStream.close();
    outStream.close();
    socket.close();