Last active
November 15, 2019 08:26
-
-
Save ergofriend/7f2a056a0f431fb90bff214232683324 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"os" | |
"sync" | |
) | |
var alphabet [32]string | |
func ini() { | |
for index := 0; index < 32; index++ { | |
num := 'a' + rune(index) | |
if 122 < num { | |
num = 'A' + rune(num-122-1) | |
} | |
alphabet[index] = string(num) | |
} | |
// for index := 0; index < 32; index++ { | |
// print(alphabet[index]) | |
// } | |
} | |
func toNum(str rune) int { | |
return int(str - 'a') | |
} | |
type Key struct{ content int } | |
func (key *Key) nextKey() { | |
r := key.content + 1 | |
switch { | |
case 32 <= r: | |
key.content = r - 32 | |
default: | |
key.content = r | |
} | |
} | |
func xor(char int, key int) int { | |
return char ^ key | |
} | |
func decrypt(text string, indexKey int) string { | |
key := Key{indexKey} | |
var result string | |
for _, r := range text { | |
index := xor(toNum(r), key.content) | |
result += alphabet[index] | |
key.nextKey() | |
} | |
println(result, ":", key.content) | |
return result | |
} | |
// go run Vernam.go eaonj | |
func main() { | |
var wg sync.WaitGroup | |
varnamText := os.Args[1] | |
ini() | |
for index := 0; index < 32; index++ { | |
wg.Add(1) | |
go func(index int) { | |
decrypt(varnamText, index) | |
wg.Done() | |
}(index) | |
} | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment