alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def mult_cipher(plaintext, key):
"""Multiply the number corresponding to each letter in plaintext by key modulo 26. The key should be invertible
modulo 26, to ensure the message can be decrypted."""
ciphertext = []
for i in range(len(plaintext)):
place_alpha = alphabet.find(plaintext[i])
new_place = (key * place_alpha) % 26
ciphertext.append(alphabet[new_place])
return ciphertext
mult_cipher("ABCZ", 3)
"".join(mult_cipher("HELLO", 3))
# to decrypt, use key=9, which is inverse of 3 modulo 26, since 3*9 = 27, and 27%26=1.
mult_cipher('VMHHQ', 9)
mult_cipher("HELLO", 0)
alphabet[13]
# below we are using key=2, which is not invertible mod 26
# both A,N get encrypted as A, which means that decryption is impossible
mult_cipher("ANB",2)
factor(2^31-1)
factor(2^12311212)
%%time
factor(2^183-1)
%%time
# below is much faster than above factor computation, which means that sage cannot be just factoring
# the two numbers and comparing the prime factors
gcd(2^183-1, 2^31-1)
def my_gcd(a,b):
"""Returns gcd of a,b. Computed using Euclidean algorithm."""
if a==0:
return b
if b==0:
return a
if a<b:
print(a,b%a)
return my_gcd(a, b%a)
if a>=b:
print(a%b,b)
return my_gcd(a%b,b)
my_gcd(100,17)
%%time
my_gcd(2^31-1, 2^183-1)
my_gcd(2^31-1, 2^37-1)
my_gcd(2^122,3^15*2^42)
2^42