Unity3d Game Resources Package encryption (picture/xml/txt, etc.) C # encoding (i)

Source: Internet
Author: User

This article is just about the process, using a very simple packaging encryption method, as to what kind of encryption results, please follow the requirements to modify, byte offset, inversion plus algorithm can be, but generally need not be so complex, and too complex encryption for the extremely pursuit of running efficiency of the game, is also a heavy burden.


For unity, although unity itself will be compressed encryption, but its decryption algorithm is ubiquitous on the internet, if you feel that the information in the game is confidential, please encrypt it yourself.


The principle of package encryption:

1, we all know that the files are composed of bytes.

2. A picture looks beautiful because its data is arranged in a certain order.

Beautiful sword-ling sister.


We can open it with a text editor.

is garbled, otherwise you want to see what?


3. What happens if we scramble the picture data or add some messy data in front of it?


Well, the picture doesn't show up.

It's easy to understand, like when you're watching a piece of mosaic.

Think carefully, why do you play mosaic?

Not just for secrecy.


OK, we've got a secret-encryption feature on it.


4, I found a picture ... Meng Meng Da's.



Also open the image with a text editor.

Copy all the contents to the first picture file behind.

What would be the result?

Two pictures will be spelled together?



It's unfortunate ...

But this is in line with my subject. Encryption, is to let people do not see.


So here's the principle, and here's the code.

The code is not for a single file, but for packaging and encrypting multiple folders.

The encryption method is just a package of multiple files.


-----------------------------------------------------------------------------------

I'm a cute split line.

-----------------------------------------------------------------------------------


Knowledge points to use:

1. Read and Write files

2, the operation of folders, files, get all the files


-----------------------------------------------------------------------------------

I'm a cute split line.

-----------------------------------------------------------------------------------

Here's the main code:


Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace mypackres{    class Helper    {public        static void Log (String str)        {            console.write (str+ "\ n \ nthe ");        }        public static void Pause ()        {            Console.Write ("Press any key to continue ... ");            Console.readkey (True);}}}    

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks;        Using system.io;using System.diagnostics;namespace mypackres{class Program {private static int m_id=0;         private static int m_totalsize = 0;//private static list<int> m_idlist = new list<int> ();// private static list<int> m_startposlist = new list<int> ();//private static list<int> M_sizel        ist = new list<int> ();//private static list<string> m_pathlist = new list<string> ();        private static dictionary<int,onefileinfor> M_allfileinfodic = new dictionary<int,onefileinfor> ();        private static string m_currentdir = "";            static void Main (string[] args) {m_currentdir = environment.currentdirectory;            Helper.Log ("Mypackres Start" + m_currentdir);          list<string> foldertopacklist = new list<string> ();  /** Read configuration file **/Helper.Log ("folder required for Packaging:");            StreamReader StreamReader = new StreamReader ("Mypackres.ini", Encoding.default);            String line;                while ((Line=streamreader.readline ())!=null) {Helper.Log (line);                if (line) {foldertopacklist.add-!foldertopacklist.contains (line);            }} streamreader.close (); /** Traversal Package folder **/for (int index = 0; index < foldertopacklist.count;index++) {Pa            Ckfolder (Foldertopacklist[index]); } Helper.Log ("Pack up!            ");        Helper.pause (); /** Traverse folder for all file information **/private static void Traversefolder (String foldername) {Helper.Log            ("Traverse folder" + foldername);            /** read the information for all files under the folder **/DirectoryInfo dirinfo = new DirectoryInfo (foldername); foreach (FileInfo FileInfo in DIRINFO.GETFIles ("*. *", Searchoption.alldirectories)) {string filename = (FileInfo. Fullname.replace (m_currentdir+ "\ \", "")).                Replace ("\ \", "/"); int filesize = (int) FileInfo.                Length;                Helper.Log (m_id + ":" + filename + "File size:" + filesize);                Onefileinfor info = new Onefileinfor ();                info.m_id = m_id;                Info.m_size = FileSize;                                Info.m_path = filename; /** reads this file **/FileStream filestreamread = new FileStream (FileInfo.                FullName, FileMode.Open, FileAccess.Read); if (Filestreamread = = null) {Helper.Log ("Read file failed:" +fileinfo.                    FullName);                    Helper.pause ();                Return                    } else {byte[] filedata = new Byte[filesize];                    Filestreamread.read (filedata, 0, filesize); Info.m_data = fiLedata;                } filestreamread.close ();                M_allfileinfodic.add (M_id,info);                m_id++;            M_totalsize + = FileSize; }}/** Package a folder **/private static void Packfolder (String foldername) {Traversef            older (foldername);            Helper.Log ("Number of files:" + m_id);            Helper.Log ("Total File Size:" + m_totalsize);            /** update file start point in upk **/int firstfilestartpos = 4 + (4 + 4 + 4 + +) * M_ALLFILEINFODIC.COUNT;            int startpos = 0;                for (int index = 0; index < m_allfileinfodic.count; index++) {if (index = = 0)                {startpos = Firstfilestartpos; } else {startpos = M_allfileinfodic[index-1].m_startpos + m_allfileinf                odic[index-1].m_size;//the start of the previous file + file size; } M_allfileinfodic[index].m_startpos = StartPos }/** Write file **/FileStream FileStream = new FileStream (foldername + ".            UPK ", FileMode.Create);            /** total number of files **/byte[] Totaliddata=system.bitconverter.getbytes (m_id); FileStream.Write (totaliddata, 0, Totaliddata.            Length); for (int index = 0; index < m_allfileinfodic.count;index++) {/** Write ID **/by                te[] Iddata = System.BitConverter.GetBytes (m_allfileinfodic[index].m_id); FileStream.Write (iddata, 0, Iddata.                Length); /** write Startpos **/byte[] startposdata = System.BitConverter.GetBytes (M_allfileinfodic[index].m_startpos)                ; FileStream.Write (startposdata, 0, Startposdata.                Length);                /** write Size **/byte[] sizedata = System.BitConverter.GetBytes (m_allfileinfodic[index].m_size); FileStream.Write (sizedata, 0, Sizedata.                Length);             /** Write to Path **/   byte[] Pathdata = new byte[256]; byte[] Mypathdata = new UTF8Encoding ().                GetBytes (M_allfileinfodic[index].m_path); for (int i = 0; i < Mypathdata. Length;                i++) {Pathdata[i] = Mypathdata[i]; } pathdata[mypathdata.                Length] = 0; FileStream.Write (pathdata, 0, Pathdata.                            Length);                /** Write file Data **/for (int index = 0; index < m_allfileinfodic.count; index++) {            FileStream.Write (m_allfileinfodic[index].m_data, 0, m_allfileinfodic[index].m_size);            } filestream.flush ();            Filestream.close ();            /** Reset data **/m_id = 0;            m_totalsize = 0;        M_allfileinfodic.clear ();        }} public class Onefileinfor {public int m_id=0;        public int m_startpos=0;        public int m_size=0;        public string M_path= ""; Public byte[] m_data = null; };}

-----------------------------------------------------------------------------------

I'm a cute split line.

-----------------------------------------------------------------------------------

Code easy to understand (#▽#)


Create a new configuration file first

Mypackres.ini


Add the folders that need to be packaged in each line:


Look at the results of our operation.

First look at the diagram I prepared. From the path of the blue of the TP extraction OH.


After the package is finished, a UPK file is generated.


UPK, this suffix is the default resource file format for Unreal engine.


Finally attach the project:

http://download.csdn.net/detail/cp790621656/8393457


Unity3d Game Resources Package encryption (picture/xml/txt, etc.) C # encoding (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.