Sage Code 6

Assignment 1, question 3

Note that the point in the question was not to see how good are your cipher-cracking skills, neither to test your knowledge in Python/Sage/gibberish.
Just to see that the nice theory can be very different from field experiments' results.

sage: import urllib
sage: C=urllib.urlopen('http://benyehuda.org/frischmann/ilias.html').read().decode('cp1255')
sage: T=urllib.urlopen('http://tau-crypto.wikidot.com/local--files/home-assignments/cipher1.txt').read().decode('cp1255')
sage: print T

ץאסב פישנ ,ףשר דהר
:נןףט ,נמרמ ףס-פמבקר
ץאטז ךפך חכיס אק נק
.נןץכ חץה-פדטנ

!חנהעבך חט פזר ךץנל"
!נזץבו-יפס ,נזש-ןסט
חנהו ,מנזד ץפלץל-צנהט
"!נזנלטר שעסט

:אסך נבפמ ציש קנךנ
— נפץו-ףפסב דק שבס ,ןז
,גאטא יור ןטה ךכנ
.נפץנךץךר מנבד

,דנלך-ךבש ןטנה נןנה
,דנףינן נפזפה ,חנהעבךנ
,ןנזטך ץהפר שנשנ
!ענףזנ ההנר

- !מרך !מרך - אנאס פסטר
!ץךטזר צפמך םדכד
ןאך נץלב שקץ דק יעז
.ץךו נדפר יקנ

?חנהעבך דק ,נדיעף צק"
!סוך פןיפ ,םףרסק
"!קרפ !קרפ !ךךיו-גנפ נך
.סוב נדנןסר

ץאסב פישנ ,ףשר דהר
:נןףט ,נמרמ ףס-פמבקר
ץאטז ךפך חכיס אק נק
.נןץכ חץה-פדטנ

sage: def rev_lines(txt):
...       return u"\n".join(x[::-1] for x in txt.split(u"\n"))
sage: print rev_lines(T)

רהד רשף, נשיפ בסאץ
רקבמפ-סף מרמנ, טףןנ:
קנ קא סיכח ךפך זטאץ
נטדפ-הץח כץןנ.

"לנץך רזפ טח ךבעהנח!
טסן-שזנ, ספי-ובץזנ!
טהנצ-לץלפץ דזנמ, והנח
טסעש רטלנזנ!"

נךנק שיצ מפבנ ךסא:
זן, סבש קד בספף-וץפנ —
נכך הטן רוי אטאג,
דבנמ רךץךנץפנ.

הנןנ הנטן שבך-ךלנד,
נךבעהנח, הפזפנ ןניףנד,
נשנש רפהץ ךטזנן,
רנהה נזףנע!

רטספ סאנא - ךרמ! ךרמ! -
דכדם ךמפצ רזטךץ!
זעי קד ץקש בלץנ ךאן
נקי רפדנ וךץ.

"קצ ףעידנ, קד ךבעהנח?
קסרףם, פיןפ ךוס!
ךנ פנג-ויךך! פרק! פרק!"
רסןנדנ בוס.

רהד רשף, נשיפ בסאץ
רקבמפ-סף מרמנ, טףןנ:
קנ קא סיכח ךפך זטאץ
נטדפ-הץח כץןנ.

sage: aleph, tav = ord(u'א'), ord(u'ת')
sage: alphabet = u"".join(unichr(x) for x in range(aleph,1+tav)) # a unicode string
sage: [C.count(x) for x in alphabet]
[1364, 1008, 249, 461, 1502, 2015, 176, 492, 139, 1890, 151, 487, 1366, 688, 765, 207, 685, 333, 624, 64, 358, 28, 177, 319, 920, 654, 910]
sage: [T.count(x) for x in alphabet]
[12, 15, 2, 16, 16, 8, 13, 9, 17, 13, 26, 6, 6, 2, 12, 14, 54, 18, 7, 11, 26, 20, 4, 16, 23, 12, 0]
sage: order_corpus = sorted(alphabet, key=C.count)
sage: order_cipher = sorted(alphabet, key=T.count)
sage: for x,y in zip(order_corpus, order_cipher): print "The letter", x, "is encoded by the letter", y

The letter ץ is encoded by the letter ת
The letter ף is encoded by the letter ג
The letter ט is encoded by the letter ם
The letter ך is encoded by the letter צ
The letter ז is encoded by the letter כ
The letter צ is encoded by the letter ל
The letter ן is encoded by the letter ע
The letter ג is encoded by the letter ו
The letter ק is encoded by the letter ח
The letter ס is encoded by the letter ף
The letter פ is encoded by the letter א
The letter ד is encoded by the letter מ
The letter כ is encoded by the letter ש
The letter ח is encoded by the letter ז
The letter ע is encoded by the letter י
The letter ש is encoded by the letter ן
The letter נ is encoded by the letter ב
The letter ם is encoded by the letter ד
The letter מ is encoded by the letter ה
The letter ת is encoded by the letter ק
The letter ר is encoded by the letter ט
The letter ב is encoded by the letter ס
The letter א is encoded by the letter ץ
The letter ל is encoded by the letter ר
The letter ה is encoded by the letter ך
The letter י is encoded by the letter פ
The letter ו is encoded by the letter נ
sage: table = dict( (ord(y),x) for x,y in zip(order_corpus, order_cipher) )
sage: print rev_lines(T.translate(table))

למם לכס, וכעי נבפא
לתנדי-בס דלדו, רסשו:
תו תפ בעזק היה חרפא
ורםי-מאק זאשו.

"צואה לחי רק הנןמוק!
רבש-כחו, ביע-גנאחו!
רמוך-צאציא םחוד, גמוק
רבןכ לרצוחו!"

והות כעך דינו הבפ:
חש, בנכ תם נביס-גאיו —
וזה מרש לגע פרפף,
םנוד להאהואיו.

מושו מורש כנה-הצום,
והנןמוק, מיחיו שועסום,
וכוכ לימא הרחוש,
לוממ וחסון!

לרבי בפופ - הלד! הלד! -
םזםט הדיך לחרהא!
חןע תם אתכ נצאו הפש
ותע ליםו גהא.

"תך סןעםו, תם הנןמוק?
תבלסט, יעשי הגב!
הו יוף-געהה! ילת! ילת!"
לבשוםו נגב.

למם לכס, וכעי נבפא
לתנדי-בס דלדו, רסשו:
תו תפ בעזק היה חרפא
ורםי-מאק זאשו.

sage: # Now use the hint
sage: crib_plain  = u"ילדי הצח"
sage: crib_cipher = u"פיןפ ךוס"
sage: order_corpus = sorted(set(alphabet) - set(crib_plain), key=C.count)
sage: order_cipher = sorted(set(alphabet) - set(crib_cipher), key=T.count)
sage: table = dict( (ord(y),x) for x,y in zip(order_corpus, order_cipher) + zip(crib_plain, crib_cipher) )
sage: print rev_lines(T.translate(table))

אתמ אפס, ופלי נחכב
אםנעי-חס עאעו, רסדו:
םו םכ חלזק היה שרכב
ורמי-תבק זבדו.

"ןובה אשי רק הנגתוק!
רחד-פשו, חיל-צנבשו!
רתוך-ןבןיב משוע, צתוק
רחגפ ארןושו!"

והום פלך עינו החכ:
שד, חנפ םמ נחיס-צביו —
וזה תרד אצל כרכף,
מנוע אהבהוביו.

תודו תורד פנה-הןומ,
והנגתוק, תישיו דולסומ,
ופופ איתב הרשוד,
אותת ושסוג!

ארחי חכוכ - האע! האע! -
מזמט העיך אשרהב!
שגל םמ בםפ נןבו הכד
וםל אימו צהב.

"םך סגלמו, םמ הנגתוק?
םחאסט, ילדי הצח!
הו יוף-צלהה! יאם! יאם!"
אחדומו נצח.

אתמ אפס, ופלי נחכב
אםנעי-חס עאעו, רסדו:
םו םכ חלזק היה שרכב
ורמי-תבק זבדו.
[[/code]]

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License