用异或法简单加密Android的图片资源

思路:本地用异或加密好图片---放入Android的assets文件夹下---在程序里用异或解密。

 

都知道解压APK文件能拿到程序的图片资源,为了保护图片资源不被盗用,可采用简单异或的方法对图片进行加密,这样即使解压APK文件,图片也无法打开。

第一步,通过创建一个JAVA工程来对本地图片异或加密。

 

[java] view plain copy

  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileOutputStream;  
  4.   
  5. /** 
  6.  * 用异或方法加密和解密图片 
  7.  * @author Administrator 
  8.  * 
  9.  */  
  10. public class EncryptDemo {  
  11.   
  12.     public static final int XOR_CONST = 0X99// 异或密钥  
  13.   
  14.     /** 
  15.      * @param args 
  16.      */  
  17.     public static void main(String[] args) {  
  18.   
  19.         File load = new File("D:\\Windows\\Desktop\\eee.jpg");      //原图  
  20.         File result = new File("D:\\Windows\\Desktop\\eee.dat");    //加密后得到的文件  
  21.         File reload = new File("D:\\Windows\\Desktop\\eee2.jpg");   //解密后得到的原图  
  22.   
  23.         try {  
  24.             encryptImg(load, result);   // 加密原图  
  25.             encryptImg(result, reload); // 对加密后的文件再加密得到原图  
  26.         } catch (Exception e) {  
  27.             e.printStackTrace();  
  28.         }  
  29.     }  
  30.   
  31.     /** 
  32.      * 文件流异或加密 
  33.      * @param src 
  34.      * @param dest 
  35.      * @throws Exception 
  36.      */  
  37.     public static void encryptImg(File load, File result) throws Exception {  
  38.         FileInputStream fis = new FileInputStream(load);  
  39.         FileOutputStream fos = new FileOutputStream(result);  
  40.   
  41.         int read;  
  42.         while ((read = fis.read()) > -1) {  
  43.             fos.write(read ^ XOR_CONST);    //进行异或运算并写入结果  
  44.         }  
  45.         fos.flush();  
  46.         fos.close();  
  47.         fis.close();  
  48.     }  
  49.   
  50. }  

 

 

 

 

 

第二步,把生成的加密文件.dat放到android工程的assets文件夹下。

 

第三步,在android程序创建一个ImageView,用来显示解密图片资源。

 

 

[java] view plain copy

  1. package com.example.encryptdemo2;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7. import com.example.encryptdemo2.R;  
  8. import android.os.Bundle;  
  9. import android.app.Activity;  
  10. import android.content.Context;  
  11. import android.graphics.Bitmap;  
  12. import android.graphics.BitmapFactory;  
  13. import android.widget.ImageView;  
  14.   
  15. /** 
  16.  * android中用异或解密图片资源 
  17.  * @author Administrator 
  18.  * 
  19.  */  
  20. public class MainActivity extends Activity {  
  21.       
  22.     private ImageView img;  
  23.     private Bitmap bitmap;  
  24.       
  25.     @Override  
  26.     protected void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.activity_main);  
  29.           
  30.         img = (ImageView) findViewById(R.id.img);  
  31.         bitmap = readBitmap(this"eee.dat");   //获取解密后的图片  
  32.           
  33.         if(bitmap != null) {  
  34.             img.setImageBitmap(bitmap);  
  35.         } else {  
  36.             System.out.println("图片为空");  
  37.         }  
  38.     }  
  39.   
  40.     /** 
  41.      * 异或解密图片方法 
  42.      * @param context 
  43.      * @param fileName 
  44.      * @return 
  45.      */  
  46.     public static Bitmap readBitmap(Context context, String fileName) {  
  47.         Bitmap bitmap = null;  
  48.         List<Byte> list = new ArrayList<Byte>();  
  49.         try {  
  50.             InputStream is = context.getAssets().open(fileName);  
  51.             int read;  
  52.             while ((read = is.read()) > -1) {  
  53.                 read = read ^ 0X99;     // 密钥  
  54.                 list.add((byte) read);  
  55.             }  
  56.   
  57.             byte[] arr = new byte[list.size()];  
  58.             for (int i = 0; i < arr.length; i++) {  
  59.                 arr[i] = (Byte) list.get(i);    //list转成byte[]  
  60.             }  
  61.             //通过byte数组获取二进制图片流  
  62.             bitmap = BitmapFactory.decodeByteArray(arr, 0, arr.length);  
  63.             System.out.println(bitmap);  
  64.         } catch (IOException e) {  
  65.             e.printStackTrace();  
  66.         }  
  67.         return bitmap;  
  68.     }  
  69. }  
  70.  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值