SharedLearnPhython.sagewsOpen in CoCalc
# instantiate shift cryptosystem object
S = ShiftCryptosystem(AlphabeticStrings()); S
# get my text ready for enciphering by removing spaces/commas,etc.
P = S.encoding("You might belong in Gryffindor, where dwell the brave at heart, their daring, nerve, and chivalry set Gryffindors apart")
P
#choose key size, encrypt first 3 letters by hand Y>B O>R U>X
key = 3
# actually encode the text
C = S.enciphering(key, P);
C
#get partner text ready for decipher
Q = S.encoding("IDQVIRUWKHSDVWWZRZHHNVBRXKDYHEHHQUHDGLQJDERXWWKHEDGEUHDNLJRWBHWWRGDBLFRQVLGHUPBVHOIWKHOXFNLHVWPDQRQWKHIDFHRIWKHHDUWK")
Q
#run through all possible keys while deciphering the text
for i in range(0,25):
QQ = S.deciphering(i, Q);
QQ

#instatiate cryptosystem object
A = AffineCryptosystem(AlphabeticStrings()); A
#get me text ready for enciphering
B = A.encoding("I have a dream that one day this nation will rise up and live out the true meaning of its creed We hold these truths to be self-evident, that all men are created equal" )
B
#choose key sizes
keya = 3
keyb = 1
#actually encode the text, by hand I>Z, H>W, A>B
D = A.enciphering(keya, keyb, B)
D
#get partner text ready to decipher
E = A.encoding("PETERSGTEGPPETERSGTEGPPETERPETERPETERPETERGPPWFZGPSSRECFWRYICBBBCRSEWRDEFFOPETERSGTEGPEHQEDFFWQWPTGQFGWPYWLZWPWMRCPXSWWXYEPYEPETEROGEBXFWLWRQEPETEROGEBXFWFZECDDCREPFBOWTERAZEBIGPSIGSZFWLFZEEPEIO")
E
#run through possible keys
for i in range(0,25):
if gcd(i,26)==1:
for j in range(0,25):
EE = A.deciphering(i, j, E);
EE

#Jones text NEVERGIVEINNEVERGIVEINNEVERNEVERNEVERNEVERINNOTHINGGREATORSMALLLARGEORPETTYNEVERGIVEINEXCEPTTOCONVICTIONSOFHONOURANDGOODSENSENEVERYIELDTOFORCENEVERYIELDTOTHEAPPARENTLYOVERWHELMINGMIGHTOFTHEENEMY