Android simple encryption protects its own image resources

Source: Internet
Author: User

Currently, image resources of most Android applications can be used directly after being decompiled. If you do not want your image resources to be decompiled for use, you should first consider encrypting the images. Here, I wrote a simple encryption method for images, hoping to be useful to you.

The first step is the encryption part. Here we use a simple difference or to encrypt the image. Create a Java project or simply write down the following code using a text file, and then execute the code in the command line as follows:

1 import Java. io. file; 2 3 Import javax. imageIO. stream. fileimageinputstream; 4 Import javax. imageIO. stream. fileimageoutputstream; 5 import javax. imageIO. stream. imageinputstream; 6 Import javax. imageIO. stream. imageoutputstream; 7 8 public class encriptdemo {9 10 public static final int xor_const = 0x99; // key 11 12/** 13 * @ Param args14 */15 public static void main (string [] ARGs) {16 17 file load = new file ("/home/Jade/desktop/loading.png"); 18 file loaddest = new file ("/home/Jade/desktop/loading. dat "); 19 file loadd = new file ("/home/Jade/desktop/loading_depr.png "); 20 21 try {22 encrimg (load, loaddest); 23 encrimg (loaddest, loadd); 24} catch (exception e) {25 E. printstacktrace (); 26} 27} 28 29 public static void encrimg (File SRC, file DEST) throws exception {30 imageinputstream FD = new fileimageinputstream (SRC ); 31 imageoutputstream Fos = new fileimageoutputstream (DEST); 32 33 int read; 34 while (read = FCM. read ()>-1) {35 FOS. write (read ^ xor_const); 36} 37 FOS. flush (); 38 FOS. close (); 39 fi. close (); 40} 41 42}


The above Code does not determine whether the file exists. Because this code encrypts the file, check whether the file exists. If you are familiar with the exception or, you should think about it. In fact, the above encrimg (File SRC, file DEST) method can be encrypted and decrypted, and two calls are made in main, it is used for encryption for the first time and decrypted for the second time. This completes the encryption program. Next, let's take a look at the decryption method in Android (the above encryption code uses javax. imageIO. related Classes in the stream package, so it cannot be executed in the android project by default ).

Copy the encrypted loading. dat file to the assets folder of the android project, and add an imageview with the ID of IMG to layout. Then, you can execute the following code in the activity:

1 imageview IMG = (imageview) findviewbyid (R. Id. IMG); 2 Bitmap bitmap = imageadapter. readbitmap (this, "loading. dat"); 3 if (Bitmap! = NULL) {4 IMG. setimagebitmap (Bitmap); 5} else {6 system. Out. println ("picture is blank"); 7}


Here, the static method readbitmap of imageadapter is called. This method is just a demo for reading the author. Therefore, ANR and other processing are not considered. For a formal project, it is best to put the image loading and decryption part in an independent thread for execution. The imageadapter code is as follows:

 1 import; 2 import; 3 import java.util.ArrayList; 4 import java.util.List; 5  6 import android.content.Context; 7 import; 8 import; 9 10 public class ImageAdapter { 11 12     public static Bitmap readBitmap(Context context, String fileName) {13         Bitmap bitmap = null;14         List list = new ArrayList(); 15         try {16             InputStream is = context.getAssets().open(fileName);17             int read;18                 while ((read = > -1) {19                 read = read ^ 0X99;20                 list.add((byte)read);21                 }22 23                 byte[] arr = new byte[list.size()];24                 int i = 0;25                 for(Byte item : list) {26                 arr[i++] = item;27                 }28             bitmap = BitmapFactory.decodeByteArray(arr, 0, list.size());29             System.out.println(bitmap);30         } catch (IOException e) {31             e.printStackTrace();32         }33         return bitmap;34     }35 36 }


The author's method is not the best method. Here it is just a simple idea for friends who need to protect their image resources.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.