国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android對稱加密與非對稱加密

2019-12-12 05:18:40
字體:
來源:轉載
供稿:網友

凱撒密碼

1. 介紹

凱撒密碼作為一種最為古老的對稱加密體制,在古羅馬的時候都已經很流行,他的基本思想是:通過把字母移動一定的位數來實現加密和解密。明文中的所有字母都在字母表上向后(或向前)按照一個固定數目進行偏移后被替換成密文。例如,當偏移量是3 的時候,所有的字母A 將被替換成D,B 變成E,由此可見,位數就是凱撒密碼加密和解密的密鑰。

例如:字符串”ABC”的每個字符都右移3 位則變成”DEF”,解密的時候”DEF”的每個字符左移3 位即能還原,如下圖所示:

這里寫圖片描述

2. 準備知識

 //字符轉換成ASCII 碼數值 char charA = 'a'; int intA = charA; //char 強轉為int 即得到對應的ASCII 碼值,'a'的值為97//ASCII 碼值轉成charint intA = 97;//97 對應的ASCII 碼'a'char charA = (char) intA; //int 值強轉為char 即得到對應的ASCII 字符,即'a'

3. 凱撒密碼的簡單代碼實現

 /**   * 加密   * @param input 數據源(需要加密的數據)   * @param key 秘鑰,即偏移量   * @return 返回加密后的數據   */  public static String encrypt(String input, int key) {    //得到字符串里的每一個字符    char[] array = input.toCharArray();    for (int i = 0; i < array.length; ++i) {      //字符轉換成ASCII 碼值      int ascii = array[i];      //字符偏移,例如a->b      ascii = ascii + key;      //ASCII 碼值轉換為char      char newChar = (char) ascii;      //替換原有字符      array[i] = newChar;      //以上4 行代碼可以簡寫為一行      //array[i] = (char) (array[i] + key);    }    //字符數組轉換成String    return new String(array);  }  /**   * 解密   * @param input 數據源(被加密后的數據)   * @param key 秘鑰,即偏移量   * @return 返回解密后的數據   */  public static String decrypt(String input, int key) {    //得到字符串里的每一個字符    char[] array = input.toCharArray();    for (int i = 0; i < array.length; ++i) {      //字符轉換成ASCII 碼值      int ascii = array[i];      //恢復字符偏移,例如b->a      ascii = ascii - key;      //ASCII 碼值轉換為char      char newChar = (char) ascii;      //替換原有字符      array[i] = newChar;      //以上4 行代碼可以簡寫為一行      //array[i] = (char) (array[i] - key);    }    //字符數組轉換成String    return new String(array);  }

代碼輸出結果:

這里寫圖片描述

4. 破解凱撒密碼:頻率分析法

凱撒密碼加密強度太低,只需要用頻度分析法即可破解。
在任何一種書面語言中,不同的字母或字母組合出現的頻率各不相同。而且,對于以這種語言書寫的任意一段文本,都具有大致相同的特征字母分布。比如,在英語中,字母E 出現的頻率很高,而X 則出現得較少。

英語文本中典型的字母分布情況如下圖所示:

這里寫圖片描述

5. 破解流程

統計密文里出現次數最多的字符,例如出現次數最多的字符是是'h'。

計算字符'h'到'e'的偏移量,值為3,則表示原文偏移了3 個位置。

將密文所有字符恢復偏移3 個位置。

注意點:統計密文里出現次數最多的字符時,需多統計幾個備選,因為最多的可能是空格或者其他字符,例如下圖出現次數最多的字符'#'是空格加密后的字符,'h'才是'e'偏移后的值。
這里寫圖片描述

解密時要多幾次嘗試,因為不一定出現次數最多的字符就是我們想要的目標字符,如下圖,第二次解密的結果才是正確的。

/** * 頻率分析法破解凱撒密碼 */public class FrequencyAnalysis {  //英文里出現次數最多的字符  private static final char MAGIC_CHAR = 'e';  //破解生成的最大文件數  private static final int DE_MAX_FILE = 4;  public static void main(String[] args) throws Exception {    //測試1,統計字符個數    //printCharCount("article1_en.txt");    //加密文件    //int key = 3;    //encryptFile("article1.txt", "article1_en.txt", key);    //讀取加密后的文件    String artile = file2String("article1_en.txt");    //解密(會生成多個備選文件)    decryptCaesarCode(artile, "article1_de.txt");  }  public static void printCharCount(String path) throws IOException{    String data = file2String(path);    List<Entry<Character, Integer>> mapList = getMaxCountChar(data);    for (Entry<Character, Integer> entry : mapList) {      //輸出前幾位的統計信息      System.out.println("字符'" + entry.getKey() + "'出現" + entry.getValue() + "次");    }  }  public static void encryptFile(String srcFile, String destFile, int key) throws IOException {    String artile = file2String(srcFile);    //加密文件    String encryptData = MyEncrypt.encrypt(artile, key);    //保存加密后的文件    string2File(encryptData, destFile);  }  /**   * 破解凱撒密碼   * @param input 數據源   * @return 返回解密后的數據   */  public static void decryptCaesarCode(String input, String destPath) {    int deCount = 0;//當前解密生成的備選文件數    //獲取出現頻率最高的字符信息(出現次數越多越靠前)    List<Entry<Character, Integer>> mapList = getMaxCountChar(input);    for (Entry<Character, Integer> entry : mapList) {      //限制解密文件備選數      if (deCount >= DE_MAX_FILE) {        break;      }      //輸出前幾位的統計信息      System.out.println("字符'" + entry.getKey() + "'出現" + entry.getValue() + "次");      ++deCount;      //出現次數最高的字符跟MAGIC_CHAR的偏移量即為秘鑰      int key = entry.getKey() - MAGIC_CHAR;      System.out.println("猜測key = " + key + ", 解密生成第" + deCount + "個備選文件" + "/n");      String decrypt = MyEncrypt.decrypt(input, key);      String fileName = "de_" + deCount + destPath;      string2File(decrypt, fileName);    }  }  //統計String里出現最多的字符  public static List<Entry<Character, Integer>> getMaxCountChar(String data) {    Map<Character, Integer> map = new HashMap<Character, Integer>();    char[] array = data.toCharArray();    for (char c : array) {      if(!map.containsKey(c)) {        map.put(c, 1);      }else{        Integer count = map.get(c);        map.put(c, count + 1);      }    }    //輸出統計信息    /*for (Entry<Character, Integer> entry : map.entrySet()) {      System.out.println(entry.getKey() + "出現" + entry.getValue() + "次");    }*/    //獲取獲取最大值    int maxCount = 0;    for (Entry<Character, Integer> entry : map.entrySet()) {      //不統計空格      if (/*entry.getKey() != ' ' && */entry.getValue() > maxCount) {         maxCount = entry.getValue();      }    }    //map轉換成list便于排序    List<Entry<Character, Integer>> mapList = new ArrayList<Map.Entry<Character,Integer>>(map.entrySet());    //根據字符出現次數排序    Collections.sort(mapList, new Comparator<Entry<Character, Integer>>(){      @Override      public int compare(Entry<Character, Integer> o1,          Entry<Character, Integer> o2) {        return o2.getValue().compareTo(o1.getValue());      }    });    return mapList;  }  public static String file2String(String path) throws IOException {    FileReader reader = new FileReader(new File(path));    char[] buffer = new char[1024];    int len = -1;    StringBuffer sb = new StringBuffer();    while ((len = reader.read(buffer)) != -1) {      sb.append(buffer, 0, len);    }    return sb.toString();  }  public static void string2File(String data, String path){    FileWriter writer = null;    try {      writer = new FileWriter(new File(path));      writer.write(data);    } catch (Exception e) {      e.printStackTrace();    }finally {      if (writer != null) {        try {          writer.close();        } catch (IOException e) {          e.printStackTrace();        }      }    }  }}

這里寫圖片描述

對稱加密

介紹

加密和解密都使用同一把秘鑰,這種加密方法稱為對稱加密,也稱為單密鑰加密。
簡單理解為:加密解密都是同一把鑰匙。
凱撒密碼就屬于對稱加密,他的字符偏移量即為秘鑰。

對稱加密常用算法

AES、DES、3DES、TDEA、Blowfish、RC2、RC4、RC5、IDEA、SKIPJACK 等。

DES:全稱為Data Encryption Standard,即數據加密標準,是一種使用密鑰加密的塊算法,1976 年被美國聯邦政府的國家標準局確定為聯邦資料處理標準(FIPS),隨后在國際上廣泛流傳開來。

3DES:也叫Triple DES,是三重數據加密算法(TDEA,Triple Data Encryption Algorithm)塊密碼的通稱。
它相當于是對每個數據塊應用三次DES 加密算法。由于計算機運算能力的增強,原版DES 密碼的密鑰長度變得容易被暴力破解;3DES 即是設計用來提供一種相對簡單的方法,即通過增加DES 的密鑰長度來避免類似的攻擊,而不是設計一種全新的塊密碼算法。

AES: 高級加密標準(英語:Advanced Encryption Standard,縮寫:AES),在密碼學中又稱Rijndael 加密法,是美國聯邦政府采用的一種區塊加密標準。這個標準用來替代原先的DES,已經被多方分析且廣為全世界所使用。經過五年的甄選流程,高級加密標準由美國國家標準與技術研究院(NIST)于2001 年11 月26 日發布于FIPS PUB 197,并在2002 年5 月26 日成為有效的標準。2006 年,高級加密標準已然成為對稱密鑰加密中最流行的算法之一。

DES 算法簡介

DES 加密原理(對比特位進行操作,交換位置,異或等等,無需詳細了解)

準備知識

Bit 是計算機最小的傳輸單位。以0 或1 來表示比特位的值
例如數字3 對應的二進制數據為:00000011

代碼示例

 int i = 97; String bit = Integer.toBinaryString(i); //輸出:97 對應的二進制數據為: 1100001 System.out.println(i + "對應的二進制數據為: " + bit);

Byte 與Bit 區別

數據存儲是以“字節”(Byte)為單位,數據傳輸是以大多是以“位”(bit,又名“比特”)為單位,一個位就代表一個0 或1(即二進制),每8 個位(bit,簡寫為b)組成一個字節(Byte,簡寫為B),是最小一級的信息單位。

Byte 的取值范圍:

//byte 的取值范圍:-128 到127System.out.println(Byte.MIN_VALUE + "到" + Byte.MAX_VALUE);

即10000000 到01111111 之間,一個字節占8 個比特位

二進制轉十進制圖示:

這里寫圖片描述

任何字符串都可以轉換為字節數組

String data = "1234abcd";byte[] bytes = data.getBytes();//內容為:49 50 51 52 97 98 99 100

上面數據49 50 51 52 97 98 99 100 對應的二進制數據(即比特位為):

00110001
00110010
00110011
00110100
01100001
01100010
01100011
01100100

將他們間距調大一點,可看做一個矩陣:

0 0 1 1 0 0 0 1
0 0 1 1 0 0 1 0
0 0 1 1 0 0 1 1
0 0 1 1 0 1 0 0
0 1 1 0 0 0 0 1
0 1 1 0 0 0 1 0
0 1 1 0 0 0 1 1
0 1 1 0 0 1 0 0

之后可對他們進行各種操作,例如交換位置、分割、異或運算等,常見的加密方式就是這樣操作比特位的。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 扎鲁特旗| 东丰县| 西平县| 闵行区| 阳谷县| 应用必备| 土默特右旗| 蒙自县| 城固县| 仪陇县| 阿鲁科尔沁旗| 油尖旺区| 广宗县| 东兰县| 晋州市| 南江县| 武清区| 鸡西市| 盐边县| 格尔木市| 宁津县| 遂溪县| 承德县| 沽源县| 浪卡子县| 股票| 杂多县| 兴海县| 宝丰县| 息烽县| 胶南市| 临江市| 梁平县| 荔浦县| 即墨市| 宜都市| 厦门市| 句容市| 辽阳县| 乌苏市| 恩平市|