golang|python簡單與運算實現的對稱式加密演算法

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
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;

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.