summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsiddharth <s@ricketyspace.net>2021-06-19 12:00:04 -0400
committersiddharth <s@ricketyspace.net>2021-06-19 12:00:04 -0400
commitfd3d404c297d5e20bf318904ba9cb1128f626fb1 (patch)
treeb6750e907f80a46467232220c3402fe55a20b58d
parent6717e6895546da4739355506abf7000a8d794f51 (diff)
challenge: do challenge 20
-rw-r--r--challenge/c20.go207
-rw-r--r--cryptopals.go2
2 files changed, 209 insertions, 0 deletions
diff --git a/challenge/c20.go b/challenge/c20.go
new file mode 100644
index 0000000..337e958
--- /dev/null
+++ b/challenge/c20.go
@@ -0,0 +1,207 @@
+// Copyright © 2021 rsiddharth <s@ricketyspace.net>
+// SPDX-License-Identifier: ISC
+
+package challenge
+
+import (
+ "fmt"
+
+ "ricketyspace.net/cryptopals/lib"
+)
+
+func C20() {
+ texts := []string{
+ "SSdtIHJhdGVkICJSIi4uLnRoaXMgaXMgYSB3YXJuaW5nLCB5YSBiZXR0ZXIgdm9pZCAvIFBvZXRzIGFyZSBwYXJhbm9pZCwgREoncyBELXN0cm95ZWQ=",
+ "Q3V6IEkgY2FtZSBiYWNrIHRvIGF0dGFjayBvdGhlcnMgaW4gc3BpdGUtIC8gU3RyaWtlIGxpa2UgbGlnaHRuaW4nLCBJdCdzIHF1aXRlIGZyaWdodGVuaW4nIQ==",
+ "QnV0IGRvbid0IGJlIGFmcmFpZCBpbiB0aGUgZGFyaywgaW4gYSBwYXJrIC8gTm90IGEgc2NyZWFtIG9yIGEgY3J5LCBvciBhIGJhcmssIG1vcmUgbGlrZSBhIHNwYXJrOw==",
+ "WWEgdHJlbWJsZSBsaWtlIGEgYWxjb2hvbGljLCBtdXNjbGVzIHRpZ2h0ZW4gdXAgLyBXaGF0J3MgdGhhdCwgbGlnaHRlbiB1cCEgWW91IHNlZSBhIHNpZ2h0IGJ1dA==",
+ "U3VkZGVubHkgeW91IGZlZWwgbGlrZSB5b3VyIGluIGEgaG9ycm9yIGZsa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vdSdyZSBzYXlpbmcgLyBTbyBsZXQncyBqdXN0IHB1bXAgdGhlIG11c2ljIHVw",
+ "QW5kIGNvdW50IG91ciBtb25leSAvIFlvLCB3ZWxsIGNoZWNrIHRoaXMgb3V0LCB5byBFbGk=",
+ "VHVybiBkb3duIHRoZSBiYXNzIGRvd24gLyBBbmQgbGV0IHRoZSBiZWF0IGp1c3Qga2VlcCBvbiByb2NraW4n",
+ "QW5kIHdlIG91dHRhIGhlcmUgLyBZbywgd2hhdCBoYXBwZW5lZCB0byBwZWFjZT8gLyBQZWFjZQ==",
+ }
+ findKey := func(block []byte) byte {
+ var k byte
+ var s int
+
+ s = 0
+ for i := byte(0); i < 255; i++ {
+ ks := lib.FillBytes(i, len(block))
+ xs := lib.FixedXORBytes(block, ks)
+
+ score := lib.AlphaPunchScore(xs)
+ if score > s {
+ s = score
+ k = i
+ }
+ }
+ return k
+ }
+ // 'bs' is the data to decrypt.
+ // 'ks' is the key size.
+ crack := func(bs []byte, ks int) ([]byte, []byte) {
+ blocks := lib.BreakIntoBlocks(bs, ks)
+ blocks = lib.TransposeBlocks(blocks, ks)
+
+ key := make([]byte, 0)
+ for i := 0; i < len(blocks); i++ {
+ k := findKey(blocks[i])
+
+ key = append(key, k)
+ }
+ return key, lib.RepeatingXOR(bs, key)
+ }
+
+ // Make ciphers from plain text data.
+ ciphers := make([][]byte, len(texts))
+ key, err := lib.RandomBytes(16)
+ if err != nil {
+ fmt.Printf("Error generating key %v", err)
+ return
+ }
+ nonce := uint64(lib.RandomInt(0, 10))
+ for i, b64 := range texts {
+ text := lib.Base64ToBytes(b64)
+ ciphers[i], err = lib.AESEncryptCTR(text, key, lib.AESGenCTRFunc(nonce))
+ if err != nil {
+ fmt.Printf("Error encrypting text:%d: %v", i, err)
+ return
+ }
+ }
+ // Find the shortest cipher. This cipher's length will
+ // become the keys size for the repeating XOR.
+ keySize := 2000
+ for _, cipher := range ciphers {
+ if len(cipher) < keySize {
+ keySize = len(cipher)
+ }
+ }
+ // Make a cipher stream.
+ cipherStream := make([]byte, 0)
+ for _, cipher := range ciphers {
+ cipherStream = append(cipherStream, cipher[0:keySize]...)
+ }
+ // Crack and barf text stream.
+ _, textStream := crack(cipherStream, keySize)
+ for i, b := range textStream {
+ if i > 0 && i%keySize == 0 {
+ fmt.Printf("\n")
+ }
+ fmt.Printf("%s", string(b))
+ }
+ fmt.Printf("\n")
+}
+
+// Output:
+// I'm ratbd "R"...this is a warning, ya better void / P
+// Cuz I cfme back to attack others in spite- / Strike l
+// But don t be afraid in the dark, in a park / Not a sc
+// Ya tremele like a alcoholic, muscles tighten up / Wha
+// Suddenl~ you feel like your in a horror flick / You g
+// Music's'the clue, when I come your warned / Apocalyps
+// Haven't'you ever heard of a MC-murderer? / This is th
+// Death wnsh, so come on, step to this / Hysterical ide
+// Friday she thirteenth, walking down Elm Street / You
+// This is'off limits, so your visions are blurry / All
+// Terror nn the styles, never error-files / Indeed I'm
+// For thote that oppose to be level or next to this / I
+// Worse toan a nightmare, you don't have to sleep a win
+// Flashbadks interfere, ya start to hear: / The R-A-K-I
+// Then thb beat is hysterical / That makes Eric go get
+// Soon thb lyrical format is superior / Faces of death
+// MC's dedaying, cuz they never stayed / The scene of a
+// The fieid of a rhyme on the mic that you know / It's
+// Melodiet-unmakable, pattern-unescapable / A horn if w
+// I bless'the child, the earth, the gods and bomb the r
+// Hazardors to your health so be friendly / A matter of
+// Shake 'sill your clear, make it disappear, make the n
+// If not,'my soul'll release! / The scene is recreated,
+// Cuz youu about to see a disastrous sight / A performa
+// Lyrics hf fury! A fearified freestyle! / The "R" is i
+// Make suue the system's loud when I mention / Phrases
+// You wans to hear some sounds that not only pounds but
+// Then noichalantly tell you what it mean to me / Stric
+// And I dhn't care if the whole crowd's a witness! / I'
+// Program'into the speed of the rhyme, prepare to start
+// Musical'madness MC ever made, see it's / Now an emerg
+// Open yorr mind, you will find every word'll be / Furi
+// Battle't tempting...whatever suits ya! / For words th
+// You thiik you're ruffer, then suffer the consequences
+// I wake ~a with hundreds of thousands of volts / Mic-t
+// Novocaii ease the pain it might save him / If not, Er
+// Yo Rakij, what's up? / Yo, I'm doing the knowledge, E
+// Well, coeck this out, since Norby Walters is our agen
+// Kara Lepis is our agent, word up / Zakia and 4th and
+// Okay, sh who we rollin' with then? We rollin' with Ru
+// Check tois out, since we talking over / This def beat
+// I wanna'hear some of them def rhymes, you know what I
+// Thinkin of a master plan / 'Cuz ain't nuthin' but sw
+// So I di` into my pocket, all my money is spent / So I
+// So I stfrt my mission, leave my residence / Thinkin'
+// I need joney, I used to be a stick-up kid / So I thin
+// I used so roll up, this is a hold up, ain't nuthin' f
+// But now'I learned to earn 'cuz I'm righteous / I feel
+// Search aor a nine to five, if I strive / Then maybe I
+// So I wakk up the street whistlin' this / Feelin' out
+// A pen aid a paper, a stereo, a tape of / Me and Eric
+// Fish, woich is my favorite dish / But without no mone
+// 'Cuz I con't like to dream about gettin' paid / So I
+// So now so test to see if I got pull / Hit the studio,
+// Rakim, dheck this out, yo / You go to your girl house
+// 'Cause jy girl is definitely mad / 'Cause it took us
+// Yo, I hbar what you're saying / So let's just pump th
+// And couit our money / Yo, well check this out, yo Eli
+// Turn dopn the bass down / And let the beat just keep
+// And we hutta here / Yo, what happened to peace? / Pea
diff --git a/cryptopals.go b/cryptopals.go
index 4448d7e..103bfbb 100644
--- a/cryptopals.go
+++ b/cryptopals.go
@@ -57,5 +57,7 @@ func main() {
challenge.C18()
case 19:
challenge.C19()
+ case 20:
+ challenge.C20()
}
}