Skip to content

Instantly share code, notes, and snippets.

@payam-int
Created March 31, 2019 07:06
Show Gist options
  • Save payam-int/5e5517446b5999202edc37eda811a917 to your computer and use it in GitHub Desktop.
Save payam-int/5e5517446b5999202edc37eda811a917 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/hex"
)
func main() {
cipherTextHex := "09e1c5f70a65ac519458e7e53f36"
plaintext := "attack at dawn"
newPlainText := "attack at dusk"
p := []byte(plaintext)
c, _ := hex.DecodeString(cipherTextHex)
key := crack(p, c)
p2 := []byte(newPlainText)
c2 := enc(p2, key)
println(hex.EncodeToString(c2))
}
func enc(plaintext []byte, key []byte) []byte {
cipherText := xorBytes([]byte(plaintext), key)
return cipherText
}
func crack(plaintext []byte, cipherText []byte) []byte {
key := xorBytes(plaintext, cipherText)
return key
}
func xorBytes(b1 []byte, b2 []byte) []byte {
r := make([]byte, len(b1))
for i := 0; i < len(b1); i++ {
r[i] = b1[i] ^ b2[i]
}
return r
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment