2015-10-15 21:30:52 +08:00
|
|
|
package tscipher
|
|
|
|
|
|
|
|
|
|
import ()
|
|
|
|
|
|
|
|
|
|
type XOR struct {
|
|
|
|
|
key []byte
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *XOR) Decrypt(data []byte) (decrypted []byte, err error) {
|
|
|
|
|
decrypted = make([]byte, len(data))
|
|
|
|
|
for i := 0; i < len(data); i++ {
|
2015-10-16 23:23:45 +08:00
|
|
|
decrypted[i] = data[len(data)-i-1] ^ this.key[i%len(this.key)]
|
2015-10-15 21:30:52 +08:00
|
|
|
}
|
|
|
|
|
err = nil
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *XOR) Encrypt(data []byte) (encryped []byte, err error) {
|
|
|
|
|
encryped = make([]byte, len(data))
|
|
|
|
|
for i := 0; i < len(data); i++ {
|
2015-10-16 23:23:45 +08:00
|
|
|
encryped[i] = data[len(data)-i-1] ^ this.key[i%len(this.key)]
|
2015-10-15 21:30:52 +08:00
|
|
|
}
|
|
|
|
|
err = nil
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewXOR(key []byte) (cipher Cipher) {
|
|
|
|
|
return &XOR{
|
|
|
|
|
key: key,
|
|
|
|
|
}
|
|
|
|
|
}
|