下面的代码主要是看DES加密算法时,发觉能力有限只会一点点,那就让我会的那一点与base64加密用在一起而已。
代码如下:
package xlib import ( "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 % 8 r_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]byte for j := 0; j < 8; j++ { tmp[j] = b[i+j] } res_tmp := XEncode8(tmp, key, m) for j := 0; j < len(res_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 == 64 { 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]byte for j := 0; j < 8; j++ { tmp[j] = b[i+j] } res_tmp := XDecode8(tmp, key, m) for j := 0; j < len(res_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 []byte) (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]]>>4), (m[b[i+1]]<<4)|(m[b[i+2]]>>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_buf []byte) { l := len(b) k := l % 3 r_buf = append(r_buf, tb[k]) switch k { case 2: b = append(b, 0) case 1: b = append(b, 0, 0) } for 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]>>4)]) r_buf = append(r_buf, tb[((b[i+1]<<4)>>2)|(b[i+2]>>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(64)) b := int(RandInt31n(64)) //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 < 32; 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 % 8 p := position / 8 if bit == 0 { b[p] &= ^(1 << k) } else { b[p] |= (1 << k) } return } func GetBit(b []byte, position uint) (r byte) { r = 0 if len(b)*8 < int(position) { r = 0xFF return } k := position % 8 p := position / 8 r |= (b[p] >> k) & 1 return } func XEncode8(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 := 64 key_len := len(key) r_buf = 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 := 64 key_len := len(key) r_buf = make([]byte, 8) k_len := key_len/block_len - 1 k := 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[tb[i]] = i } //fmt.Println(m) }
package main import ( "./xlib" "fmt" ) //由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+CMyGOrYmfjz13squPB0d7UxJZWecgFbAIsfeE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VcdKlSnzYVj5CpAazdS7tlvHJLIWDXUeBsk6ucOn1MfGE=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)) }
输出结果:
0QYTyhqM5cGCXqGQQQQYhQhIQ
1230987654
下面是java 实现:
package xlib; import java.util.Random; public class XEncode { private Random r = new Random(); private String tb_str = "QF0KItX8GqwHfopghAbnmCaMEBTJNWjPYiZdyueOrUV21=xcLRzsD9klv+S76543"; private byte[] tb ; private byte[] m = new byte[256]; public XEncode() { //System.out.println(tb.length()); tb = new byte[64]; char[] tmp = tb_str.toCharArray(); for (int i = 0 ; i < 64 ;i ++ ) tb[i] = (byte)tmp[i]; for (int i = 0 ; i < 256 ; i++ ) m[i] = 0; for (int i = 0 ; i < 64 ;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 ) & 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]]; //for (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& 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 = 64 ; i < key.length ; i += 64 ) { for (int j = 0 ;j < 64 ; 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 < 64 ;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]; } return r_buf; } public byte[] Encode(byte[] b , byte[] key) { //System.out.printf("%c\n" , key[1]); byte[] r_buf = null; byte[] tmp = null; byte[] 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 < 64 ; 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]; for (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 < 64 ; i ++ ) h[key[i]] = (byte)i; int p = 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]; byte[] 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) { case 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 byte[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; case 2: r_buf = CutBuffer(r_buf , 0 , l - 1 ) ; break; } return r_buf; } public String CreateList() { String r_str = ""; char[] tmp = tb_str.toCharArray(); for (int i = 0 ; i < 1000 ; i++ ) { int a = RandIntn(64); int b = RandIntn(64); if (a == b ) continue; tmp[a] ^= tmp[b]; tmp[b] ^= tmp[a]; tmp[a] ^= tmp[b]; } for (int i = 0 ; i < 64 ; 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使用实例:
import xlib.XEncode; public class Main { public static String key = "4XweFDUBcSklWrugAOn+9R23hoxGjCNmI=faqzQdY0vKy8MisE6tVLTP75ZHpb1JNfipMDuenPWKw70J+9xZABR1a=5omz4TyqrFc28IH3XlQvgh6bGOdskSEYLUjCVt1P+K9rGNk7I4VdWD2wUsLbiehcg=JFSHuYAa8f6EBlxqOo3jnpZzTRQtymCvMX50S0lNtPVnMWQ4mrLzpDjAk5oGUZ8XwKbRT3C92YvIy=gJq61EOsiudBFxf+Ha7echtwzDrb6H8d1WljUZJYB7Qux0kSmNpRLiT5XaPFqMC2gOysA+Vc9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skunW3wCYe1vg9x+F5GhrIK4ZO2=y16ACHRN7vlBePDqWzEaYQbmiLMUdSn3VoTpX8wuj0ctJfsk0+djeX6=GyAg5iFVz3BvlIUKsTW7cD8atpxP4JE9kmZMYonhH2burNQqfC1OLwRSzQM1LGhEVFa32sYnP7cI6Xe9NjDoyvwRtdmHU8pxWJZfB=ugbkr0TC4SiAlq5+KOc=AshXyOBz3pC8fNk7w0IWDMYtQGPl4vR9Zg6Uj5+HaeSLrobqx1dmun2TJVKEiFfSEupb=5t9CXd2UJyTwcMDNr6nHvFgYxG01VOI3Q4sBARjoLl78ziKaZekW+PmqhvPec09bjx=6HKolISMF8pUmWkNTYsQGADi7Z4yOBERdhV51gna+Xqt3Cu2zJwfLrUy43JHTLS+hmVW1RlgiM=9cI5XjtGesObfoaDPkp2qw8u7xABCKr6YFzEvdNQnZ0BPvrIoFEyDA=ephSNV1dixzZkfcLUgbJCQ+aM6q7wKtlXnGW3u92jTm5Y0RHO8s4m4eW2wrj9RMJxQiL=Bk7PX8fb16qau3OICTpDNEov+0UHgs5tYFZhyGAVcdKlSnzYVj5CpAazdS7tlvHJLIWDXUeBsk6ucOn1MfGE=03gR94Fw2ZQqrmb8i+xhKNPTyo"; public static void main(String[] args) { XEncode x = new XEncode(); String ss = "0QYTyhqM5cGCXqGQQQQYhQhIQ";//这是上面GO加密后的字符串 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)); } }
1230987654
key 的值要跟Go语言的 k 变量相同,这样才能解经过GO加密的字符。
等到寒假再把js 与php 的代码写好吧。
有疑问加站长微信联系(非本文作者)