Données chiffrées En 128, vous pouvez personnaliser le tableau des symboles et utiliser vos symboles préférés pour le cryptage
La copie de code est la suivante:
package com.wmly.enc;
import java.util.hashmap;
/ **
* Cryptage et décryptage 128 bits, un symbole peut représenter 7 bits
* Vous pouvez personnaliser le tableau des symboles, mais le symbole ne peut pas être répété
* /
classe publique MyBase128 {
Public Static Final Char [] Symboltable = New Char [128];
public static final hashmap <caractère, entier> indextable = new hashmap <> (128);
statique {
int i = 0;
pour (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;
}
}
pour (Char C: "Ceci est écrit par le programme de codec des gens chinois [Bie si yao aifa] et a une table de symboles personnalisée unique | Mais le tissu peut utiliser des mots en double, la sueur (il s'agit toujours de 12 connaissances *. #). Venez pour nous, mon cher!
symboltable [i ++] = c;
}
checkTable ();
pour (int j = 0; j <128; j ++) {
Indextable.put (Symboltable [J], J);
}
}
private static void checkTable () lance une erreur {
if (symboltable [127] == 0) {
Jetez une nouvelle erreur ("La longueur du tableau des symboles est incorrecte!");
}
pour (char a: symboltable) {
int count = 0;
pour (char b: symboltable) {
if (a == b) {
Count ++;
}
}
if (count> 2) {
Jetez une nouvelle erreur ("Le tableau des symboles a des symboles répétés!");
}
}
}
Encode de chaîne publique (data byte []) {
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);
résultat.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 ();
}
octet public [] Decode (String Base128) {
if (base128 == null || base128.length () == 0) {
return nouvel octet [] {};
}
int length = (int) math.floor (base128.length () * 0,875);
octet [] résultat = nouveau octet [longueur];
int idx = 0;
int head = indexable.get (base128.charat (0)) << 1;
pour (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 & (inxtable.get (base128.charat (i ++)) << 1);
} autre {
head = 0xff & (code << (mod + 1));
}
}
Résultat de retour;
}
////////////////////////////////////////////////////////////////////////// / ////////////////////////////////////////////////////////////////////////// / ////////////////////////////////////////////////////////////////////////// / ////////////////////////////////////////////////////////////////////////// / ////////////////////////////////////////////////////////////////////////// / ///// ///////////////
public static void main (String [] args) {
MyBase128 Base128 = new MyBase128 ();
test (base128);
String txt = "Ceci est mon test de chiffrement et de décryptage";
String enc = base128.encode (txt.getBytes ());
System.out.println (ENC);
System.out.println ("----------------");
System.out.println (nouvelle chaîne (base128.decode (ENC)));
}
Test de vide statique privé (MyBase128 Base128) {
pour (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 ("Encryption a échoué !:" + R);
}
}
}
chaîne statique privée randomdata () {
String textString = "Je suis fatigué de la cafetière et porte une perruque / 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 () * (ji) + i);
}
}