summaryrefslogtreecommitdiffstats
path: root/challenge/c24.go
diff options
context:
space:
mode:
authorsiddharth <s@ricketyspace.net>2021-09-04 16:59:58 -0400
committersiddharth <s@ricketyspace.net>2021-09-04 17:00:23 -0400
commit880658d348c45655170564aab6787bd3fa2dad2e (patch)
tree29d2fba15c0f91b46f53aaacd59aa3677ffc23e6 /challenge/c24.go
parent823471e97d03d0f2c020c344e941668ea7e7769d (diff)
challenge: do challenge 24, part 1
Diffstat (limited to 'challenge/c24.go')
-rw-r--r--challenge/c24.go50
1 files changed, 50 insertions, 0 deletions
diff --git a/challenge/c24.go b/challenge/c24.go
new file mode 100644
index 0000000..4658557
--- /dev/null
+++ b/challenge/c24.go
@@ -0,0 +1,50 @@
+// Copyright © 2021 rsiddharth <s@ricketyspace.net>
+// SPDX-License-Identifier: ISC
+
+package challenge
+
+import (
+ "fmt"
+
+ "ricketyspace.net/cryptopals/lib"
+)
+
+func C24() {
+ // Part I: Crack MT19937 16-bit seed.
+ crack := func(stream []byte) []byte {
+ i, j := byte(0), byte(0)
+ for i <= 255 {
+ gs, m := lib.MTXORStream(stream, []byte{i, j}), true
+ for k := len(gs) - 1; k >= len(gs)-14; k-- {
+ if gs[k] != 'A' {
+ m = false
+ }
+ }
+ if m {
+ return []byte{i, j}
+ }
+ j += 1
+ if j == 0 {
+ i += 1
+ }
+ }
+ return []byte{}
+ }
+ seed, err := lib.RandomBytes(2) // Generate random seed.
+ if err != nil {
+ panic(err)
+ }
+ plain := append(
+ lib.RandomBytesWithLengthBetween(8, 64),
+ lib.StrToBytes("AAAAAAAAAAAAAA")..., // 14 'A's.
+ ) // Plaintext; last 14 characters known.
+ cipher := lib.MTXORStream(plain, seed) // Encrypt plaintext.
+ cseed := crack(cipher) // Try to crack seed
+ if lib.BytesEqual(cseed, seed) {
+ panic(fmt.Errorf("Unable to crack 16-bit seed %v != %v\n", cseed, seed))
+ }
+ fmt.Printf("Cracked 16-bit seed %v == %v\n", cseed, seed)
+}
+
+// Output:
+// Cracked 16-bit seed [74 8] == [74 8]