数据加密与保存
来源:程序员人生 发布时间:2015-03-20 09:19:48 阅读次数:2302次
//---------------------------------------------------------------------
// 1、加密算法:
//---------------------------------------------------------------------
//加密或解密字符串,change加密、-change解密
public static String Encryption(String str, int change)
{
short sign = 1;
if(change<0){ sign = ⑴; change *= ⑴; }
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, ⑺733171); //解密串
}
//===========================================================================================================
// 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); //创建1个文件输出流,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); //从文件创建1个输入流
}
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(); }
}
}
2、拓展
//---------------------------------------------------------------
// 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⑴ ? "" : ",");
}
rewardInfo += (i == flag.length⑴ ? "" : ";");
}
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);
}
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠