字符串的简单加密

I want to encrypt a string with Go, my actual code is:

package main

import (
    "fmt"
)

const key = "\xbd\xb2\x3d\xbc\x20\xe2\x8c\x98"  //some random numbers here

func Encrypt(input string) (output string) {
  for i := 0; i < len(input); i++ {
    output += fmt.Sprintf("\\x%02x", input[i] ^ key[i % len(key)])
      }
  return output;
} 

func Decrypt(input string) (output string) {
  key := "\xbd\xb2\x3d\xbc\x20\xe2\x8c\x98"
  for i := 0; i < len(input); i++ {
    output += string(input[i] ^ key[i % len(key)])
    }
    return output;
} 


func main() {

   stringa := "password"
   encrypted := Encrypt(stringa)
   fmt.Println(encrypted)

   fmt.Println(Decrypt(encrypted))
   fmt.Println(stringa)
}

\xcd\xd3\x4e\xcf\x57\x8d\xfe\xfc
áE^O|?è«áE      U|?ï_á?|?'üáE[U|?êû
password

Problem is after encrypt string, when I try to decrypt return different output. Where did I go wrong?

It looks like your goal is to xor the the bytes in a string with the bytes in a key. Here's one way to do it:

func xor(input string) string {
    output := make([]byte, len(input))
    for i := 0; i < len(input); i++ {
        output[i] = input[i] ^ key[i%len(key)]
    }
    return string(output)
}

The Encrypt and Decrypt functions are the same:

func Encrypt(input string) string { return xor(input) }
func Decrypt(input string) string { return xor(input) }