GO language encryption Algorithm (with Java version translation)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The following code is mainly to look at the DES encryption algorithm, the discovery of limited capacity only a little bit, then let me that point and base64 encryption used together.

The code is as follows:

Package Xlibimport ("Crypto/md5" "FMT" "io") func Xencode (b, Key []byte) (R_buf []byte) {L: = Len (key) if l%64! = 0 | | l = = 64 {return}m: = Make (Map[byte]byte) for I: = 0; I < 64; i++ {m[key[i]] = byte (i)}b_len: = Len (b) k: = b_len% 8r_buf = append (R_buf, byte (k)) for I: = 0; I < 8-k && k! = 0; i++ {b = append (b, 0)}for I: = 0; i < B_len; i + = 8 {var tmp [8]bytefor J: = 0; J < 8; J + + {Tmp[j] = b[i+j]}res_tmp: = XENCODE8 (tmp, key, M) for J: = 0; J < Len (r ES_TMP); J + + {r_buf = append (R_buf, res_tmp[j])}}return}func Xdecode (b, Key []byte) (R_buf []byte) {L: = Len (key) if l%64! = 0 | | l = = Return}m: = Make (Map[byte]byte) for I: = 0; I < 64; i++ {m[key[i]] = byte (i)}b_len: = Len (b) for i: = 1; i < B_len; i + = 8 {var tmp [8]bytefor J: = 0; J < 8; J + + {Tmp[j] = b[i+j]}res_tmp: = XDECODE8 (tmp, key, M) for J: = 0; J < Len (r ES_TMP); J + + {r_buf = append (R_buf, res_tmp[j])}}if b[0]! = 0 {r_buf = r_buf[:b_len-1-(8-int (b[0)))]}return}func Base64decode (b []b Yte) (R_buf []bYte) {L: = Len (b) if (L-1)%4! = 0 {return}for I: = 1; i < l; i + = 4 {r_buf = append (R_buf, (m[b[i]]<<2) | ( M[B[I+1]]&GT;&GT;4), (m[b[i+1]]<<4) | (M[B[I+2]]&GT;&GT;2), (m[b[i+2]]<<6) | (M[b[i+3]])} L = Len (r_buf) switch m[b[0]] {case 1:R_BUF = r_buf[:l-2]case 2:r_buf = R_buf[:l-1]}return}func Base64Encode (b []byte) (R_b UF []byte) {L: = Len (b) k: = l% 3r_buf = append (R_buf, tb[k]) switch k {case 2:b = append (b, 0) Case 1:b = append (b, 0, 0)}f or I: = 0; I < L; i + = 3 {r_buf = append (R_buf, tb[b[i]>>2]) r_buf = append (R_buf, tb[((b[i]&0x03) <<4) | ( B[I+1]&GT;&GT;4)] R_buf = append (R_buf, tb[((b[i+1]<<4) >>2) | ( B[I+2]&GT;&GT;6)] R_buf = append (R_buf, tb[b[i+2]&0x3f])}return}func createlist () (R_str string) {tmp: = make ([] BYTE, Tb_len) for I: = 0; i < Tb_len; i++ {Tmp[i] = tb[i]}for I: = 0; I < 10000; i++ {A: = Int (randint31n ()) B: = Int (randint31n)//fmt. Println (A, b) if a = = b {Continue}tmp[a] ^= tmp[b]tmp[b] ^= tmp[a]tmp[a] ^= tmp[b]}r_str = String (TMP) Return}func CreateKey () (R_str string) {for I: = 0; i <; i++ {r_str + = CreateList ()}return}func Setbit (b []byte, position uint, bit byte) (err error) {if bit! = 0 && bit! = 1 {err = FMT. Errorf ("bit must be 0 or 1") return}k: = position% 8p: = position/8if bit = = 0 {b[p] &= ^ (1 << k)} else {B[p] |= (1 << k)}return}func getbit (b []byte, Position uint) (R byte) {r = 0if len (b) *8 < int (position) {r = 0xFFretur N}k: = position% 8p: = position/8r |= (b[p] >> k) & 1return}func XEncode8 (b [8]byte, Key []byte, M map[byte]b Yte) (R_buf []byte) {tmp: = make ([]byte, 8) for I: = 0; i < 8; i++ {Tmp[i] = B[i]}block_len: = 64key_len: = Len (key) R_bu f = Make ([]byte, 8) for I: = Block_len; i < Key_len; i + = Block_len {for j: = 0; J < Block_len; J + + {r: = getbit (tmp, UINT (M[key[j+i])) setbit (R_buf, UINT (j), R)}for J: = 0 ; J < 8; J + + {Tmp[j] = R_buf[j]}}return}func XDecode8 (b [8]byte, Key []byte, M Map[byte]byte) (R_buf []byte) {tmp: = MakE ([]byte, 8) for I: = 0; I < 8;  i++ {Tmp[i] = B[i]}block_len: = 64key_len: = Len (key) R_buf = make ([]byte, 8) K_len: = key_len/block_len-1k: = Make ([]int, key_len/block_len-1) for I: = 0; i < key_len/block_len-1; i++ {K[i] = (i + 1) * block_len}for i: = k_len-1; I >= 0;  i--{for J: = 0, J < Block_len, J + + {r: = getbit (tmp, UINT (j)) Setbit (r_buf, UINT (M[key[j+k[i]]), R)}for J: = 0; J < 8; J + + {Tmp[j] = R_buf[j]}}return}func Md5encode (b []byte) (R_buf []byte) {h: = MD5. New () Io. WriteString (H, string (b)) R_buf = H.sum (nil) Return}func md5string (S string) (R_str string) {h: = MD5. New () Io. WriteString (H, s) r_str = Fmt. Sprintf ("%x", H.sum (nil)) Return}func init () {m = Make (Map[byte]byte) L: = Byte (len (TB)) for I: = byte (0); I < L; i++ {m[t B[i]] = i}//fmt. Println (M)}


The instance code is as follows:

Package Mainimport ("./xlib" "FMT")//generated by CreateKey var k string = "4xwefdubcsklwrugaon+9r23hoxgjcnmi= faqzqdy0vky8mise6tvltp75zhpb1jnfipmduenpwkw70j+9xzabr1a=5omz4tyqrfc28ih3xlqvgh6bgodskseylujcvt1p+ k9rgnk7i4vdwd2wuslbiehcg=jfshuyaa8f6eblxqoo3jnpzztrqtymcvmx50s0lntpvnmwq4mrlzpdjak5oguz8xwkbrt3c92yviy= gjq61eosiudbfxf+ha7echtwzdrb6h8d1wljuzjyb7qux0ksmnprlit5xapfqmc2goysa+vc9vehf34ni=kogeotws+ k6p80g7mlert1df5xaqnkviq=gswvfe4cmz2habbjn9ourpjuylydz3ihxcu51k0hiy4nxecefdfcytqbdjabz67mtgkrwmlswpri2o8va+ oqjlpvzhn=x93sguw6b7anvwlx=1qlt2nihatxd8r+3iyqzdv0ojogpgkkcm5hfu94ysrbpfsmcujeze8monq7w=zf4rrpevbtcws2+ qsgexm0ytjcd5illufk9ob63jhhadpxyuvzngkia1iqmjsnd5glihk4bsayayfjpzur9w326h=oot+ zrkvenvptcxqbfc0ldg1u8mxw7efs7xbic6t3ezoprfuhba42ajmygjlr5+8x=ulkqd1m9opgtnicvhzkdswnwyq0vefdbk+ gx1hocovnqvuz0mluhn7j5ltcka=zwmss3p4e9tw6iapygjfxdbyr2rq8iexngha70amg8k3bsirrpt9huv= woonxdvqwe4cebpjlqlud256styzcfziykj1+mfvvo2ldixs68knhthk9q5ewarnp4ti=l+ cmygorymfjz13squpb0d7uxjzwecgfbaisfeeyhycu7mrqp6wdwiczbknuzk5hl2gd+0v9f1n8=4axqbo3moagprjjxlvtstnvkbr8ndrehv67lxbgp0uyzqlwpsesqi14jc2tdxuao9jmmwczh5=+ Gtka3ffyio2jc3r7u0raxecpofxamt1sdow9fkzbgi5vqpye4+ud8hybil= vnjwktzmgs6lhnqsoiplyu9cgj6fd3xmnrjyswbdivbz4cz7qmv8p=50kn1etx+f2htuowarhkelaqgho4d1w2f+ tip3ebmihfjxzvq0r5a7xklao8uv6yudbwcmlzt9sgeynpnqrkcg=js1r26tlippx9=azgewqjfxkw7mdv8zigs+ 3rkyqjncbu0bfloshydavcueh45montbqupt493dknq5uywiytbv0vajoswpecxdgj=nl7e2fo18zfscr+ Zri6mlhhmxakglbzhq9l06rfrjabh5tz47xui+gokdcsnp8=dmejxamtqpy2vifogskunw3wcye1vg9x+f5ghrik4zo2= y16achrn7vlbepdqwzeayqbmilmudsn3votpx8wuj0ctjfsk0+djex6= gyag5ifvz3bvliukstw7cd8atpxp4je9kmzmyonhh2burnqqfc1olwrszqm1lghevfa32synp7ci6xe9njdoyvwrtdmhu8pxwjzfb= ugbkr0tc4sialq5+koc=ashxyobz3pc8fnk7w0iwdmytqgpl4vr9zg6uj5+haeslrobqx1dmun2tjvkeiffseupb= 5t9cxd2ujytwcmdnr6nhvfgyxg01voi3q4sbarjoll78zikazekw+pmqhvpec09bjx=6hkolismf8pumwkntysqgadi7z4yoberdhv51gna+ Xqt3cu2zjwflruy43jhtls+hmvw1rlgim=9ci5xjtgesobfoadpkp2qw8u7xabckr6yfzevdnqnz0bpvriofeyda= Ephsnv1dixzzkfclugbjcq+am6q7wktlxngw3u92jtm5y0rho8s4m4ew2wrj9rmjxqil=bk7px8fb16qau3oictpdneov+0uhgs5tyfzhygavcdklsnzyvj5cpaazds7tlvhjliwdxuebsk6ucon1mfge=03gr94fw2zqqrmb8i+ Xhknptyo "Func main () {var src =" 1230987654 "DST: = Xlib. Xencode ([]byte (SRC), []byte (k)) DST = Xlib. Base64Encode (DST) fmt. Println (String (DST)) DST = Xlib. Base64decode (DST) DST = Xlib. Xdecode (DST, []byte (k)) fmt. Println (String (DST))}


Output Result:

0qytyhqm5cgcxqgqqqqyhqhiq

1230987654


Here is the Java implementation:

Package Xlib;import Java.util.random;public class Xencode {private Random R = new Random ();p rivate String tb_str = "Qf0kit x8gqwhfopghabnmcamebtjnwjpyizdyueoruv21=xclrzsd9klv+s76543 ";p rivate byte[] TB;p rivate byte[] m = new byte[256];p ublic Xencode () {//system.out.println (Tb.length ()), TB = new byte[64];char[] tmp = Tb_str.tochararray (); for (int i = 0; i < 6  4; i + +) tb[i] = (byte) tmp[i];for (int i = 0; i <; i++) M[i] = 0;for (int i = 0; i <; i + +) {M[tb[i]] = (byte) I;}} public int randintn (int l) {return r.nextint (l);} Public char[] Bytetochar (byte[] buf) {char[] dst = new Char[buf.length];for (int i = 0; i < buf.length; i++) Dst[i] = (char) Buf[i];return DST;} Public byte[] Cutbuffer (byte[] buf, int pos, int l) {byte[] tmp = new Byte[l];for (int i = pos; i < POS + L; i++) Tmp[i] = Buf[i];return tmp;}  Public byte[] DEC4 (byte W, byte x, byte y, byte z) {byte[] r_buf = new byte[3];//system.out.printf ("%c\n", W); r_buf[0] = (byte) ((M[w] << 2) &Amp 0xFF) | ((M[x] >> 4) & 0xFF)); R_BUF[1] = (byte) ((M[x] << 4) & 0xFF | (M[y] >> 2) & 0xFF); r_buf[2] = (byte) ((M[y] << 6) & 0xFF | (M[z]) & 0xFF);//p (r_buf[0]);//for (int i = 0; i< 3;i + +) r_buf[i] = M[r_buf[i]];return r_buf;}  Public byte[] ENC3 (byte x, byte y, byte z) {byte[] r_buf = new Byte[4];r_buf[0] = (byte) (x >> 2); r_buf[1] = (byte) ((Byte) ((X & 0x03) << 4) | (byte) (y >> 4)); R_BUF[2] = (byte) (y << 2& 0x3F) | (z >> 6)); R_BUF[3] = (byte) (Z & 0x3F),//p ((byte) (y)), for (int i = 0; i < 4; i++) R_buf[i] = tb[r_buf[i]];//fo R (int i = 0; i< 4; i++) System.out.printf ("%c", (char) r_buf[i]); return r_buf;} public boolean setbit (byte[] B, int pos, byte bit) {if (bit! = 0 && bit! = 1) {return false;} int p = pos/8;int k = pos% 8;if (bit = = 1) {b[p] |= 1 << k;} if (bit = = 0) {B[p] &= ~ (1 << k);} return true;} Public byte getbit (byte[] B , int pos) {byte r = -1;int p = pos/8; int k = pos% 8; if (P >= b.length) Return-1;r = (byte) (B[p] >> K&A mp 1); return r;} Public byte[] XEncode8 (byte[] b, byte[] key, byte[] h) {byte[] r_buf = new Byte[8]; if (b.length! = 8) return null;for (int i = up; i < key.length; i + = 64) {for (int j = 0; J <; J + +) {byte R = getbit (b, h[key[i+j]]);//system.out.printf ("%d \ n", R); Setbit (R_buf, J, R);} for (int j = 0; J <8; j + +) B[j] = R_buf[j];} for (int i = 0; i < r_buf.length; i++) System.out.printf ("%d", R_buf[i]); return r_buf;} Public byte[] XDecode8 (byte[] b, byte[] key, byte[] h) {byte[] r_buf = new Byte[8];if (b.length! = 8) return null;int[]  K = new Int[key.length/64-1];for (int i = 0; i < k.length; i + +) k[i] = (i + 1) * 64;FOR (int i = K.length-1 ; I >= 0; I--) {for (int j = 0; J <; J + +) {byte R = getbit (b, J); Setbit (R_buf, H[key[k[i]+j]], r);} for (int j = 0; J < 8; j + +) B[j] = R_buf[j];} ReturnR_buf;} Public byte[] Encode (byte[] b, byte[] key) {//system.out.printf ("%c\n", key[1]); byte[] r_buf = null;byte[] tmp = Null;by te[] h = new Byte[256];if (b.length = = 0) return null;int k = b.length% 8;if (k! = 0) {tmp = new Byte[b.length + (8- k)];} else {tmp = new byte[b.length];} for (int i =0; i <, i + +) H[key[i]] = (byte) i;for (int i = 0; i < b.length; i++) Tmp[i] = B[i];r_buf = new Byte[tmp.length + 1];r_buf[0] = Key[k];int p = 1; for (int i = 0; i < tmp.length; i + = 8) {byte[] tt = new BYTE[8];FO R (Int j = 0; J < 8; j + +) Tt[j] = Tmp[i + j];byte[] r = XEncode8 (TT, Key, h); for (int j = 0; J < 8; J + +) r _buf[p++] = r[j];} for (int i = 0; i < r_buf.length; i++) System.out.printf ("%d", R_buf[i]); return r_buf;}  Public byte[] Decode (byte[] b, byte[] key) {byte[] r_buf = null;if ((b.length-1)% 8! = 0) return null;byte[] h = new Byte[256];r_buf = new Byte[b.length-1];for (int i = 0; i < p; i + +) H[key[i]] = (byte) i;int = 0; for (int i = 1; i < b.length; i + = 8) {byte[] tt = new Byte[8];for (int j = 0; J < 8; j + +) Tt[j] = B[i+j];byt E[] r = XDecode8 (TT, Key, h); for (int j = 0; J < 8; j + +) r_buf[p++] = R[j];} return r_buf;} Public byte[] Base64Encode (byte[] b) {int L = b.length;int k = l% 3; byte[] r_buf = null;byte[] tmp = Null;switch (k) {CA Se 1:tmp = new Byte[l + 2];tmp[l] = 0; tmp[l+1] = 0;break;case 2:tmp = new Byte[l+1];tmp[l] = 0;break;default:tmp = new B Yte[l];break;} for (int i = 0; i < l; i++) tmp[i] = B[i];r_buf = new BYTE[TMP.LENGTH/3 * 4 + 1];//system.out.println (tmp.length);  R_buf[0] = Tb[k];int p = 1;for (int i = 0; i < TMP.LENGTH/3; i + +) {byte[] tt = ENC3 (Tmp[i * 3], tmp[i*3 + 1], tmp[i* 3 + 2]);//system.out.printf ("%s\n", String.valueof (Bytetochar (TT, 4)); /system.out.printf ("%c\n", tt[3]); for (int j = 0; J < 4; j + +) r_buf[p++] = Tt[j]; }//system.out.printf ("%s\n", String.valueof (Bytetochar (R_BUF))); return r_buf;} Public byte[] Base64decode (bYte[] b) {byte[] r_buf = null;if ((b.length-1)% 4 = 0) return null;r_buf = new byte[(b.length-1)/4 * 3];int K = M[b[0]];int p = 0; for (int i = 0; i < (b.length-1)/4; i + +) {byte[] tmp = this. DEC4 (b[1 + i * 4], b[1 + i * 4 + 1], b[1 + i * 4 + 2], b[1 + i * 4 + 3]); for (int j = 0; J < 3; j + +) r_buf[p++] = Tmp[j];} int L = R_BUF.LENGTH;//SYSTEM.OUT.PRINTLN (l); switch (k) {case 0:break;case 1:r_buf = Cutbuffer (r_buf, 0, l-2); Break;ca Se 2:r_buf = cutbuffer (r_buf, 0, L-1); return r_buf;} Public String CreateList () {String r_str = ' "; char[] tmp = Tb_str.tochararray (); for (int i = 0; i < 1000; i++) {int a = RANDINTN (+), int b = RANDINTN (+), if (a = = b) Continue;tmp[a] ^= tmp[b];tmp[b] ^= tmp[a];tmp[a] ^= tmp[b]; }for (int i = 0; i <; i + +) {r_str + = Tmp[i];} return R_STR;} Public String CreateKey () {String r_str = ' "; for (int i = 0; I < 32; i++) {r_str + = CreateList ();} return R_STR;} public void P (byte y) {for (int i = 7; I; = 0; i--) {System.out.printf ("%d", (char) y >> i&1);} System.out.println ();}}

Java Usage Examples:

Import Xlib. Xencode;public class Main {public static String key = "4xwefdubcsklwrugaon+9r23hoxgjcnmi= faqzqdy0vky8mise6tvltp75zhpb1jnfipmduenpwkw70j+9xzabr1a=5omz4tyqrfc28ih3xlqvgh6bgodskseylujcvt1p+ k9rgnk7i4vdwd2wuslbiehcg=jfshuyaa8f6eblxqoo3jnpzztrqtymcvmx50s0lntpvnmwq4mrlzpdjak5oguz8xwkbrt3c92yviy= gjq61eosiudbfxf+ha7echtwzdrb6h8d1wljuzjyb7qux0ksmnprlit5xapfqmc2goysa+vc9vehf34ni=kogeotws+ k6p80g7mlert1df5xaqnkviq=gswvfe4cmz2habbjn9ourpjuylydz3ihxcu51k0hiy4nxecefdfcytqbdjabz67mtgkrwmlswpri2o8va+ oqjlpvzhn=x93sguw6b7anvwlx=1qlt2nihatxd8r+3iyqzdv0ojogpgkkcm5hfu94ysrbpfsmcujeze8monq7w=zf4rrpevbtcws2+ qsgexm0ytjcd5illufk9ob63jhhadpxyuvzngkia1iqmjsnd5glihk4bsayayfjpzur9w326h=oot+ zrkvenvptcxqbfc0ldg1u8mxw7efs7xbic6t3ezoprfuhba42ajmygjlr5+8x=ulkqd1m9opgtnicvhzkdswnwyq0vefdbk+ gx1hocovnqvuz0mluhn7j5ltcka=zwmss3p4e9tw6iapygjfxdbyr2rq8iexngha70amg8k3bsirrpt9huv= woonxdvqwe4cebpjlqlud256styzcfziykj1+mfvvo2ldixs68knhthk9q5ewarnp4ti=l+ cmygorymfjz13squpb0d7uxjzwecgfbaisfeeyhycu7mrqp6wdwiczbknuzk5hl2gd+0v9f1n8=4axqbo3moagprjjxlvtstnvkbr8ndrehv67lxbgp0uyzqlwpsesqi14jc2tdxuao9jmmwczh5=+ Gtka3ffyio2jc3r7u0raxecpofxamt1sdow9fkzbgi5vqpye4+ud8hybil= vnjwktzmgs6lhnqsoiplyu9cgj6fd3xmnrjyswbdivbz4cz7qmv8p=50kn1etx+f2htuowarhkelaqgho4d1w2f+ tip3ebmihfjxzvq0r5a7xklao8uv6yudbwcmlzt9sgeynpnqrkcg=js1r26tlippx9=azgewqjfxkw7mdv8zigs+ 3rkyqjncbu0bfloshydavcueh45montbqupt493dknq5uywiytbv0vajoswpecxdgj=nl7e2fo18zfscr+ Zri6mlhhmxakglbzhq9l06rfrjabh5tz47xui+gokdcsnp8=dmejxamtqpy2vifogskunw3wcye1vg9x+f5ghrik4zo2= y16achrn7vlbepdqwzeayqbmilmudsn3votpx8wuj0ctjfsk0+djex6= gyag5ifvz3bvliukstw7cd8atpxp4je9kmzmyonhh2burnqqfc1olwrszqm1lghevfa32synp7ci6xe9njdoyvwrtdmhu8pxwjzfb= ugbkr0tc4sialq5+koc=ashxyobz3pc8fnk7w0iwdmytqgpl4vr9zg6uj5+haeslrobqx1dmun2tjvkeiffseupb= 5t9cxd2ujytwcmdnr6nhvfgyxg01voi3q4sbarjoll78zikazekw+pmqhvpec09bjx=6hkolismf8pumwkntysqgadi7z4yoberdhv51gna+ Xqt3cu2zjwflruy43jhtls+hmvw1rlgim=9ci5xjtgesobfoadpkp2qw8u7xabckr6yfzevdnqnz0bpvriofeyda= Ephsnv1dixzzkfclugbjcq+am6q7wktlxngw3u92jtm5y0rho8s4m4ew2wrj9rmjxqil=bk7px8fb16qau3oictpdneov+0uhgs5tyfzhygavcdklsnzyvj5cpaazds7tlvhjliwdxuebsk6ucon1mfge=03gr94fw2zqqrmb8i+ Xhknptyo ";p ublic static void Main (string[] args) {Xencode x = new Xencode (); string ss = "0qytyhqm5cgcxqgqqqqyhqhiq";//This is the string above go encrypted char[] tmp = Ss.tochararray (); byte[] buf = new Byte[tmp.length];  for (int i = 0; i < tmp.length; i++) buf[i] = (byte) tmp[i];byte[] b = X.base64decode (BUF);//byte[] C = X.encode (buf, Key.getbytes ()); byte[] D = x.decode (b, key.getbytes ()); System.out.println (X.bytetochar (d));}}


Output Result:

1230987654

The value of key must be the same as the K variable in the Go language in order to solve the character that goes through the go encryption.

Wait until the winter vacation and then JS and PHP code to write well.

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.