-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
xor.go
29 lines (27 loc) · 1.05 KB
/
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
26
27
28
29
// Package xor is an encryption algorithm that operates the exclusive disjunction(XOR)
// description: XOR encryption
// details: The XOR encryption is an algorithm that operates the exclusive disjunction(XOR) on each character of the plaintext with a given key
// time complexity: O(n)
// space complexity: O(n)
// 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
}