forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xor.go
25 lines (23 loc) · 845 Bytes
/
xor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Package xor is an encryption algorithm that operates the exclusive disjunction(XOR)
// ref: https://en.wikipedia.org/wiki/XOR_cipher
package xor
// Encrypt encrypts with Xor encryption after converting each character to byte
// The returned value might not be readable because there is no guarantee
// which is within the ASCII range
// If using other type such as string, []int, or some other types,
// add the statements for converting the type to []byte.
func Encrypt(key byte, plaintext []byte) []byte {
cipherText := []byte{}
for _, ch := range plaintext {
cipherText = append(cipherText, key^ch)
}
return cipherText
}
// Decrypt decrypts with Xor encryption
func Decrypt(key byte, cipherText []byte) []byte {
plainText := []byte{}
for _, ch := range cipherText {
plainText = append(plainText, key^ch)
}
return plainText
}