---------------------------------------------------------------------//1, encryption algorithm://---------------------------- -----------------------------------------//Encrypt or decrypt a string, change encryption,-change decrypt 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;//limited to [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, save data to localdb//-------------------- -------------------------------------------------//Save encrypted string to LocalDB if (key = = "Name1") {String data = " Abcdefghijklmnopq ... "; String info = encryption (data, 7733171);//Encrypt string localdb.setstring ("Name1", info),//Save to localdb}//from LocalDB, get saved data if ( LocalDB. $string. ContainsKey ("Name1")) {string info = LocalDB. $string. Get ("Name1");//Get Data Info = encryption (value,- 7733171);//Decryption String}
//===========================================================================================================// 3. Save data to File//=================================================================================================== ========//class Localdb.javaimport 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;/** * Such functions * 1, save data to HashMap Object * 2, save HashMap Object data to file */public class Localdb{private static String fi Lename = "LocalDB.txt";//the file name used to hold the data public static hashmap<string, object> $object;p ublic static hashmap<string , integer> $int;p ublic static hashmap<string, float> $float;p ublic static hashmap<string, string> $string ;p ublic static hashmap<string, double> $double;p ublic static hashmap<string, long> $long;p ublic static Hashmap<string, byte> $byte;p ublic static hashmap<string, short> $short;p ublic staticHashmap<string, boolean> $boolean;//Use HashMap static objects to temporarily save data in the program//private static hashmap<string, type> $ type;//Create HashMap Objects//hashmap use the following://$type. Put (String key, type value)//store type type data to $type, index identifies key store//$type. Get (Object Key)//Get the index identity key from the $type data acquisition//$type. ContainsKey (Object key)//To determine whether the $type contains an index key judgment//$type. Remove (Object key)// Remove the data for index ID key from $type remove/** * Save all object data to file filename-save */public static void Save () {try {//have SD card if ( Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {file file; FileOutputStream F;try {//Get SD card path string pathstring = Environment.getexternalstoragedirectory (). GetPath (); file = new File (pathstring+ "/" + FileName), if (File.exists ()) file.createnewfile (); If the file does not exist, then new F = new FileOutputStream (file, false); Create a file output stream, true to add at the end of the file, False overwrite} 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 ();}} /** * from the file filename, get the saved data into the static object-load */@SuppressWarnings ("unchecked") public static void Load () {try {//Get SD card path 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 (); If the file does not exist, new F = new FileInputStream (file);//create an input stream from the 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& gt; (); $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 ();}}}
Second, expand
---------------------------------------------------------------//1,//boolean array data converted to string, encrypted storage//--------------- ------------------------------------------------//Encrypted flag is a string 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);//encryption string return reward;}
---------------------------------------------------------------------//2, save data to localdb//-------------------- -------------------------------------------------//From LocalDB, get the saved data if (LocalDB. $string. ContainsKey ("Name1")) { String info = LocalDB. $string. Get ("Name1"), Boolean flag[][] = Getbooleans (info, 7733171),//decrypted as Boolean array for (int i=0; i< Flag.length; i++) Treasure_box_form.flag[i] = flag[i];//Copy Existing data}//save encrypted string to LocalDB if (key = = "Name1") {String reward = dataencryption.$ String.set (Treasure_box_form.flag, 7733171);//encryption for string localdb.setstring ("Name1", reward);}
Data encryption and preservation