An XOR encryption algorithm in Golang, Python, used to encrypt a string.

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

In the go language, there are symmetric or asymmetric cryptographic functions, but it's a bit of a heavyweight and a bit cumbersome to use.

General game design, certainly do not have to go with those encryption library, because the processing speed is a bit slow, the server can't keep up with the speed, the client can not keep up with speed.

In Delphi and easy language I often use this algorithm encryption, the algorithm is online copy:

Xorkey can customize the changes themselves.

Delphi Code:

Const   xorkey:array[0..7] of Byte = ($B 2, $09, $BB, $55, $93, $6d, $44, $47);//String encryption with function ENC (str:string): Strin G Character cryptographic functions  This is a difference or a cryptographic var   i, J:integer;begin   Result: = ';   J: = 0;   For I: = 1 to Length (STR) does   begin      Result: = result + Inttohex (Byte (Str[i]) xor Xorkey[j], 2);      J: = (j + 1) MoD 8;   End;end;             function Dec (str:string): string; Character decryption function var   i, J:integer;begin   Result: = ';   J: = 0;   For I: = 1 to Length (str) div 2 do   begin      Result: = result + Char (strtoint (' $ ' + Copy (Str, I * 2-1, 2)) XOR XOR KEY[J]);      J: = (j + 1) MoD 8;   End;end;

After rewriting with the go language, this is the case. You can optimize the processing yourself.

var Xorkey []byte = []byte{0xb2, 0x09, 0xBB, 0x55, 0x93, 0x6d, 0x44, 0x47}type Xor struct {}type m interface {   ENC (src String) string   dec (src string) string}func (a *xor) enc (src String) string {   var result string   J: = 0   S: = ""   bt: = []rune (src) for   i: = 0; I < Len (BT); i++ {      s = StrConv. Formatint (Int64 (Byte (bt[i)) ^xorkey[j])      If Len (s) = = 1 {         s = "0" + s      }      result = result + (s)      J = (j + 1)% 8   }   return Result}func (a *xor) Dec (src String) string {   var result string   var s int64
  j: = 0   bt: = []rune (src)   fmt. Println (BT)   for I: = 0; i < len (src)/2; i++ {      S, _ = StrConv. parseint (String (bt[i*2:i*2+2), 0)      result = result + string (byte (s) ^xorkey[j])      j = (j + 1)% 8   }   R Eturn Result}func Main () {   xor: = xor{}   fmt. Println (Xor.enc ("123FSGDG0FD"))   FMT. Println (Xor.dec ("833b8833e00a2020826fdf")}

In Python, you can write like this.

 class Xor:xorkey=[0xb2, 0x09, 0xBB, 0x55, 0x93, 0x6d, 0x44, 0x47] def __init__ (self): pass @classmet           Hod def ENC (self,src): J,result=0, "" Bt=bytes (SRC, ' ASCII ') H=len (BT) for I in Range (h): Result=result+hex (bt[i]^ (self).        XORKEY[J])) [2:] j= (j+1)%8 return result @classmethod def Dec (self,src): J,result,h=0, "", 0 H=len (SRC) for I in Range (0,h,2): Result=result + chr (int (src[i:i+2],16) ^self. XORKEY[J]) j= (j+1)%8 return Resulta=xor () print (A.enc ("123FSGDG0FD")) Print (A.dec ("833B8833E00A2020826FDF") )
Related Article

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.