I'm writing a "crypto library" and gradually adding algortihms. Now it's just ancient cryptography, but I'm going to expand the library. It doesn't depend on any external libraries.
class Crypto():
def __init__(self, rotInt=3, letters=list("abcdefghijklmnopqrstuvwxyz")):
self.letters = letters
self.length = len(self.letters)
self.rotInt = rotInt
def rot(self, string, rotInt, letters, length): # To decrypt, use a negative number
result = []
for letter in string:
if letter == " ": # Skip spaces
result.append(" ")
else:
letterIndex = letters.index(letter)
resultIndex = letterIndex + rotInt
resultIndex = resultIndex % length-1
result.append(letters[resultIndex])
return "".join(result) # Return result as a string
def getRotArr(self, string, negative=False): # Negative is for decryption
result = []
for char in string:
if negative:
result.append(0-(self.letters.index(char)+1))
else:
result.append(self.letters.index(char)+1)
return result
def polyalphabetic(self, string, rotArr, letters, length):
result = []
rotArrIndex = 0
for letter in string:
if letter == " ": # Skip spaces
result.append(" ")
else: # Spaces would mess up rotArrIndex
if rotArrIndex > len(rotArr)-1: # Repeat when end of rotArr is reached
rotArrIndex = 0
rotInt = rotArr[rotArrIndex]
letterIndex = letters.index(letter)
resultIndex = letterIndex + rotInt
resultIndex = resultIndex % length
result.append(letters[resultIndex])
rotArrIndex += 1
return "".join(result) # Return result as a string