Created
October 29, 2018 19:33
-
-
Save jamesharr/c4bfbf5fa80716cd249438f81a1547bb to your computer and use it in GitHub Desktop.
weekly quiz
This file contains hidden or 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
$ time go run random-quiz-crypto.go | |
decryptKey 239333 | |
decrypt 375289 -> 230801 | |
decrypt 485994 -> 200009 | |
decrypt 047367 -> 190020 | |
decrypt 543573 -> 051400 | |
decrypt 785337 -> 131504 | |
decrypt 477107 -> 002023 | |
decrypt 727268 -> 150000 | |
"WHAT IS TEN MOD TWO " | |
real 0m0.547s | |
user 0m0.499s | |
sys 0m0.219s |
This file contains hidden or 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 ( | |
"fmt" | |
"math/big" | |
) | |
func bruteTwoKey(encKey, mod *big.Int) *big.Int { | |
rv := big.NewInt(0) | |
two := big.NewInt(1) | |
two.SetInt64(2) | |
// Test decrypt text | |
pText := big.NewInt(12345) | |
cText := big.NewInt(0) | |
cText.Exp(pText, encKey, mod) | |
for decK := big.NewInt(1); true; decK = decK.Add(decK, two) { | |
res := big.NewInt(0) | |
res = res.Exp(cText, decK, mod) | |
if res.Cmp(pText) == 0 { | |
rv = decK | |
break | |
} | |
} | |
return rv | |
} | |
func main() { | |
// Quiz parameters | |
mod := big.NewInt(799567) | |
encKey := big.NewInt(5) | |
cipherText := []*big.Int{ | |
big.NewInt(375289), | |
big.NewInt(485994), | |
big.NewInt(47367), // ignore leading zero, it turns this into octal | |
big.NewInt(543573), | |
big.NewInt(785337), | |
big.NewInt(477107), | |
big.NewInt(727268), | |
} | |
// Brute Force key | |
decryptKey := bruteTwoKey(encKey, mod) | |
fmt.Printf("decryptKey %s\n", decryptKey) | |
// Decrypt cipherText | |
plainText := []*big.Int{} | |
for _, v := range cipherText { | |
tmp := big.NewInt(0) | |
tmp.Exp(v, decryptKey, mod) | |
plainText = append(plainText, tmp) | |
fmt.Printf("decrypt %06s -> %06s\n", v, tmp) | |
} | |
// Decode to ASCII | |
humanText := []byte{} | |
for _, v := range plainText { | |
triplet := v.Int64() | |
a := byte(triplet/10000) + 'A' - 1 | |
b := byte((triplet/100)%100) + 'A' - 1 | |
c := byte(triplet%100) + 'A' - 1 | |
humanText = append(humanText, a, b, c) | |
} | |
// Convert A-1 to space, and any unknowns to '?' | |
for i, v := range humanText { | |
if v == 'A'-1 { | |
humanText[i] = ' ' | |
} else if v < 'A' || v > 'Z' { | |
humanText[i] = '?' | |
} | |
} | |
// Display | |
fmt.Printf("%q\n", string(humanText[:])) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment