package main
import (
"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 []int
for index, value := range dataArr {
base := int(value)
dataString := base + int(0xFF&keyArr[index%keyLen])
tmpList = append(tmpList, dataString)
}
var str string
for _, 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 []int
for index, value := range dataArr {
base, _ := strconv.Atoi(value)
dataString := base - int(0xFF&keyArr[index%keyLen])
tmpList = append(tmpList, dataString)
}
var str string
for _, 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;
有疑问加站长微信联系(非本文作者)