你也可以使用的加密解密

一、簡介

加密技術是最常用的安全保密手段,利用技術手段把重要的數據變爲亂碼(加密)傳送,到達目的地後再用相同或不同的手段還原(解密)

今天主要介紹通過異或加密。

二、技術介紹

異或是一種數學運算符,計算機符號爲“xor”,數學符號爲“⊕”

如果a、b兩個值不相同,則異或結果爲1。如果a、b兩個值相同,異或結果爲0。

運算法則有:

1. a ⊕ a = 0

2. a ⊕ b = b ⊕ a

3. a ⊕b ⊕ c = a ⊕ (b ⊕ c) = (a ⊕ b) ⊕ c;

4. d = a ⊕ b ⊕ c 可以推出 a = d ⊕ b ⊕ c.

5. a ⊕ b ⊕ a = b.

6.若x是二進制數0101,y是二進制數1011;

則x⊕y=1110

只有在兩個比較的位不同時其結果是1,否則結果爲0

即“兩個輸入相同時爲0,不同則爲1”!

 

三、實踐

3.1.原理

  •  
  •  
  •  
int i = 3;System.out.println(i^456);//459System.out.println(i^456^456);//

3.2.應用

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
public static void main(String[] args) throws Exception {        //加密        FileInputStream in = new FileInputStream("D:\\testFile\\test1.jpg");        FileOutputStream out = new FileOutputStream("D:\\testFile\\test2.dat");
        BufferedInputStream bin = new BufferedInputStream(in);        BufferedOutputStream bout = new BufferedOutputStream(out);
        int len;        while((len=bin.read())!=-1){            bout.write(len^456);        }        bout.close();        bin.close();    }

 生成的test2.dat 文件無法用圖片編輯器打開

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
public static void main(String[] args) throws Exception {        //解密        FileInputStream in = new FileInputStream("D:\\testFile\\test2.dat");        FileOutputStream out = new FileOutputStream("D:\\testFile\\test3.jpg");
        BufferedInputStream bin = new BufferedInputStream(in);        BufferedOutputStream bout = new BufferedOutputStream(out);
        int len;        while((len=bin.read())!=-1){            bout.write(len^456);        }        bout.close();        bin.close();    }

 生成的test3與test1文件一樣,成功解密

3.3.延伸

    我們發現微信的緩存聊天圖片均使用加密處理

    經過測試我們發現,微信的加密圖片也是經過異或處理,所以我們只需要知道異或值就可以還原圖片。jpg圖片文件頭一般爲FF D8 開頭的,所以使用科學計算器,計算異或值。

四、總結

對圖片加密解密的方法還很多,比如將圖片轉成base64格式;或者在輸出流的時候加幾個特殊字符,輸入流時判斷並刪減特殊字符還原,後續繼續介紹加密解密思路

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章