2021-11-17 01:44:15 +00:00
|
|
|
package xorencrypt
|
|
|
|
|
|
|
|
import "encoding/base64"
|
|
|
|
|
2021-11-17 01:50:25 +00:00
|
|
|
var DEFAULT_KEY = "kumoly.io/lib/xorencrypt"
|
|
|
|
|
2021-11-17 01:44:15 +00:00
|
|
|
func Decrypt(msg, key string) (string, error) {
|
2021-11-17 01:50:25 +00:00
|
|
|
if key == "" {
|
|
|
|
key = DEFAULT_KEY
|
|
|
|
}
|
2021-11-17 01:44:15 +00:00
|
|
|
deb64, err := base64.StdEncoding.DecodeString(msg)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
dec := xor(deb64, []byte(key))
|
|
|
|
return string(dec), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Encrypt(msg, key string) string {
|
2021-11-17 01:50:25 +00:00
|
|
|
if key == "" {
|
|
|
|
key = DEFAULT_KEY
|
|
|
|
}
|
2021-11-17 01:44:15 +00:00
|
|
|
enc := xor([]byte(msg), []byte(key))
|
|
|
|
return base64.StdEncoding.EncodeToString(enc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func xor(msg, key []byte) []byte {
|
|
|
|
ret := make([]byte, len(msg))
|
|
|
|
for i := 0; i < len(msg); i++ {
|
|
|
|
ret[i] = msg[i] ^ key[i%len(key)]
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|