/
feistel.go
37 lines (35 loc) · 933 Bytes
/
feistel.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
30
31
32
33
34
35
36
37
package feistel
// Encrypt takes a left and right uint32 and encrypts it using the Feistel cipher,
// it returns the encrypted left and right parts
func Encrypt(left, right uint32, rounds int, keys []uint32) (uint32, uint32) {
size := len(keys)
for i := 0; i < rounds; i++ {
left1 := (left ^ keys[i%size]) ^ right
right1 := left
if i == (rounds - 1) {
left = right1
right = left1
} else {
left = left1
right = right1
}
}
return left, right
}
// Decrypt takes a left and right uint32 and decrypts it using the Feistel cipher,
// it returns the decrypted left and right parts
func Decrypt(left, right uint32, rounds int, keys []uint32) (uint32, uint32) {
size := len(keys)
for i := 0; i < rounds; i++ {
left1 := (left ^ keys[(rounds-i-1)%size]) ^ right
right1 := left
if i == (rounds - 1) {
left = right1
right = left1
} else {
left = left1
right = right1
}
}
return left, right
}