Android文件简单加解密

前言

在Android应用开发中,有时需要把一些内容以文件的方式保存到sdcard上,这时我们需要考虑数据的安全性,这就涉及到文件的加解密,这里简单介绍一种文件的加解密实现方法。

实现方案

我们在读写文件的时候,一般会用到FileInputStream和FileOutputStream,因此我们加解密的时机可以选在read和write数据时,这样很好的和上层业务隔离开,实现方法如下

  1. 实现加密算法
public class DataConvertUtil {
    private static byte rotateLeft(byte sourceByte, int n) {
        int temp = sourceByte & 0xFF;
        return (byte) ((temp << n) | (temp >> (8 - n)));
    }

    private static byte rotateRight(byte sourceByte, int n) {
        int temp = sourceByte & 0xFF;
        return (byte) ((temp >> n) | (temp << (8 - n)));
    }

    private static byte[] rotateLeft(byte[] sourceBytes, int n) {
        for (int i = 0; i < sourceBytes.length; i++) {
            sourceBytes[i] = rotateLeft(sourceBytes[i], n);
        }
        return sourceBytes;

    }

    private static byte[] rotateRight(byte[] sourceBytes, int n) {
        for (int i = 0; i < sourceBytes.length; i++) {
            sourceBytes[i] = rotateRight(sourceBytes[i], n);
        }
        return sourceBytes;
    }

    public static byte[] encryData(byte[] buffer) {
        return rotateLeft(buffer,3);
    }

    public static byte[] decryData(byte[] buffer) {
        return rotateRight(buffer,3);
    }
}
  1. 继承FileOutputStream
public class EncryOutputStream extends FileOutputStream {

    public EncryOutputStream(String path) throws FileNotFoundException {
        super(path);
    }

    @Override
    public void write(byte[] buffer) throws IOException {
        this.write(buffer, 0, buffer.length);
    }

    @Override
    public void write(byte[] buffer, int byteOffset, int byteCount) throws IOException {
        buffer = DataConvertUtil.encryData(buffer);
        super.write(buffer, byteOffset, byteCount);
    }

    @Override
    public void write(int oneByte) throws IOException {
        byte[] buffer = new byte[1];
        buffer[0] = (byte) oneByte;
        this.write(buffer);
    }
}

3.继承FileInputStream

public class EncryFileInputStream extends FileInputStream {

    public EncryFileInputStream(String path) throws FileNotFoundException {
        super(path);
    }

    @Override
    public int read() throws IOException {
        byte[] buffer = new byte[1];
        this.read(buffer);
        return buffer[0];
    }

    @Override
    public int read(byte[] buffer) throws IOException {
        return this.read(buffer, 0, buffer.length);
    }

    @Override
    public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException {
        int length = super.read(buffer, byteOffset, byteCount);
        DataConvertUtil.decryData(buffer);
        return length;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,099评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,803评论 18 399
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,789评论 0 33
  • 一、 1、请用Java写一个冒泡排序方法 【参考答案】 public static void Bubble(int...
    独云阅读 1,442评论 0 6
  • 这本毛姆的书,老师已经推荐了很久,可是一直没有读过,曾经下载了电子版,可终究因为眼睛的问题而放弃。昨天晚上借到,今...
    千钧阅读 995评论 0 2