Monoalphabetic Cipher Program in Python || Python
In this we are going to see a basic example of monoalphabetic cipher program in Python Programming Language.



small = list(map(chr, range(ord('a'), ord('z')+1)))
caps = list(map(chr, range(ord('A'), ord('Z')+1)))
spch = [" ", "%"]
freqDict = {}
def getSbCipher(pt, sbk):
    sbCipher = ""
    for alph in pt:
        if alph not in spch:
            if alph in small:
                sbCipher += small[(small.index(alph)+sbk) % 26]
            else:
                sbCipher += caps[(caps.index(alph)+sbk) % 26]
        else:
            sbCipher += spch[1]
        if alph not in freqDict:
            freqDict[alph] = round(((pt.count(alph)/len(pt))*100), 2)
    print("Frequency analysis of each letter of plaintext: ")
    print(freqDict)
    return sbCipher

def decryptCipher(SbCipher, sbk):
    pt = ""
    for alph in SbCipher:
        if alph not in spch:
            if alph in small:
                pt += small[(small.index(alph)-sbk) % 26]
            else:
                pt += caps[(caps.index(alph)-sbk) % 26]
        else:
            pt += spch[0]
    return pt

def main():
    pt = input("Enter Plaintext: ")
    sbk = int(input("Enter Substitution key: "))
    SbCipher = getSbCipher(pt, sbk)
    print("Encrypted Text: ", SbCipher)
    pt = decryptCipher(SbCipher, sbk)
    print("Decrypted Text: ", pt)

main()


#ENJOY CODING


Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post