码迷,mamicode.com
首页 > 其他好文 > 详细

数据加密与保存

时间:2015-03-05 16:57:22      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:数据   加密   保存   java   


//---------------------------------------------------------------------
// 1、加密算法:
//---------------------------------------------------------------------

//加密或解密字符串,change加密、-change解密
public static String Encryption(String str, int change)
{
	short sign = 1;
	if(change<0){ sign = -1; change *= -1; }
	int num = 0;
	int tmp;
	
	byte[] bytes = str.getBytes();
	for(int i = 0; i < bytes.length; i++)
	{
		if(num == 0) num = change;
		
		//限定在[0,127]之间
		tmp = bytes[i] + sign * (num % 3);
		if(tmp > 127) tmp -= 127;
		if(tmp < 0) tmp += 127;
		
		bytes[i] = (byte)tmp;
		num /= 3;
	}
	str = new String(bytes);
	return new String(bytes);
}

//---------------------------------------------------------------------
// 2、将数据保存到 LocalDB
//---------------------------------------------------------------------

//保存加密串到LocalDB中
if(key == "Name1")
{
	String data = "abcdefghijklmnopq...";
	
	String info = Encryption(data, 7733171);	//加密串
	LocalDB.setString("Name1", info);		//保存到LocalDB
}

//从LocalDB中,获取保存的数据
if(LocalDB.$string.containsKey("Name1"))
{
	String info = LocalDB.$string.get("Name1");	//获取数据
	info = Encryption(value, -7733171);		//解密串
}

//===========================================================================================================
// 3、保存数据到文件
//===========================================================================================================

//类LocalDB.java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import android.os.Environment;

/**
 * 此类功能
 * 1、保存数据到HashMap对象中
 * 2、保存HashMap对象数据到文件
 */
public class LocalDB
{
	private static String fileName = "LocalDB.txt";	//用于保存数据的文件名
	
	public static HashMap<String, Object> $object;
	public static HashMap<String, Integer> $int;
	public static HashMap<String, Float> $float;
	public static HashMap<String, String> $string;
	public static HashMap<String, Double> $double;
	public static HashMap<String, Long> $long;
	public static HashMap<String, Byte> $byte;
	public static HashMap<String, Short> $short;
	public static HashMap<String, Boolean> $boolean;

//	使用HashMap的静态对象,在程序中临时保存数据
//	private static HashMap<String, Type> $type;	//创建HashMap对象
	
//	HashMap用法如下:
//	$type.put(String key, Type value)			//存储Type类型数据到$type,索引标识key	存储
//	$type.get(Object key)						//从$type中获取索引标识key的数据		获取
//	$type.containsKey(Object key)				//判断$type中是否含有索引标识key		判断
//	$type.remove(Object key)					//将索引标识key对应的数据从$type中移除	移除

	/**
	 * 保存所有对象的数据,到文件fileName中  -保存
	 */
	public static void save() 
	{
		try 
		{	//有SD卡
			if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
			{  
				File file;
				FileOutputStream f;
				
				try 
				{  	//获得SD卡路径
					String pathString = Environment.getExternalStorageDirectory().getPath();
					file = new File(pathString+"/"+ fileName);
							
					if(file.exists())file.createNewFile(); //若文件不存在,则新建
					f = new FileOutputStream(file, false); //创建一个文件输出流,true表示在文件末尾添加, false覆盖
				} 
				catch (Exception e) { return; }
					 
				ObjectOutputStream o = new ObjectOutputStream(f);
				o.writeObject($object);
				o.writeObject($int);
				o.writeObject($byte);
				o.writeObject($float);
				o.writeObject($double);
				o.writeObject($string);
				o.writeObject($long);
				o.writeObject($boolean);
				o.writeObject($short);
				o.flush();
				f.flush();
				o.close();
				f.close();
			}
		} 
		catch (Exception e) { e.printStackTrace(); }
	}

	/**
	 * 从文件fileName中,获取保存的数据到静态对象中  -载入
	 */
	@SuppressWarnings("unchecked")
	public static void Load() 
	{
		try 
		{
			//获得SD卡路径
			if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
			{  
				File file =null;
				FileInputStream f=null;
				
				try 
				{  
					String pathString = Environment.getExternalStorageDirectory().getPath();
					file = new File(pathString+"/"+ fileName);
					
					if(file.exists()) file.createNewFile(); //若文件不存在,则新建
					f = new FileInputStream(file);			//从文件创建一个输入流
				} 
				catch (Exception e) { return; }
			
				try
				{
					ObjectInputStream o = new ObjectInputStream(f);
					$object = (HashMap<String, Object>) o.readObject();
					$int = (HashMap<String, Integer>) o.readObject();
					$byte = (HashMap<String, Byte>) o.readObject();
					$float = (HashMap<String, Float>) o.readObject();
					$double = (HashMap<String, Double>) o.readObject();
					$string = (HashMap<String, String>) o.readObject();
					$long = (HashMap<String, Long>) o.readObject();
					$boolean = (HashMap<String, Boolean>) o.readObject();
					$short = (HashMap<String, Short>) o.readObject();
					
					o.close();
					f.close();
				} 
				catch (Exception e) 
				{
					$object = new HashMap<String, Object>();
					$float = new HashMap<String, Float>();
					$int = new HashMap<String, Integer>();
					$string = new HashMap<String, String>();
					$double = new HashMap<String, Double>();
					$long = new HashMap<String, Long>();
					$byte = new HashMap<String, Byte>();
					$short = new HashMap<String, Short>();
					$boolean = new HashMap<String, Boolean>();
					
					f.close();
				}
			}
		}
		catch (Exception e) { e.printStackTrace(); }
	}
}


二、拓展

//---------------------------------------------------------------
// 1、
// boolean数组数据转换为字符串,加密存储
//---------------------------------------------------------------
//加密flag为字符串
public static String setBooleans(boolean flag[][], int change)
{
	String rewardInfo = ""; 
	for(int i = 0; i < flag.length; i++)
	{
		for(int j = 0; j < flag[i].length; j++)
		{
			rewardInfo += (flag[i][j] ? "1" : "0") + (j == flag[i].length-1 ? "" : ",");
		}
		rewardInfo += (i == flag.length-1 ? "" : ";");
	}
	
	String reward = DataEncryption.Encryption(rewardInfo, change);	//加密串
	return reward;
}

//---------------------------------------------------------------------
// 2、将数据保存到 LocalDB
//---------------------------------------------------------------------

//从LocalDB中,获取保存的数据
if(LocalDB.$string.containsKey("Name1"))
{
	String info = LocalDB.$string.get("Name1");
	boolean flag[][] = getBooleans(info, 7733171);			//解密为布尔数组
	for(int i=0; i< flag.length; i++) Treasure_box_form.flag[i] = flag[i];	//复制已有数据
	
}

//保存加密串到LocalDB中
if(key == "Name1")
{
	String reward = DataEncryption.$string.set(Treasure_box_form.flag, 7733171);	//加密为字符串
	LocalDB.setString("Name1", reward);
}





数据加密与保存

标签:数据   加密   保存   java   

原文地址:http://blog.csdn.net/scimence/article/details/44084569

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!