這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
package mainimport ("fmt""strconv""strings")func encryptData(code_data string, callback_key string) string {dataArr := []rune(code_data)keyArr := []byte(callback_key)keyLen := len(keyArr)var tmpList []intfor index, value := range dataArr {base := int(value)dataString := base + int(0xFF&keyArr[index%keyLen])tmpList = append(tmpList, dataString)}var str stringfor _, value := range tmpList {str += "@" + fmt.Sprintf("%d", value)}return str}func decryptData(nt_data string, callback_key string) string {strLen := len(nt_data)newData := []rune(nt_data)resultData := string(newData[1:strLen])dataArr := strings.Split(resultData, "@")keyArr := []byte(callback_key)keyLen := len(keyArr)var tmpList []intfor index, value := range dataArr {base, _ := strconv.Atoi(value)dataString := base - int(0xFF&keyArr[index%keyLen])tmpList = append(tmpList, dataString)}var str stringfor _, val := range tmpList {str += string(rune(val))}return str}func main() {testData := "#test %data _code$@這是一個簡單的測試樣本===*******~!@#¥%……&*()——+"testKey := "test! key"encode := encryptData(testData, testKey)fmt.Println(encode)decode := decryptData(encode, testKey)fmt.Println(decode)}
class cryptDataClass:def getByte(self, data):tmp = [];for s in data:tmp.append(ord(s));return tmp;def getChars(self, byte):str = '';for n in byte:str += chr(n);return str;def encryptData(self, data, keys):tmpList = [];dataArr = self.getByte(data);keyArr = self.getByte(keys);keyLen = len(keyArr);for index,value in enumerate(dataArr):tmpList.append(str((0xFF & value) + (0xFF & keyArr[index % keyLen])));return "@" + "@".join(tmpList);def decryptData(self, data, keys):tmpList = [];dataArr = data.split("@");del(dataArr[0]);keyArr = self.getByte(keys);keyLen = len(keyArr);for index,value in enumerate(dataArr):tmpList.append(int(value) - (0xFF & keyArr[index % keyLen]) );return self.getChars(tmpList);data = '#test %data _code$@這是一個簡單的測試樣本===*******~!@#¥%……&*()——+';key = 'test! key';cc = cryptDataClass();endata = cc.encryptData(data, key);print endata;dedata = cc.decryptData(endata, key)print dedata;