Android resource protection-Exploration

Source: Internet
Author: User

Address: http://blog.csdn.net/you_and_me12/article/details/7959349

2012-09-09

The APK file uses the decompression tool to view resources such as drawable, but some images in the game cannot be seen.

This issue has been explored for a long time ......

[1] image resources are not placed under the drawable file and placed in assets (but the image resources can also be seen when decompressing the APK). The following describes how to use the image resources.

Analysis: I) when the image resources are placed in drawable, the corresponding ID can be parsed: bitmapfactory. decoderesource (Res, ID)

If it is placed under assets, it needs to be parsed according to the file name (assetmanager is provided for Android ).

Ii) You can create multiple levels of directories to facilitate management.

Iii) such a resolution process takes more time than ID-based resolution (mobile phones become more intelligent, and this time is basically invisible ).

Code:

/*** Read the image from assets * @ Param filename: "a.png" under the assets root directory ", "ABC/a.png" * @ return */public static bitmap getimagefromassets (context, string filename) {bitmap image = NULL; assetmanager AM = context. getresources (). getassets (); try {inputstream is = aM. open (filename); image = bitmapfactory. decodestream (is); is. close ();} catch (ioexception e) {e. printstacktrace ();} return image ;}


[2] package image resources in jar, and then import the project (However, decompress the APK to view the image resources.)

Analysis: I have used a third-party jar package and cannot see it after extracting the APK. Try again. The assets file in the jar package is visible in the APK.

Step 1: Package jar

Project-> right-click-> export-> JAVA/JAR file-> select SRC and assets to be packaged (for example)


Step 2: interpret the image in assets, which is the same as [1]

Step 3: Package the APK and find that the assets in the jar package are merged with the assets of the current project!


[3] The image resources are encrypted and then read under the assets file (Resource protection can be implemented, but it seems time-consuming.)

Analysis: Pre-encryption of images in some way, decryption in the android program, and conversion to bitmap.

This may be the case for other applications. Which of the following are some tips! (The following describes the simple method)

Step 1: encrypt the file. Use the file stream method to read the resource, modify the file, and generate the file. (You can use any format, so you cannot know the image)

I) add a specified byte every several bytes.

Ii) How many bytes are exchanged at intervals (sample code)

Public class KMD1 {public static void encrypt (string filepath) {byte [] tempbytes = new byte [5000]; try {inputstream in = new fileinputstream (filepath ); outputstream out = new fileoutputstream (filepath. subsequence (0, filepath. lastindexof (". ") +" 2.jpg") while (in. read (tempbytes )! =-1) {// a simple exchange byte A = tempbytes [0]; tempbytes [0] = tempbytes [1]; tempbytes [1] = A; out. write (tempbytes); // Write File} catch (ioexception e) {e. printstacktrace () ;}} public static void main (string [] ARGs) {kmd1.encrypt ("D:/a.jpg ");}}

Step 2: decrypt the data and reverse the encryption process.

/*** Read the image from assets * @ Param filename * @ return */public static bitmap getimagefromassets (context, string filename) {bitmap image = NULL; assetmanager AM = context. getresources (). getassets (); try {inputstream is = aM. open (filename); byte [] buffer = new byte [1000000]; // is large enough. read (buffer); For (INT I = 0; I <buffer. length; I + = 5000) {// same as encryption byte temp = buffer [I]; buffer [I] = buffer [I + 1]; buffer [I + 1] = temp;} image = bitmapfactory. decodebytearray (buffer, 0, buffer. length); is. close ();} catch (ioexception e) {e. printstacktrace ();} return image ;}

[3] Use setpixel () and getpixel () to encrypt each pixel, and then restore

Analysis: Get the color value through bitmap. getpixel (x, y), encrypt the RGB value of the color, and then setpixel (X, Y, color)

Step 1: do not bother to write, directly paste the Code:

Note: bitmap must copy one copy, and the second value is true to setpixel. Otherwise, an error will be reported. encrypt and decrypt in the Code are your encryption and decryption processes;

Serious problem: for bitmap setpixel and then in getpixel, the color value is not the set value, and there is a deviation. I don't know why. If this problem can be solved, please leave a message.

Bitmap temp_bitmap = image.copy(Bitmap.Config.ARGB_8888, true);int width = temp_bitmap.getWidth();int height = temp_bitmap.getHeight();int[] pixels = new int[width * height];  //temp_bitmap.getPixels(pixels, 0, width, 0, 0, width, height);for(int i = 0; i < height; i++){for(int j = 0; j < width; j++){int color = temp_bitmap.getPixel(i, j);int r = Color.red(color);int g = Color.green(color);int b = Color.blue(color);int alpha = Color.alpha(color);//if(alpha != 0){r = encrptyRGB(r, 2*(i*j));g = encrptyRGB(g, 4*(i*j));b = encrptyRGB(b, 6*(i*j));color = Color.argb(alpha, r, g, b);pixels[width * i + j] = color;//temp_bitmap.setPixel(i, j, color);}}}temp_bitmap.setPixels(pixels, 0, width, 0, 0, width, height);for(int i = 0; i < height; i++){for(int j = 0; j < width; j++){int color = temp_bitmap.getPixel(i, j);int r = Color.red(color);int g = Color.green(color);int b = Color.blue(color);int alpha = Color.alpha(color);//if(alpha != 0){r = decryptRGB(r, 2*(i*j));g = decryptRGB(g, 4*(i*j));b = decryptRGB(b, 6*(i*j));color = Color.argb(alpha, r, g, b);pixels[width * i + j] = color;//temp_bitmap.setPixel(i, j, color);}}}temp_bitmap.setPixels(pixels, 0, width, 0, 0, width, height);return temp_bitmap;

【?] Continue to explore! Grateful!

Related Article

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.