Skip to content

Commit a516ad1

Browse files
committed
Create xorCipher.go
A simple implementation of the Xor Cipher
1 parent 5059797 commit a516ad1

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

ciphers/xorCipher.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import "fmt"
4+
func encrypt(key int, plaintext []int)(ciphertext []int){
5+
for _,char:= range plaintext {
6+
ciphertext = append(ciphertext,xor(char,key))
7+
}
8+
return
9+
}
10+
func xor(char int,key int)int{
11+
return (char^key)
12+
}
13+
14+
func decrypt(key int, ciphertext []int)(plaintext []int){
15+
for _,char:= range ciphertext {
16+
plaintext = append(plaintext , xor(char,key))
17+
}
18+
return
19+
}
20+
21+
func decodeToString(slice []int)(str string){
22+
for _,v:= range slice{
23+
str+=string(v)
24+
}
25+
return
26+
}
27+
28+
func main(){
29+
//string = hello world
30+
key :=97
31+
message:=[]int{72,101,108,108,111,32,119,111,114,108,100}
32+
encrypted:=encrypt(key,message)
33+
decrypted:=decrypt(key,encrypted)
34+
plaintext:=decodeToString(decrypted)
35+
fmt.Println(plaintext)
36+
}
37+

0 commit comments

Comments
 (0)