128の暗号化されたデータ、シンボルテーブルをカスタマイズして、暗号化にお気に入りのシンボルを使用できます
コードコピーは次のとおりです。
パッケージcom.wmly.enc;
java.util.hashmapをインポートします。
/**
* 128ビットの暗号化と復号化、1つのシンボルが7ビットを表すことができます
*シンボルテーブルをカスタマイズできますが、シンボルを繰り返すことはできません
*/
パブリッククラスmybase128 {
public static final char [] symboltable = new char [128];
public static final hashmap <文字、integer> undextable = new Hashmap <>(128);
static {
int i = 0;
for(int j = 0; j <128; j ++){
// symboltable [j] =(char)j;
if( 'a' <= j && j <= 'z'
||。
||。 '0' <= j <= '9'){
SymbolTable [i ++] =(char)j;
}
}
for(Char C:「これは中国人によって書かれています[bie si yao aifa]コーデックプログラムとユニークなカスタムシンボルテーブルがあります|しかし、布は重複した単語、汗を使用します(まだ12の知識*。#)。私たち、親愛なる!」
SymbolTable [i ++] = c;
}
checkTable();
for(int j = 0; j <128; j ++){
Indextable.put(symboltable [j]、j);
}
}
private static void checktable()スローエラー{
if(symboltable [127] == 0){
新しいエラーをスローします(「シンボルテーブルの長さが間違っています!」);
}
for(char a:symboltable){
int count = 0;
for(char b:symboltable){
if(a == b){
count ++;
}
}
if(count> 2){
新しいエラーをスローします(「シンボルテーブルにはシンボルが繰り返されます!」);
}
}
}
public string encode(byte [] data){
if(data == null || data.length == 0){
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]);
テール=(0xff&(curr <<(8 -mov)))>> 1;
if(mov == 7){
result.append(symboltable [tail]);
テール= 0;
}
}
result.append(symboltable [tail]);
return result.toString();
}
public byte [] decode(string base128){
if(base128 == null || base128.length()== 0){
新しいbyte [] {}を返します。
}
int length =(int)math.floor(base128.length() * 0.875);
byte [] result = new byte [length];
int idx = 0;
int head = dexextable.get(base128.charat(0))<< 1;
for(int i = 1; i <base128.length();){
int mod = i%8;
int code = undextable.get(base128.charat(i ++));
result [idx ++] =(byte)(0xff&(head+(code >>(7 -mod))));
if(mod == 7){
head = 0xff&(indextable.get(base128.charat(i ++))<< 1);
} それ以外 {
head = 0xff&(code <<(mod + 1));
}
}
返品結果;
}
///////////////////////////////////////////////// ///////////////////////////////////////////////// ///////////////////////////////////////////////// ///////////////////////////////////////////////// ///////////////////////////////////////////////// //// ////////////
public static void main(string [] args){
mybase128 base128 = new mybase128();
テスト(base128);
string txt = "これは私の暗号化と復号化テストです";
文字列enc = base128.encode(txt.getBytes());
System.out.println(enc);
System.out.println( "---------------");
System.out.println(new String(base128.Decode(enc)));
}
private static voidテスト(mybase128 base128){
for(int i = 0; i <10000; i ++){
文字列r = randomData();
文字列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);
textString.substring(start、end)を返します。
}
private static int random(int i、int j){
return(int)math.ceil(math.random()*(ji)+i);
}
}