cryptopals/set1/challenge_5.go

34 lines
583 B
Go
Raw Permalink Normal View History

2024-10-29 20:36:04 +02:00
package set1
func XOREncryptText(plaintext string, key string) (cipher []byte) {
cursor := 0
nextKeyChar := func() (char byte) {
char = key[cursor]
cursor++
if cursor >= len(key) {
cursor = 0
}
return
}
cipher = make([]byte, len(plaintext), len(plaintext))
for i := 0; i < len(plaintext); i++ {
cipher[i] = plaintext[i] ^ nextKeyChar()
}
return
}
func ConvertToHex(bytes []byte) (hexBytes []byte) {
for i := 0; i < len(bytes); i++ {
hexBytes = append(hexBytes, HexRepresentations[bytes[i]>>4], HexRepresentations[0b00001111&bytes[i]])
}
return
}