├── FileEnDecryptManager.java └── README.md /FileEnDecryptManager.java: -------------------------------------------------------------------------------- 1 | import java.io.File; 2 | import java.io.IOException; 3 | import java.io.RandomAccessFile; 4 | import java.nio.MappedByteBuffer; 5 | import java.nio.channels.FileChannel; 6 | 7 | /** 8 | * Created by IntelliJ IDEA. 9 | * Author:Eric 10 | * Date:2015/1/14 11 | * Desc:加密解密管理类 12 | * 加密算法 : 将文件的数据流的每个字节与加密解密key对应字符做异或运算. 13 | * 解密算法 : 已经加密的文件再执行一次对文件的数据流的每个字节与加密解密key对应字符做异或运算. 14 | * this method can decrypt or encrypt a large file in 100 milliseconds,just have a try and see 15 | */ 16 | public class FileEnDecryptManager { 17 | 18 | private String key = "abcdefg"; // 加密解密key(Encrypt or decrypt key) 19 | 20 | private FileEnDecryptManager() { 21 | } 22 | 23 | private static FileEnDecryptManager instance = null; 24 | 25 | public static FileEnDecryptManager getInstance() { 26 | synchronized (FileEnDecryptManager.class) { 27 | if (instance == null) { 28 | instance = new FileEnDecryptManager(); 29 | } 30 | } 31 | return instance; 32 | } 33 | 34 | /** 35 | * 加密入口(encrypt intrance) 36 | * 37 | * @param fileUrl 文件绝对路径 38 | * @return 39 | */ 40 | public boolean doEncrypt(String fileUrl) { 41 | try { 42 | if (isDecrypted(fileUrl)) { 43 | if (encrypt(fileUrl)) { 44 | // 可在此处保存加密状态到数据库或文件(you can save state into db or file) 45 | LogUtils.d("encrypt succeed"); 46 | return true; 47 | } else { 48 | LogUtils.d("encrypt failed"); 49 | return false; 50 | } 51 | } 52 | } catch (IOException e) { 53 | e.printStackTrace(); 54 | } 55 | return false; 56 | } 57 | 58 | private final int REVERSE_LENGTH = 28; // 加解密长度(Encryption length) 59 | 60 | /** 61 | * 加解密(Encrypt or deprypt method) 62 | * 63 | * @param strFile 源文件绝对路径 64 | * @return 65 | */ 66 | private boolean encrypt(String strFile) { 67 | int len = REVERSE_LENGTH; 68 | try { 69 | File f = new File(strFile); 70 | if (f.exists()) { 71 | RandomAccessFile raf = new RandomAccessFile(f, "rw"); 72 | long totalLen = raf.length(); 73 | 74 | if (totalLen < REVERSE_LENGTH) 75 | len = (int) totalLen; 76 | 77 | FileChannel channel = raf.getChannel(); 78 | MappedByteBuffer buffer = channel.map( 79 | FileChannel.MapMode.READ_WRITE, 0, REVERSE_LENGTH); 80 | byte tmp; 81 | for (int i = 0; i < len; ++i) { 82 | byte rawByte = buffer.get(i); 83 | if (i <= key.length() - 1) { 84 | tmp = (byte) (rawByte ^ key.charAt(i)); // 异或运算(XOR operation) 85 | } else { 86 | tmp = (byte) (rawByte ^ i); 87 | } 88 | buffer.put(i, tmp); 89 | } 90 | buffer.force(); 91 | buffer.clear(); 92 | channel.close(); 93 | raf.close(); 94 | return true; 95 | } 96 | } catch (Exception e) { 97 | e.printStackTrace(); 98 | } 99 | return false; 100 | } 101 | 102 | /** 103 | * 解密入口(decrypt intrance) 104 | * 105 | * @param fileUrl 源文件绝对路径 106 | */ 107 | public void doDecrypt(String fileUrl) { 108 | try { 109 | if (!isDecrypted(fileUrl)) { 110 | decrypt(fileUrl); 111 | } 112 | } catch (Exception e) { 113 | e.printStackTrace(); 114 | } 115 | } 116 | 117 | private void decrypt(String fileUrl) { 118 | if (encrypt(fileUrl)) { 119 | // 可在此处保存解密状态到数据库或文件(you can save state into db or file) 120 | LogUtils.d("decrypt succeed"); 121 | } 122 | } 123 | 124 | /** 125 | * fileName 文件是否已经解密了(Whether file has been decrypted) 126 | * 127 | * @param filePath 128 | * @return 129 | * @throws IOException 130 | */ 131 | private boolean isDecrypted(String filePath) throws IOException { 132 | // 从数据库或者文件中取出此路径对应的状态(get state out from db or file) 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android-File-Encrypt 2 | Encrypt and decrypt files in android or java 3 | 4 | #Encrypt 5 | FileEnDecryptManager.getInstance().doEncrypt(filePath); 6 | 7 | #Decrypt 8 | FileEnDecryptManager.getInstance().doDecrypt(filePath); 9 | --------------------------------------------------------------------------------