func main() { const key = 11 var ( d byte s string = "abc" ds string dsSlice []byte ) //加密 for _, v := range s { d = byte(v ^ key) dsSlice = append(dsSlice, d) fmt.Printf("加密之后byte:%v,字符:%s,原字符为:%s\n",d,string(d),string(v)) } ds = string(dsSlice) fmt.Printf("明文:%s 密文:%s\n",s,ds) // 解密 dsSlice = dsSlice[:0] for _, v := range ds { d = byte(v ^ 11) dsSlice = append(dsSlice, d) fmt.Printf("解密之后byte:%v,字符:%s,原字符为:%s\n",d,string(d),string(v)) } s = string(dsSlice) fmt.Printf("密文:%s 明文:%s\n",ds,s) } //加密之后byte:106,字符:j,原字符为:a //加密之后byte:105,字符:i,原字符为:b //加密之后byte:104,字符:h,原字符为:c // 明文:abc 密文:jih //解密之后byte:97,字符:a,原字符为:j //解密之后byte:98,字符:b,原字符为:i //解密之后byte:99,字符:c,原字符为:h // 密文:jih 明文:abc