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?