encryption basics 3
encryption_basics3.py
#UPPERCASE LETTERS START AT 65 (ord("A")) #LOWERCASE LETTERS START AT 97 (ord("a")) #This can make it hard to work with Unicode ordinals. #Because of this, it is often easier to 'normalise' #Unicode characters to actual alphabet placements: c_normal = ord("c") - ord("a") print(c_normal) #prints 2 # 'c' is the 2nd element (or 3rd letter) in the alphabet: # a = 0 # b = 1 # c = 2 # d = 3 # etc. so for the alphabet placement of 'e': e_normal = ord("e") - ord("a") print(e_normal) #prints 4 #If, in encryption, i wanted to shift #character 'c' (2) by character 'e' (4), #i would get 2+4=6, which is character 'g': g_normal = c_normal + e_normal print(chr(g_normal + ord("a"))) #prints 'g' #If i was to shift character 'x' (23) by #character 'g' (6), i'd end up at element 29 #of the alphabet, which doesnt exist. #In this case, i'd wrap around to the start #of the alphabet again by using modulo (%): #(23+6)%26 = 3.. or character 'd': x_normal = ord("x") - ord("a") d_normal = (x_normal + g_normal) % 26 print(chr(d_normal + ord("a"))) #prints 'd' #the alphabet offers a base-26 system, which #offers more combinations than our decimal system #which is base-10. In our decimal system, after 9, #we go back to 0. Which is the same in the alphabet #if we go from z >> a (or a >> z). Thats why you will #see the modulo (%) operator alot with the number 26. #this lets us see how many letters past (remainder) 26 #we have gone.