bean[~/crypto] python3 Python 3.6.9 (default, Oct 8 2020, 12:12:24) [GCC 8.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> s="All the animals in the zoo!" >>> k=17 >>> s[0] 'A' >>> s[0].lower() 'a' >>> s[0].isalpha() True >>> s[0].lower() 'a' >>> ord(s[0].lower()) 97 >>> ord(s[0].lower())-ord('a') 0 >>> ord(s[1].lower())-ord('a') 11 >>> (ord(s[1].lower())-ord('a'))+k 28 >>> ((ord(s[1].lower())-ord('a'))+k)%26 2 >>> ((ord(s[1].lower())-ord('a'))+k)%26+ord('A') 67 >>> char(((ord(s[1].lower())-ord('a'))+k)%26+ord('A')) Traceback (most recent call last): File "", line 1, in NameError: name 'char' is not defined >>> chr(((ord(s[1].lower())-ord('a'))+k)%26+ord('A')) 'C' >>> chr(((ord(s[0].lower())-ord('a'))+k)%26+ord('A')) 'R' >>> chr(((ord(s[1].lower())-ord('a'))+k)%26+ord('A')) 'C' >>> for i in range(len(s)): ... chr(((ord(s[1].lower())-ord('a'))+k)%26+ord('A')) ... 'C' 'C' 'C' 'C' 'C' 'C' 'C' 'C' 'C' 'C' 'C' 'C' 'C' 'C' 'C' 'C' 'C' 'C' 'C' 'C' 'C' 'C' 'C' 'C' 'C' 'C' 'C' >>> for i in range(len(s)): ... chr(((ord(s[i].lower())-ord('a'))+k)%26+ord('A')) ... 'R' 'C' 'C' 'E' 'K' 'Y' 'V' 'E' 'R' 'E' 'Z' 'D' 'R' 'C' 'J' 'E' 'Z' 'E' 'E' 'K' 'Y' 'V' 'E' 'Q' 'F' 'F' 'F' >>> k 17 >>> k=[17,3, 4,9] >>> k+3 Traceback (most recent call last): File "", line 1, in TypeError: can only concatenate list (not "int") to list >>> for i in range(len(s)): ... if s[i].ialpha(): ... chr(((ord(s[i].lower())-ord('a'))+k)%26+ord('A')) ... Traceback (most recent call last): File "", line 2, in AttributeError: 'str' object has no attribute 'ialpha' >>> for i in range(len(s)): ... if s[i].isalpha(): ... chr(((ord(s[i].lower())-ord('a'))+k)%26+ord('A')) ... Traceback (most recent call last): File "", line 3, in TypeError: unsupported operand type(s) for +: 'int' and 'list' >>> k=3 >>> for i in range(len(s)): ... if s[i].isalpha(): ... chr(((ord(s[i].lower())-ord('a'))+k)%26+ord('A')) ... 'D' 'O' 'O' 'W' 'K' 'H' 'D' 'Q' 'L' 'P' 'D' 'O' 'V' 'L' 'Q' 'W' 'K' 'H' 'C' 'R' 'R' >>> for i in range(len(s)): ... if s[i].isalpha(): ... File "", line 3 ^ IndentationError: expected an indented block >>> >>> >>> >>> k=[1,2,17,5] >>> len(k) 4 >>> len(s) 27 >>> for i in range(len(s)): ... if s[i].isalpha(): ... chr(((ord(s[i].lower())-ord('a'))+k[i%len(k)])%26+ord('A')) ... 'B' 'N' 'C' 'U' 'J' 'V' 'B' 'P' 'Z' 'R' 'B' 'N' 'J' 'J' 'P' 'Y' 'I' 'G' 'E' 'P' 'Q' >>> for i in range(len(s)): ... if s[i].isalpha(): ... print(i," ",i%len(k)) ... 0 0 1 1 2 2 4 0 5 1 6 2 8 0 9 1 10 2 11 3 12 0 13 1 14 2 16 0 17 1 19 3 20 0 21 1 23 3 24 0 25 1 >>> len(s) 27 >>> len(k) 4 >>> s 'All the animals in the zoo!' >>> s="Alltheanimalsinthezoo" >>> for i in range(len(s)): ... if s[i].isalpha(): ... print(i," ",i%len(k)) ... 0 0 1 1 2 2 3 3 4 0 5 1 6 2 7 3 8 0 9 1 10 2 11 3 12 0 13 1 14 2 15 3 16 0 17 1 18 2 19 3 20 0 >>> len(s) 21 >>> for i in range(len(s)): ... if s[i].isalpha(): ... chr(((ord(s[i].lower())-ord('a'))+k[i%len(k)])%26+ord('A')) ... 'B' 'N' 'C' 'Y' 'I' 'G' 'R' 'S' 'J' 'O' 'R' 'Q' 'T' 'K' 'E' 'Y' 'I' 'G' 'Q' 'T' 'P' >>> ciphertext="" >>> for i in range(len(s)): ... if s[i].isalpha(): ... ciphertext += chr(((ord(s[i].lower())-ord('a'))+k[i%len(k)])%26+ord('A')) ... >>> ciphertext 'BNCYIGRSJORQTKEYIGQTP' >>> k [1, 2, 17, 5] >>> k [1, 2, 17, 5] >>> -k Traceback (most recent call last): File "", line 1, in TypeError: bad operand type for unary -: 'list' >>> val=17 >>> -val -17 >>> k [1, 2, 17, 5] >>> k [1, 2, 17, 5] >>> [-i for i in k] [-1, -2, -17, -5] >>> def vigenere_decrypt(s,k): ... return vigenere_encrypt(s,[-i for i in k]) ... >>> >>> def freq_table(string):string = string.lower() letters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"] chars_in_string = Counter(string) res = {} for letter in letters: if(letter in chars_in_string): res[letter] = chars_in_string[letter] else: res[letter] = 0 values = res.values() values_list = list(values) return(values_list) print(freq_table('ALLTHEANIMALSINTHEZOO')) File "", line 1 def freq_table(string):string = string.lower() letters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"] chars_in_string = Counter(string) res = {} for letter in letters: if(letter in chars_in_string): res[letter] = chars_in_string[letter] else: res[letter] = 0 values = res.values() values_list = list(values) return(values_list) print(freq_table('ALLTHEANIMALSINTHEZOO')) ^ SyntaxError: invalid syntax >>> from collections import Counter >>> s 'Alltheanimalsinthezoo' >>> Counter(s) Counter({'l': 3, 't': 2, 'h': 2, 'e': 2, 'a': 2, 'n': 2, 'i': 2, 'o': 2, 'A': 1, 'm': 1, 's': 1, 'z': 1}) >>> def freq_table(string): ... File "", line 2 ^ IndentationError: expected an indented block >>> def freq_table(s): ... [c for c in s] ... >>> def freq_table(s): ... counts=[0]*26 ... for i in range(26): ... counts[i] = sum([c==chr(ord('a')+i) for c in s]) ... >>> freq_table(s) >>> def freq_table(s): ... counts=[0]*26 ... for i in range(26): ... counts[i] = sum([c==chr(ord('a')+i) for c in s]) ... return counts ... >>> freq_table(s) [2, 0, 0, 0, 2, 0, 0, 2, 2, 0, 0, 3, 1, 2, 2, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 1] >>> max(freq_table(s)) 3 >>> def max_is_E(s): ... return freq_table(s)[4]==max(freq_table(s) ... ... KeyboardInterrupt >>> def max_is_E(s): ... return freq_table(s)[4]==max(freq_table(s)) ... >>> max_is_E("thaisadfaosdjfoiulkanslkjadslfkjlakdsjf") False >>> max_is_E("thaisadfaosdjfoiulkanslkjadslfkjlakdsjfeeeeeeeeeeeeeeeeeeeeeeeeeee") True >>> def CD(s,k): ... scl=[c.lower() for c in s if c.isalpha()] ... ciphertext=''.join([chr((ord(c)-ord('a')+k)%26+ord('a')) for c in scl]) ... returrn ciphertext File "", line 4 returrn ciphertext ^ SyntaxError: invalid syntax >>> >>> def CD(s,k): ... scl=[c.lower() for c in s if c.isalpha()] ... ciphertext=''.join([chr((ord(c)-ord('a')+k)%26+ord('a')) for c in scl]) ... return ciphertext ... >>> CD('DOOWKHDQLPDOVDWWKHCRR',3) 'grrznkgtosgrygzznkfuu' >>> CD('DOOWKHDQLPDOVDWWKHCRR',4) 'hssaolhupthszhaaolgvv' >>> CD('DOOWKHDQLPDOVDWWKHCRR',-3) 'alltheanimalsatthezoo' >>> def CD(s,k): ... scl=[c.lower() for c in s if c.isalpha()] ... ciphertext=''.join([chr((ord(c)-ord('a')-k)%26+ord('a')) for c in scl]) ... return ciphertext ... >>> CD('DOOWKHDQLPDOVDWWKHCRR',3) 'alltheanimalsatthezoo' >>> for i in range(1,26): ... print(i,": ",CD('DOOWKHDQLPDOVDWWKHCRR',i) ... ... ) ... 1 : cnnvjgcpkocnucvvjgbqq 2 : bmmuifbojnbmtbuuifapp 3 : alltheanimalsatthezoo 4 : zkksgdzmhlzkrzssgdynn 5 : yjjrfcylgkyjqyrrfcxmm 6 : xiiqebxkfjxipxqqebwll 7 : whhpdawjeiwhowppdavkk 8 : vggoczvidhvgnvooczujj 9 : uffnbyuhcgufmunnbytii 10 : teemaxtgbfteltmmaxshh 11 : sddlzwsfaesdksllzwrgg 12 : rcckyvrezdrcjrkkyvqff 13 : qbbjxuqdycqbiqjjxupee 14 : paaiwtpcxbpahpiiwtodd 15 : ozzhvsobwaozgohhvsncc 16 : nyygurnavznyfnggurmbb 17 : mxxftqmzuymxemfftqlaa 18 : lwwesplytxlwdleespkzz 19 : kvvdrokxswkvckddrojyy 20 : juucqnjwrvjubjccqnixx 21 : ittbpmivquitaibbpmhww 22 : hssaolhupthszhaaolgvv 23 : grrznkgtosgrygzznkfuu 24 : fqqymjfsnrfqxfyymjett 25 : eppxliermqepwexxlidss >>> for i in range(1,26): ... print(i,": ",CD('DOOWKHDQLPDOVDWWKHCRR',i)) ... 1 : cnnvjgcpkocnucvvjgbqq 2 : bmmuifbojnbmtbuuifapp 3 : alltheanimalsatthezoo 4 : zkksgdzmhlzkrzssgdynn 5 : yjjrfcylgkyjqyrrfcxmm 6 : xiiqebxkfjxipxqqebwll 7 : whhpdawjeiwhowppdavkk 8 : vggoczvidhvgnvooczujj 9 : uffnbyuhcgufmunnbytii 10 : teemaxtgbfteltmmaxshh 11 : sddlzwsfaesdksllzwrgg 12 : rcckyvrezdrcjrkkyvqff 13 : qbbjxuqdycqbiqjjxupee 14 : paaiwtpcxbpahpiiwtodd 15 : ozzhvsobwaozgohhvsncc 16 : nyygurnavznyfnggurmbb 17 : mxxftqmzuymxemfftqlaa 18 : lwwesplytxlwdleespkzz 19 : kvvdrokxswkvckddrojyy 20 : juucqnjwrvjubjccqnixx 21 : ittbpmivquitaibbpmhww 22 : hssaolhupthszhaaolgvv 23 : grrznkgtosgrygzznkfuu 24 : fqqymjfsnrfqxfyymjett 25 : eppxliermqepwexxlidss >>> for i in range(1,26): ... cl=CD('DOOWKHDQLPDOVDWWKHCRR',i) ... if max_is_E(cl): ... print(i,": ",cl," might be the right answer") ... 25 : eppxliermqepwexxlidss might be the right answer >>> s="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. " >>> s 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. ' >>> cipher=CD(s,-17) >>> cipher 'cfivdzgjldufcfijzkrdvktfejvtkvkliruzgzjtzexvczkjvuufvzljdfukvdgfizetzuzuleklkcrsfivvkufcfivdrxerrczhlrlkvezdrudzezdmvezrdhlzjefjkiluvovitzkrkzfelccrdtfcrsfizjezjzlkrczhlzgvovrtfddfuftfejvhlrkulzjrlkvzilivufcfizeivgivyveuvizkzemfclgkrkvmvczkvjjvtzccldufcfivvlwlxzrkelccrgrizrklivotvgkvlijzekfttrvtrktlgzurkrkefegifzuvekjlekzetlcgrhlzfwwztzruvjvilekdfcczkrezdzuvjkcrsfild' >>> for i in range(1,26): ... cl=CD(cipher,i) ... if max_is_E(cl): ... print(i,": ",cl," might be the right answer") ... 21 : hknaieloqizkhknoepwiapykjoaypapqnwzeleoyejcahepoazzkaeqoikzpailknejyezezqjpqphwxknaapzkhknaiwcjwwhemqwqpajeiwziejeirajewimqeojkopnqzatanyepwpekjqhhwiykhwxkneojeoeqpwhemqelatawykiikzkykjoamqwpzqeowqpaenqnazkhknejnalnadajzanepejrkhqlpwparahepaooayehhqizkhknaaqbqcewpjqhhwlwnewpqnatyalpaqnoejpkyywaywpyqlezwpwpjkjlnkezajpoqjpejyqhlwmqekbbeyewzaoanqjpikhhepwjeiezaophwxknqi might be the right answer >>> s="[32] But I must explain to you how all this mistaken idea of reprobating pleasure and extolling pain arose. To do so, I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure? [33] On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammeled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains." >>> cipher=CD(s,-17) >>> cipher 'slkzdljkvogcrzekfpflyfnrcckyzjdzjkrbvezuvrfwivgifsrkzexgcvrjlivreuvokfcczexgrzerifjvkfufjfznzccxzmvpflrtfdgcvkvrttflekfwkyvjpjkvdreuvogfleukyvrtklrckvrtyzexjfwkyvxivrkvogcfivifwkyvkilkykyvdrjkvislzcuvifwyldreyrggzevjjeffevivavtkjuzjczbvjfirmfzujgcvrjlivzkjvcwsvtrljvzkzjgcvrjlivslksvtrljvkyfjvnyfufefkbefnyfnkfglijlvgcvrjlivirkzferccpvetflekvitfejvhlvetvjkyrkrivvokivdvcpgrzewlcefirxrzezjkyvivrepfevnyfcfmvjfiglijlvjfiuvjzivjkffskrzegrzefwzkjvcwsvtrljvzkzjgrzeslkfttrjzferccptzitldjkretvjfttlizenyztykfzcreugrzetregiftlivyzdjfdvxivrkgcvrjlivkfkrbvrkizmzrcvordgcvnyztyfwljvmvileuvikrbvjcrsfizfljgypjztrcvovitzjvvotvgkkffskrzejfdvrumrekrxvwifdzkslknyfyrjrepizxykkfwzeuwrlcknzkyrdrenyftyffjvjkfveafprgcvrjlivkyrkyrjefreefpzextfejvhlvetvjfifevnyfrmfzujrgrzekyrkgifultvjefivjlckrekgcvrjlivfekyvfkyviyreunvuvefletvnzkyizxykvfljzeuzxerkzfereuuzjczbvdvenyfrivjfsvxlzcvureuuvdfirczqvuspkyvtyridjfwgcvrjlivfwkyvdfdvekjfsczeuvuspuvjzivkyrkkyvptreefkwfivjvvkyvgrzereukiflscvkyrkrivsfleukfvejlvreuvhlrcscrdvsvcfexjkfkyfjvnyfwrzczekyvziulkpkyiflxynvrbevjjfwnzccnyztyzjkyvjrdvrjjrpzexkyiflxyjyizebzexwifdkfzcreugrzekyvjvtrjvjrivgviwvtkcpjzdgcvreuvrjpkfuzjkzexlzjyzerwivvyflinyvefligfnvifwtyfztvzjlekirddvcvureunyveefkyzexgivmvekjflisvzexrscvkfufnyrknvczbvsvjkvmvipgcvrjlivzjkfsvnvctfdvureuvmvipgrzermfzuvuslkzetvikrzetzitldjkretvjreufnzexkfkyvtcrzdjfwulkpfikyvfsczxrkzfejfwsljzevjjzknzccwivhlvekcpfttlikyrkgcvrjlivjyrmvkfsvivgluzrkvureureefpretvjrttvgkvukyvnzjvdrekyvivwfivrcnrpjyfcujzekyvjvdrkkvijkfkyzjgizetzgcvfwjvcvtkzfeyvivavtkjgcvrjlivjkfjvtlivfkyvixivrkvigcvrjlivjfivcjvyvveulivjgrzejkfrmfzunfijvgrzej' >>> for i in range(1,26): ... cl=CD(cipher,i) ... if max_is_E(cl): ... print(i,": ",cl," might be the right answer") ... 17 : butimustexplaintoyouhowallthismistakenideaofreprobatingpleasureandextollingpainarosetodosoiwillgiveyouacompleteaccountofthesystemandexpoundtheactualteachingsofthegreatexplorerofthetruththemasterbuilderofhumanhappinessnoonerejectsdislikesoravoidspleasureitselfbecauseitispleasurebutbecausethosewhodonotknowhowtopursuepleasurerationallyencounterconsequencesthatareextremelypainfulnoragainisthereanyonewholovesorpursuesordesirestoobtainpainofitselfbecauseitispainbutoccasionallycircumstancesoccurinwhichtoilandpaincanprocurehimsomegreatpleasuretotakeatrivialexamplewhichofuseverundertakeslaboriousphysicalexerciseexcepttoobtainsomeadvantagefromitbutwhohasanyrighttofindfaultwithamanwhochoosestoenjoyapleasurethathasnoannoyingconsequencesoronewhoavoidsapainthatproducesnoresultantpleasureontheotherhandwedenouncewithrighteousindignationanddislikemenwhoaresobeguiledanddemoralizedbythecharmsofpleasureofthemomentsoblindedbydesirethattheycannotforeseethepainandtroublethatareboundtoensueandequalblamebelongstothosewhofailintheirdutythroughweaknessofwillwhichisthesameassayingthroughshrinkingfromtoilandpainthesecasesareperfectlysimpleandeasytodistinguishinafreehourwhenourpowerofchoiceisuntrammeledandwhennothingpreventsourbeingabletodowhatwelikebesteverypleasureistobewelcomedandeverypainavoidedbutincertaincircumstancesandowingtotheclaimsofdutyortheobligationsofbusinessitwillfrequentlyoccurthatpleasureshavetoberepudiatedandannoyancesacceptedthewisemanthereforealwaysholdsinthesematterstothisprincipleofselectionherejectspleasurestosecureothergreaterpleasuresorelseheendurespainstoavoidworsepains might be the right answer >>>