128 in-process encrypted data sample sharing _java

Source: Internet
Author: User

128 encryption data, you can customize the symbol table, using their favorite symbols to do encryption

Copy Code code as follows:

Package Com.wmly.enc;

Import Java.util.HashMap;

/**
* 128-plus decryption, a symbol can represent 7 bit
* Can customize the symbol table, the symbol can not repeat
*/
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: "This is written by the Chinese [think about love] codec & have a unique custom symbol table | But the cloth can use repeated words, sweat AH (still 12 to know *.#). Cheer for us, kiss! ". 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 ("The symbol table length is not correct!") ");
  }
  for (char a:symboltable) {
   int count = 0;
   for (char b:symboltable) {
    if (a = = b) {
      count++;
    }
   }
   if (Count > 2) {
    throw new Error ("symbol table has duplicate symbol!") ");
   }
  }
 }

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;
}

Test method///////////////////////////////
public static void Main (string[] args) {
MyBASE128 base128 = new MyBASE128 ();
Test (base128);

String txt = "This is my encryption and decryption test";
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 ("Add decryption failed!") : "+ R");
}
}
}

private static String Randomdata () {
String TextString = "The coffee machine tired, wear a wig \n\r oh-";
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);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.