I was planning to write this article when I came back from work. On the one hand, it is a note record for today's learning, and on the other hand, it is a basic knowledge of SWF file encryption for some friends. The reason is that it is the foundation that I just learned. However, after watching the video for a while in the evening, I am a legend of the video, saying that Guo Degang is always playing Xie Nan. Is there a base? Don't explain ......
Before explaining the encryption and decryption methods, I would like to explain some theoretical aspects.
SWF Encryption. In fact, whether it is Flash SWF file encryption or other types of encryption, the purpose is very simple, is private, do not want to disclose the details of the specific file. Of course, this is also the trend of the big environment. Many of them come to China and understand it. SWF file encryption has two main purposes:
1. flash code encryption, such as commercial game code, is always difficult to use for theft.
2. Flash material encryption to prevent theft by some tools. Friends who use flash for animation should have a deep understanding.
File Format. What is the file format? Baidu Baike introduces this: the file format (or file type) refers to the special encoding method used by the computer to store information, is used to identify data stored internally. Simply put, all files are binary data, and files in a specific format (such as video files and video files) are classified according to a specific rule. Only suffix such as "{.swf" will be added later for identification.
In this way, the theoretical method of encryption is born for the above SWF encryption purpose.
SWF Encryption Method.
If yesEncryption CodeThen, some tools are used for code obfuscation to reduce readability. Even if the code is cracked and cannot be understood, the code is cracked in white. Some tools on the Internet are suitable, such as doswf.
If yesEncrypted materialThe file must be damaged so that the cracking tool cannot parse the data, so that the file can be protected. Destroying a file destroys binary data and the file format mentioned above. Of course, it cannot be damaged at will, because once the file is damaged, not only can someone else crack your file, but your own file cannot run normally, and the horse is put upside down. Therefore, regular damages are required.Encryption Algorithm.
Now, my files are damaged by encryption, but they cannot be used. To achieve normal use, we need another program, that isDecryption algorithm. The decryption algorithm is exactly the opposite of the encryption algorithm. It destroys a file and fixes a file to achieve normal use.
(Note: I wrote the previous paragraphs on Saturday. I accidentally smashed my feet by a stone. Although there was no blur, it was still quite painful. I would like to remind you that, I had a rock in my arms. It was time to let it go early and put it late, so I fell asleep ...... Continue to write)
So, the encryption method that I can think of now is that the sub-file must be obfuscated with the code and encrypted file at the same time, while the main file must contain the decryption code and the code at the same time, prevent Cracking.
Let's use a small example to demonstrate it. In this case, I will first use flash to make a small animation. There is a picture in it, and the generated file is named unfinished-1.swf ". Here, this small animation is a sub-file, and now I want to encrypt it.
Create a new classEncrypttool.The role of this animation isEncryption. It should be noted that, to encrypt a file, local file read/write operations will be involved. General flash player is not supported and requires an air project to run.
Extract some code to explain the encryption process.
- _ Urlloader = new urlloader ();
- _ Urlloader. dataformat = urlloaderdataformat. Binary;
Copy code
(I don't know what's going on. The Code will always be removed automatically when it is put on, so I will write it out.) _ urlloader. addeventlistener (event. Complete, loaded );
_ Urlloader. Load (New URLRequest ("Unnamed -1.swf "));
The above code indicates loading and reading the animation in binary data mode. After the animation is loaded, the following method is triggered.
- Private function loaded (E: Event): void
- {
- _ Urlloader. removeeventlistener (event. Complete, loaded );
- VaR data: bytearray = _ urlloader. Data as bytearray; // use a binary array to keep the read data.
- Var key: String = "this is a test"; // set the password key to this string.
- VaR keybytes: bytearray = new bytearray ();
- Keybytes. writeobject (key); // converts the string to a binary array
- // The following is the encryption algorithm.
- VaR P: Int = data. Length/2; // obtain the location index in the center of the original data
- VaR B1: bytearray = new bytearray ();
- For (var I: Int = 0; I <keybytes. length; I ++ ){
- B1.writebyte (data [I + P] ^ keybytes [I])
- }
- // The above cycle mainly refers to the calculation of the password data and the original data, that is, the data of the same number of bytes as the password data is obtained from the intermediate position of the original data, they perform an exclusive or operation and store the operation results in a binary array.
- // I don't know what an exclusive or operation is. Baidu, I just checked it ......
- Data. Position = P; // adjust the original data position to the intermediate position.
- Data. writebytes (B1); // replace all the data after calculation in the middle
- // The encryption process is in the middle, which is a little troublesome. It is also simpler, such as tuning the first byte.
- // The data above is encrypted. Now you need to save the encrypted data as a file. For convenience, it is still saved as a SWF file.
- Var file: file = new file ("C:/Documents and Settings/user/desktop/encryption/Decryption/decrypttool/src/mc.swf ");
- VaR FS: filestream = new filestream ();
- FS. Open (file, filemode. Write );
- FS. writebytes (data );
- FS. Close ();
Copy code
After all, you have saved an encrypted file and copied mc.swf in the folder. Although it looks like a normal SWF animation file, if you open it directly, you will find that there is nothing left blank. At this time, try again to use the flash client Genie and other cracking software to crack this animation, and the prompt "file damaged" will pop up. Yes, the file is damaged, encrypted, and cannot be cracked.
The file is encrypted, but it cannot run normally. At this time, you need to find the place to use it.Decryption. Create a classDecrypttool., This does not require the air project. It is normal. Now, if this animation is required, decryption starts.
Similarly, you must first load the encrypted file as binary data.
- _ Urlloader = new urlloader ();
- _ Urlloader. dataformat = urlloaderdataformat. Binary;
- _ Urlloader. addeventlistener (event. Complete, bytesloaded );
- _ Urlloader. Load (New URLRequest ("mc.swf "));
Copy code
After loading, trigger the following method.
- Private function bytesloaded (E: Event): void
- {
- _ Urlloader. removeeventlistener (event. Complete, bytesloaded );
- VaR data: bytearray = _ urlloader. Data as bytearray; // Similarly, save the read data, which is encrypted
- // The following decryption operation is the same as the encryption operation because of the exclusive or operation. The reverse operation is no different from the previous one.
- Var key: String = "this is a test ";
- VaR keybytes: bytearray = new bytearray ();
- Keybytes. writeobject (key );
- VaR P: Int = data. Length/2;
- VaR B1: bytearray = new bytearray ();
- For (var I: Int = 0; I <keybytes. length; I ++ ){
- B1.writebyte (data [I + P] ^ keybytes)
- }
- Data. Position = P;
- Data. writebytes (B1 );
- // The data obtained above is the decrypted data. This is the complete data, which is exactly the same as the previously read "untitled -1.swf" data, because this step is used to restore the data. At this time, if you save this data as a SWF file, you will find that this file can be used normally, and the bitmap is displayed normally, which is no different from the previous source file.
- // After the complete data is obtained, use loader to read the binary data. Note the following bold lines of code. This is very important. Otherwise, an error is reported, indicating that SWF is not supported.
Copy code
(I don't know what's going on. The Code will always be automatically removed from the back. I will write it out directly)
- _ Loader = new loader ();
- _ Loader. contentloaderinfo. addeventlistener (event. Complete, loaded );
- VaR LC: loadercontext = new loadercontext ();
- LC. allowcodeimport = true;
- _ Loader. loadbytes (data, LC );
Copy code
(The code turned into a Q expression just now. It's too speechless. Let's take a look)
After reading the data above, proceed to the following method.
- Private function loaded (E: Event): void
- {
- _ Loader. contentloaderinfo. removeeventlistener (event. Complete, loaded );
- VaR MC: movieclip = _ loader. content as movieclip;
- Addchild (MC );
- }
Copy code
The above program gets a video clip from the binary array through loader. At this time, the animation in the program can be played normally, indicating that the decryption is successful.
This is the basic idea of encryption and decryption. Of course, do not forget to confuse the decryption code. Otherwise, it will be useless. If you don't understand the code, you only need a hacker to crack the sub-file.
Code obfuscation. We haven't conducted any experiments yet. If you don't do any demonstration, download the tool.
Encryption is not omnipotent, and it will certainly be able to be implemented. However, with this foundation, it is still possible to defend against cainiao and individual cracking tools. The path to encryption is endless.
By the way, the Sina Blog does not have the function of inserting code. As a result, it takes a lot of time to manually print the code.
Code formatting is really uncomfortable. This is also a problem.
Reproduced from: http://bbs.9ria.com/forum.php? MoD = viewthread & tid = 172529 & page = 1 & extra = # pid1654416
[Switch] SWF file encryption Basics