SharedClassical.sagewsOpen in CoCalc
y = """DYHXPWCJKFKNXESXYCPDRKWCPWXSKNXIWKNJSXCXPJSXHEDRRCJDRRWXNXNGXWEDJSIKYMYXCCPYMWXCBXFJJSXKFXPYDYPRRDJCNKKMCJSXDNBKCCDGRXCSDNNXWDYVNDWWKWFPRNPYMJSXCJKWNJKCCXMVWXXYEPJXWCLWVDYVKAXWJSXGKEPYMJSXYJSXWXEDRRFKNXPVPDYPIPDYJESDIIKICJPFUVPCPIPDYJXFSKKIXYVDYXPYMWLMMXWKWMXWCPADCDKYKIJSXGWDVSJGLYJDYVKICDVYPRIRPVCCYPBBDYVPJJSXHPWMPWNPWXIWPDYKISXPWJHRPLVSJXWDYJSXEPWMWKKNPYMFSDXICTLPWJXWCPYMNXCCMXFUCVKYXPCSKWXIKWVKKMJSXHEDRRVWKEEDCJILRPGKLJJSXDWYPAHMPHCESXYJSXCXPCGXRKYVXMJKJSXNPYMPYXEBKWJKIFPRREPCXAXWKAXWJSXSKWDQKYWXNXNGXWDYVJSDCJSXHEDRRCJPYMJPRRXWPYMCPHDEPCPCPDRKWKYFXDEPCBPWJKIJSXYPAHPYMJSXYPAHEDRRPREPHCGXBPWJKINX"""
#y
#type(y)
A = AlphabeticStrings()
A
type(A)
S = SubstitutionCryptosystem(A)
ciphertext = S.encoding(y)
print str("Here is the cipher text:")
ciphertext
type(ciphertext)
dict_ctext = (ciphertext.frequency_distribution()).function()
#dict_ctext
#type(dict_ctext)
list_ctext = [[dict_ctext[x],x] for x in dict_ctext.keys()]
#list_ctext
#type(list_ctext)
list_ctext.sort(reverse=True)
list_ctext

# frequency analysis, order of letters etaoi| nshrd | lcumw
#######ABCDEFGHIJKLMNOPQRSTUVWXYZ
K = A('VPSIWCBYFTOUDMZAZLHQKGRENZ')
#type(K)
d = S(K)
#type(d)
plaintext = d(ciphertext)
print "Here is the decrypted plaintext text:"
plaintext
print "Here are the letter frequencies for this particular piece of text:"
dict_ctext = (plaintext.frequency_distribution()).function()
#dict_ctext
#type(dict_ctext)
list_ctext = [[dict_ctext[x],x] for x in dict_ctext.keys()]
#list_ctext
#type(list_ctext)
list_ctext.sort(reverse=True)
list_ctext




Free alphabetic string monoid on A-Z <class 'sage.monoids.string_monoid.AlphabeticStringMonoid_with_category'> Here is the cipher text: DYHXPWCJKFKNXESXYCPDRKWCPWXSKNXIWKNJSXCXPJSXHEDRRCJDRRWXNXNGXWEDJSIKYMYXCCPYMWXCBXFJJSXKFXPYDYPRRDJCNKKMCJSXDNBKCCDGRXCSDNNXWDYVNDWWKWFPRNPYMJSXCJKWNJKCCXMVWXXYEPJXWCLWVDYVKAXWJSXGKEPYMJSXYJSXWXEDRRFKNXPVPDYPIPDYJESDIIKICJPFUVPCPIPDYJXFSKKIXYVDYXPYMWLMMXWKWMXWCPADCDKYKIJSXGWDVSJGLYJDYVKICDVYPRIRPVCCYPBBDYVPJJSXHPWMPWNPWXIWPDYKISXPWJHRPLVSJXWDYJSXEPWMWKKNPYMFSDXICTLPWJXWCPYMNXCCMXFUCVKYXPCSKWXIKWVKKMJSXHEDRRVWKEEDCJILRPGKLJJSXDWYPAHMPHCESXYJSXCXPCGXRKYVXMJKJSXNPYMPYXEBKWJKIFPRREPCXAXWKAXWJSXSKWDQKYWXNXNGXWDYVJSDCJSXHEDRRCJPYMJPRRXWPYMCPHDEPCPCPDRKWKYFXDEPCBPWJKIJSXYPAHPYMJSXYPAHEDRRPREPHCGXBPWJKINX <class 'sage.monoids.string_monoid_element.StringMonoidElement'> [[0.115894039735099, X], [0.0976821192052979, P], [0.0761589403973510, W], [0.0745033112582782, J], [0.0728476821192053, K], [0.0711920529801325, Y], [0.0695364238410596, C], [0.0645695364238411, D], [0.0546357615894040, S], [0.0463576158940398, R], [0.0364238410596027, M], [0.0331125827814570, N], [0.0314569536423841, I], [0.0314569536423841, E], [0.0298013245033113, V], [0.0198675496688742, H], [0.0182119205298013, F], [0.0149006622516556, G], [0.0115894039735099, L], [0.0115894039735099, B], [0.0115894039735099, A], [0.00331125827814570, U], [0.00165562913907285, T], [0.00165562913907285, Q]] Here is the decrypted plaintext text: INYEARSTOCOMEWHENSAILORSAREHOMEFROMTHESEATHEYWILLSTILLREMEMBERWITHFONDNESSANDRESPECTTHEOCEANINALLITSMOODSTHEIMPOSSIBLESHIMMERINGMIRRORCALMANDTHESTORMTOSSEDGREENWATERSURGINGOVERTHEBOWANDTHENTHEREWILLCOMEAGAINAFAINTWHIFFOFSTACKGASAFAINTECHOOFENGINEANDRUDDERORDERSAVISIONOFTHEBRIGHTBUNTINGOFSIGNALFLAGSSNAPPINGATTHEYARDARMAREFRAINOFHEARTYLAUGHTERINTHEWARDROOMANDCHIEFSQUARTERSANDMESSDECKSGONEASHOREFORGOODTHEYWILLGROWWISTFULABOUTTHEIRNAVYDAYSWHENTHESEASBELONGEDTOTHEMANDANEWPORTOFCALLWASEVEROVERTHEHORIZONREMEMBERINGTHISTHEYWILLSTANDTALLERANDSAYIWASASAILORONCEIWASPARTOFTHENAVYANDTHENAVYWILLALWAYSBEPARTOFME Here are the letter frequencies for this particular piece of text: [[0.115894039735099, E], [0.0976821192052979, A], [0.0761589403973510, R], [0.0745033112582782, T], [0.0728476821192053, O], [0.0711920529801325, N], [0.0695364238410596, S], [0.0645695364238411, I], [0.0546357615894040, H], [0.0463576158940398, L], [0.0364238410596027, D], [0.0331125827814570, M], [0.0314569536423841, W], [0.0314569536423841, F], [0.0298013245033113, G], [0.0198675496688742, Y], [0.0182119205298013, C], [0.0149006622516556, B], [0.0115894039735099, V], [0.0115894039735099, U], [0.0115894039735099, P], [0.00331125827814570, K], [0.00165562913907285, Z], [0.00165562913907285, Q]]
# instantiate shift cryptosystem object
S = ShiftCryptosystem(AlphabeticStrings()); S
# get my text ready for enciphering by removing spaces/commas,etc.
s = """ this is a very
        long string if I had the
        energy to type more and more ..."""
P = S.encoding(s)
P
key = 1
# actually encode the text
C = S.enciphering(key, P);
C
PP = S.deciphering(key, C)
PP

# a = 0, (a + k) mod 26
a = 0
k = 10;
y = mod(a+k, 26)
# this is the encoded letter "a"
y

Shift cryptosystem on Free alphabetic string monoid on A-Z THISISAVERYLONGSTRINGIFIHADTHEENERGYTOTYPEMOREANDMORE UIJTJTBWFSZMPOHTUSJOHJGJIBEUIFFOFSHZUPUZQFNPSFBOENPSF THISISAVERYLONGSTRINGIFIHADTHEENERGYTOTYPEMOREANDMORE 10
# computer_brands = ["Apple", "Asus", "Dell", "Samsung"]
# for i in computer_brands:
#     print "i = " + str(i)
    
for i in range(1, 6):
    print "i = " + str(i)
i = 1 i = 2 i = 3 i = 4 i = 5
mod(40,26)

for i in range(0,26):
    if gcd(i, 26) == 1:
        a = inverse_mod(i, 26)
        print str(i) + "*" + str(a) + " = 1 mod 26" 
14 1*1 = 1 mod 26 3*9 = 1 mod 26 5*21 = 1 mod 26 7*15 = 1 mod 26 9*3 = 1 mod 26 11*19 = 1 mod 26 15*7 = 1 mod 26 17*23 = 1 mod 26 19*11 = 1 mod 26 21*5 = 1 mod 26 23*17 = 1 mod 26 25*25 = 1 mod 26
y = """GLJJSXWXDCCKNXJSDYVJSPJDNLCJCPHJKNHBXKBZXESKCJPYMKYJSXEPWNJSWXCSKZMESDFSZXPMCDYJKJSXBPZPFXKIULCJDFXDYJSXBWKFXCCKIVPDYDYVKLWWDVSJILZBZPFXEXNLCJYKJGXVLDZJHKIEWKYVILZMXXMCZXJLCYKJCXXRJKCPJDCIHKLWJSDWCJIKWIWXXMKNGHMWDYRDYVIWKNJSXFLBKIGDJJXWYXCCPYMSPJWXMEXNLCJIKWXAXWFKYMLFJKLWCJWLVVZXKYJSXSDVSBZPYXKIMDVYDJHPYMMDCFDBZDYXEXNLCJYKJPZZKEKLWFWXPJDAXBWKJXCJJKMXVXYXWPJXDYJKBSHCDFPZADKZXYFXPVPDYPYMPVPDYEXNLCJWDCXJKJSXNPUXCJDFSXDVSJCKINXXJDYVBSHCDFPZIKWFXEDJSCKLZIKWFX
""";
A = AlphabeticStrings(); A
S = SubstitutionCryptosystem(A); S
Y = S.encoding(y)
Y
Y.frequency_distribution()
Free alphabetic string monoid on A-Z Substitution cryptosystem on Free alphabetic string monoid on A-Z GLJJSXWXDCCKNXJSDYVJSPJDNLCJCPHJKNHBXKBZXESKCJPYMKYJSXEPWNJSWXCSKZMESDFSZXPMCDYJKJSXBPZPFXKIULCJDFXDYJSXBWKFXCCKIVPDYDYVKLWWDVSJILZBZPFXEXNLCJYKJGXVLDZJHKIEWKYVILZMXXMCZXJLCYKJCXXRJKCPJDCIHKLWJSDWCJIKWIWXXMKNGHMWDYRDYVIWKNJSXFLBKIGDJJXWYXCCPYMSPJWXMEXNLCJIKWXAXWFKYMLFJKLWCJWLVVZXKYJSXSDVSBZPYXKIMDVYDJHPYMMDCFDBZDYXEXNLCJYKJPZZKEKLWFWXPJDAXBWKJXCJJKMXVXYXWPJXDYJKBSHCDFPZADKZXYFXPVPDYPYMPVPDYEXNLCJWDCXJKJSXNPUXCJDFSXDVSJCKINXXJDYVBSHCDFPZIKWFXEDJSCKLZIKWFX Discrete probability space defined by {V: 0.0349344978165939, X: 0.115720524017467, J: 0.104803493449782, L: 0.0414847161572052, N: 0.0262008733624454, Y: 0.0611353711790393, P: 0.0545851528384279, B: 0.0240174672489083, D: 0.0742358078602620, F: 0.0349344978165939, H: 0.0174672489082969, S: 0.0524017467248908, U: 0.00436681222707424, W: 0.0567685589519650, I: 0.0327510917030568, K: 0.0829694323144105, M: 0.0327510917030568, Z: 0.0393013100436681, A: 0.00655021834061135, C: 0.0676855895196506, E: 0.0218340611353712, G: 0.00873362445414847, R: 0.00436681222707424}
# put your name as a comment at the top

# how do you declare a function?

# DESC: fabulous function does this
# INPUT: a, b, c integers
# OUTPUT: what does it return
# EXAMPLE: my_func(1,1,1) returns 2
def my_func(a, b, c):
    r = a*b + c
    return (r, a*b)
#end def my_func

(d,c) = my_func(1,1,1)
d
c

def myfactorial(n):
    print "n = " + str(n)
    if n == 1:
        return 1
    return n*myfactorial(n - 1)

#myfactorial(3)


2 1