程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 128進制加密數據示例分享

128進制加密數據示例分享

編輯:關於JAVA

128進制加密數據示例分享。本站提示廣大學習愛好者:(128進制加密數據示例分享)文章只能為提供參考,不一定能成為您想要的結果。以下是128進制加密數據示例分享正文


128進制加密數據,可以自定符號表,應用本身愛好的符號做加密


package com.wmly.enc;

import java.util.HashMap;

/**
 * 128進制加解密, 一個符號可表現7個bit
 * 可以自界說符號表, 符號不克不及反復
 */
public class MyBASE128 {

 public static final char[] symbolTable = new char[128];
 public static final HashMap<Character, Integer> indexTable = new HashMap<>(128);
 static {
  int i = 0;
  for (int j = 0; j < 128; j++) {
//   symbolTable[j] = (char) j;
   if('A' <= j && j <= 'Z'
    || 'a' <= j && j <= 'z'
    || '0' <= j && j <= '9') {
    symbolTable[i++] = (char) j;
   }
  }
  for (char c : "這是中國人寫地[備思要愛發]編解碼法式&具有奇特的自界說符號表|不外布能應用反復字,汗啊(還差一十二個知*.#)。為我們加油吧,親!".toCharArray()) {
   symbolTable[i++] = c;
  }

  checkTable();

  for (int j = 0; j < 128; j++) {
   indexTable.put(symbolTable[j], j);
  }
 }

 private static void checkTable() throws Error {
  if(symbolTable[127] == 0) {
   throw new Error("符號表長度不准確!");
  }
  for (char a : symbolTable) {
   int count = 0;
   for (char b : symbolTable) {
    if(a == b) {
     count++;
    }
   }
   if(count > 2) {
    throw new Error("符號表有反復符號!");
   }
  }
 }

 public String encode(byte[] data) {
  if(data == null || data.length == 0) {
   return new String();
  }
  StringBuilder result = new StringBuilder();
  int tail = 0;
  for (int i = 0; i < data.length; i++) {
   int mov = (i % 7 + 1);
   int curr = 0xFF & data[i];
   int code = tail + (curr >> mov);
   result.append(symbolTable[code]);
   tail = (0xFF & (curr << (8 - mov))) >> 1;
   if(mov == 7) {
    result.append(symbolTable[tail]);
    tail = 0;
   }
  }
  result.append(symbolTable[tail]);
  return result.toString();
 }

 public byte[] decode(String base128) {
  if(base128 == null || base128.length() == 0) {
   return new byte[] { };
  }
  int length = (int) Math.floor(base128.length() * 0.875);
  byte[] result = new byte[length];
  int idx = 0;
  int head = indexTable.get(base128.charAt(0)) << 1;
  for (int i = 1; i < base128.length();) {
   int mod = i % 8;
   int code = indexTable.get(base128.charAt(i++));
   result[idx++] = (byte) (0xFF & (head + (code >> (7 - mod))));
   if(mod == 7) {
    head = 0xFF & (indexTable.get(base128.charAt(i++)) << 1);
   } else {
    head = 0xFF & (code << (mod + 1));
   }
  }
  return result;
 }

 ///////////////////////測試辦法///////////////////////////////
 public static void main(String[] args) {
  MyBASE128 base128 = new MyBASE128();
  test(base128);

  String txt = "這是我的加解密測試";
  String enc = base128.encode(txt.getBytes());
  System.out.println(enc);
  System.out.println("----------------");
  System.out.println(new String(base128.decode(enc)));
 }

 private static void test(MyBASE128 base128) {
  for (int i = 0; i < 10000; i++) {
   String r = randomData();
   String d = new String(base128.decode(base128.encode(r.getBytes())));
   if(!r.equals(d)) {
//    d = new String(base128.decode(base128.encode(r.getBytes())));
    System.out.println("加解密掉敗!: " + r);
   }
  }
 }

 private static String randomData() {
  String textString = "了咖啡機累啊戴假發\n\r哦-";
  int start = random(0, textString.length() - 3);
  int end = random(start + 1, textString.length() - 1);
  return textString.substring(start, end);
 }

 private static int random(int i, int j) {
  return (int) Math.ceil(Math.random()*(j-i)+i);
 }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved