Verschlüsselte Daten in 128 können Sie die Symboltabelle anpassen und Ihre Lieblingssymbole für die Verschlüsselung verwenden
Die Codekopie lautet wie folgt:
Paket com.wmly.enc;
import Java.util.hashMap;
/**
* 128-Bit-Verschlüsselung und Entschlüsselung, ein Symbol kann 7 Bit darstellen
* Sie können die Symboltabelle anpassen, aber das Symbol kann nicht wiederholt werden
*/
öffentliche Klasse myBase128 {
öffentliches statisches Final -Char [] symbolble = neuer Char [128];
öffentliches statisches endgültiges Hashmap <Charakter, Integer> unbestreitbar = neuer Hashmap <> (128);
statisch {
int i = 0;
für (int j = 0; j <128; j ++) {
// symboltbar [j] = (char) j;
if ('a' <= j && j <= 'z'
||. 'a' <= j && j <= 'Z'
||.
symboltbar [i ++] = (char) j;
}
}
für (char c: "Dies wird von chinesischen Leuten geschrieben [Bie Si yao Aifa] Codec -Programm und hat eine einzigartige benutzerdefinierte Symboltabelle | Aber Tuch kann doppelte Wörter verwenden, Schweiß (es ist immer noch 12 Wissen*. #). uns, Liebes! ".toarArray ()) {
Symboltbar [i ++] = C;
}
kontrollbar ();
für (int j = 0; j <128; j ++) {
unbestreitbar.put (symbotbar [j], j);
}
}
private static void checktable () löst Fehler {aus
if (symboltbar [127] == 0) {
Neuen Fehler werfen ("Die Symboltabellelänge ist falsch!");
}
für (char a: symboltbar) {
int count = 0;
für (char b: symboltbar) {
if (a == b) {
zählen ++;
}
}
if (count> 2) {
Neuen Fehler werfen ("Die Symboltabelle hat wiederholte Symbole!");
}
}
}
public String codes (byte [] data) {
if (data == null || data.length == 0) {
neuer String () zurückgeben;
}
StringBuilder result = new StringBuilder ();
int schwanz = 0;
für (int i = 0; i <data.length; i ++) {
int mov = (i % 7 + 1);
int Curr = 0xff & data [i];
int code = schwanz + (Curr >> mov);
result.Append (symboltbar [Code]);
Tail = (0xff & (Curr << (8 - mov)) >> 1;
if (mov == 7) {
result.Append (symboltbar [Schwanz]);
Schwanz = 0;
}
}
result.Append (symboltbar [Schwanz]);
Rückgabeergebnis.ToString ();
}
public byte [] decode (String base128) {
if (base128 == null || base128.length () == 0) {
Neue Byte zurückgeben [] {};
}
int länge = (int) math.floor (Base128.Length () * 0,875);
byte [] result = new Byte [Länge];
int idx = 0;
int head = undertxtable.get (Base128.charat (0)) << 1;
für (int i = 1; i <base128.length ();) {
int mod = i % 8;
int code = inxtierbar.get (Base128.charat (i ++));
Ergebnis [idx ++] = (byte) (0xff & (Kopf+(Code >> (7 - mod))));
if (mod == 7) {
head = 0xff & (unbestreitbar.get (base128.charat (i ++)) << 1);
} anders {
head = 0xff & (Code << (mod + 1));
}
}
Rückgabeergebnis;
}
////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ///// //////////////////////
public static void main (String [] args) {
MyBase128 Base128 = neuer MyBase128 ();
Test (Base128);
String txt = "Dies ist meine Verschlüsselungs- und Entschlüsselungstest";
String ENC = Base128.Encode (txt.getBytes ());
System.out.println (ENC);
System.out.println ("----------------");
System.out.println (neue String (Base128.decode (ENC));
}
privater statischer Hohlraumtest (MyBase128 Base128) {
für (int i = 0; i <10000; i ++) {
String r = randomData ();
String d = neuer String (Base128.Decode (Base128.Encode (R.GetByTes ())));
if (! R.Equals (d)) {
// d = new String (Base128.decode (Base128.Encode (R.GetByTes ())));
System.out.println ("Verschlüsselung fehlgeschlagen !:" + r);
}
}
}
private statische Zeichenfolge randomData () {
String textString = "Ich habe es leid von Kaffeemaschine und trage eine Perücke/n/r-";
int start = random (0, textstring.length () - 3);
int end = random (start + 1, textstring.length () - 1);
return textstring.substring (Start, Ende);
}
private static int random (int i, int j) {
return (int) math.ceil (math.random ()*(ji)+i);
}
}