android或java用DES加密解密文件

来源:互联网 发布:淘宝淘宝网首页 编辑:程序博客网 时间:2024/05/22 16:41

项目中想将SQLIte中部分数据字段加密,然后查询后解密展示,避免数据被破解,

网上找了很多资料,AES,DES加密,在Android 端不同的平台,加密,解密生成的Key都不是一样的,

无法通用,且和JAVA SE的key也是不一样的,一直没有解决。

  最后用了下文的加解密文件的算法,文件可以java android 加解密可以通用,不过仍没有解决掉String的加解密通用版本问题。

以后有时间再研究~!

http://blog.csdn.net/springsky_/article/details/7548089

UTF-8编码 文件与代码都是

package com.spring.sky.util;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.security.Key;import javax.crypto.Cipher;import javax.crypto.CipherInputStream;import javax.crypto.spec.SecretKeySpec;import org.apache.http.entity.InputStreamEntity;/*** * DES文件加密&解密  <br> * 可以实现android和window的文件互通  * @author spring.sky * Email:vipa1888@163.com * QQ:840950105 * */public class FileDES {/**加密解密的key*/private Key mKey;/**解密的密码*/private Cipher mDecryptCipher;/**加密的密码*/private Cipher mEncryptCipher;public FileDES(String key) throws Exception{initKey(key);initCipher();}/** * 创建一个加密解密的key * @param keyRule  */public void initKey(String keyRule) {byte[] keyByte = keyRule.getBytes();// 创建一个空的八位数组,默认情况下为0byte[] byteTemp = new byte[8];// 将用户指定的规则转换成八位数组for (int i = 0; i < byteTemp.length && i < keyByte.length; i++) {byteTemp[i] = keyByte[i];}mKey = new SecretKeySpec(byteTemp, "DES");}/*** * 初始化加载密码 * @throws Exception */private void initCipher() throws Exception{mEncryptCipher = Cipher.getInstance("DES");mEncryptCipher.init(Cipher.ENCRYPT_MODE, mKey);mDecryptCipher = Cipher.getInstance("DES");mDecryptCipher.init(Cipher.DECRYPT_MODE, mKey);}/** * 加密文件 * @param in * @param savePath 加密后保存的位置 */public void doEncryptFile(InputStream in,String savePath){if(in==null){System.out.println("inputstream is null");return;}try {CipherInputStream cin = new CipherInputStream(in, mEncryptCipher);OutputStream os = new FileOutputStream(savePath);byte[] bytes = new byte[1024];int len = -1;while((len=cin.read(bytes))>0){os.write(bytes, 0, len);os.flush();}os.close();cin.close();in.close();System.out.println("加密成功");} catch (Exception e) {System.out.println("加密失败");e.printStackTrace();}}/** * 加密文件 * @param filePath 需要加密的文件路径 * @param savePath 加密后保存的位置 * @throws FileNotFoundException  */public void doEncryptFile(String filePath,String savePath) throws FileNotFoundException{doEncryptFile(new FileInputStream(filePath), savePath);}/** * 解密文件 * @param in */public void doDecryptFile(InputStream in){if(in==null){System.out.println("inputstream is null");return;}try {CipherInputStream cin = new CipherInputStream(in, mDecryptCipher);BufferedReader reader = new BufferedReader(new InputStreamReader(cin)) ;String line = null;while((line=reader.readLine())!=null){System.out.println(line);}reader.close();cin.close();in.close();System.out.println("解密成功");} catch (Exception e) {System.out.println("解密失败");e.printStackTrace();}}/** * 解密文件 * @param filePath  文件路径 * @throws Exception */public void doDecryptFile(String filePath) throws Exception{doDecryptFile(new FileInputStream(filePath));}public static void main(String[] args)throws Exception {FileDES fileDES = new FileDES("spring.sky");fileDES.doEncryptFile("d:/a.txt", "d:/b");  //加密fileDES.doDecryptFile("d:/b"); //解密}}


原创粉丝点击