Example #1
0
 def decrypt(self,ciphertext):
     ciphertext = cryptutils.convertInput(ciphertext,['upper','nospace','charonly'])
     #second, break the ciphertext up into digraphs
     digraphs = self.createDigraphs(ciphertext)
     #encode each digraph
     ciphergraphs = []
     for dg in digraphs:
         pair = self.transposeDigraph(dg,forward=False)
         ciphergraphs.append(''.join(pair))
         
     #remove any superfluous X characters
     plaintext = ''
     i = 0
     while i < len(ciphergraphs)-1:
         cg1 = ciphergraphs[i]
         cg2 = ciphergraphs[i+1]
         if cg1[1] == 'X' and cg1[0] == cg2[0]:
             plaintext = plaintext + cg1[0]
         else:
             plaintext = plaintext + cg1
         i += 1
     plaintext = plaintext + ciphergraphs[-1]
     if plaintext[-1] == 'X' and plaintext[-2] not in 'AEIOUY':
         plaintext = plaintext[0:-1]
     return plaintext
Example #2
0
 def encrypt(self, message):
     message = cryptutils.convertInput(message, ['upper'])
     message = message.replace(' ', '/')
     ct = self.charToNum(message)
     ct = self.addKey(ct)
     ct = self.numToChar(ct)
     return (ct)
Example #3
0
 def decrypt(self, ciphertext):
     ciphertext = cryptutils.convertInput(ciphertext, ['upper', 'nospace'])
     pt = self.charToNum(ciphertext)
     pt = self.subtractKey(pt)
     pt = self.numToChar(pt)
     pt = pt.replace('/', ' ')
     return (pt)
Example #4
0
    def decrypt(self, ciphertext):
        ciphertext = cryptutils.convertInput(ciphertext,
                                             ['upper', 'nospace', 'charonly'])
        R = self.key
        N = len(ciphertext)
        d = (R - 1) * 2
        row = range(0, N, d)
        plaintext = list('0' * N)
        cidx = 0
        for idx in row:
            plaintext[idx] = ciphertext[cidx]
            cidx += 1

        for i in range(1, R - 1):
            e = ((R - 1) - i) * 2
            p1 = range(i, N, d)
            p2 = range(i + e, N, d)
            dmax = max(len(p1), len(p2))
            for j in range(0, dmax):
                if j <= len(p1) - 1:
                    idx = p1[j]
                    plaintext[idx] = ciphertext[cidx]
                    cidx += 1
                if j <= len(p2) - 1:
                    idx = p2[j]
                    plaintext[idx] = ciphertext[cidx]
                    cidx += 1
        row = range(R - 1, N, d)
        for idx in row:
            plaintext[idx] = ciphertext[cidx]
            cidx += 1

        return ''.join(plaintext)
Example #5
0
 def decrypt(self,ciphertext):
     ciphertext = cryptutils.convertInput(ciphertext,['upper','nospace'])
     pt = self.charToNum(ciphertext)
     pt = self.subtractKey(pt)
     pt = self.numToChar(pt)
     pt = pt.replace('/',' ')
     return(pt)
Example #6
0
    def decrypt(self, ciphertext):
        ciphertext = cryptutils.convertInput(ciphertext,
                                             ['upper', 'nospace', 'charonly'])
        #second, break the ciphertext up into digraphs
        digraphs = self.createDigraphs(ciphertext)
        #encode each digraph
        ciphergraphs = []
        for dg in digraphs:
            pair = self.transposeDigraph(dg, forward=False)
            ciphergraphs.append(''.join(pair))

        #remove any superfluous X characters
        plaintext = ''
        i = 0
        while i < len(ciphergraphs) - 1:
            cg1 = ciphergraphs[i]
            cg2 = ciphergraphs[i + 1]
            if cg1[1] == 'X' and cg1[0] == cg2[0]:
                plaintext = plaintext + cg1[0]
            else:
                plaintext = plaintext + cg1
            i += 1
        plaintext = plaintext + ciphergraphs[-1]
        if plaintext[-1] == 'X' and plaintext[-2] not in 'AEIOUY':
            plaintext = plaintext[0:-1]
        return plaintext
Example #7
0
    def decrypt(self,ciphertext):
        ciphertext = cryptutils.convertInput(ciphertext,['upper','nospace','charonly'])
        R = self.key
        N = len(ciphertext)
        d = (R-1)*2
        row = range(0,N,d)
        plaintext = list('0'*N)
        cidx = 0
        for idx in row:
            plaintext[idx] = ciphertext[cidx]
            cidx += 1

        for i in range(1,R-1):
            e = ((R-1)-i)*2
            p1 = range(i,N,d)
            p2 = range(i+e,N,d)
            dmax = max(len(p1),len(p2))
            for j in range(0,dmax):
                if j <= len(p1)-1:
                    idx = p1[j]
                    plaintext[idx] = ciphertext[cidx]
                    cidx += 1
                if j <= len(p2)-1:
                    idx = p2[j]
                    plaintext[idx] = ciphertext[cidx]
                    cidx += 1
        row = range(R-1,N,d)
        for idx in row:
            plaintext[idx] = ciphertext[cidx]
            cidx += 1

        return ''.join(plaintext)
Example #8
0
 def encrypt(self,message):
     message = cryptutils.convertInput(message,['upper'])
     message = message.replace(' ','/')
     ct = self.charToNum(message)
     ct = self.addKey(ct)
     ct = self.numToChar(ct)
     return(ct)
Example #9
0
 def decrypt(self,ciphertext):
     key = self.key
     ciphertext = cryptutils.convertInput(ciphertext,['upper','nospaces','charonly'])
     plaintext = []
     for i in range(0,len(ciphertext)):
         ch = ciphertext[i]
         plaintext.append(cryptutils.rotateChar(ch,key,direction='backward'))
     return ''.join(plaintext)
Example #10
0
 def encrypt(self,message):
     message = cryptutils.convertInput(message,['upper','nospace','charonly'])
     digraphs = self.createDigraphs(message)
     #encode each digraph
     ciphergraphs = []
     for dg in digraphs:
         pair = self.transposeDigraph(dg)
         ciphergraphs.append(''.join(pair))
     return ''.join(ciphergraphs)
Example #11
0
    def encrypt(self,message):
        key = self.key
        message = cryptutils.convertInput(message,['upper','nospaces','charonly'])
        outmessage = []
        for i in range(0,len(message)):
            ch = message[i]
            outmessage.append(cryptutils.rotateChar(ch,key))

        return ''.join(outmessage)
Example #12
0
 def encrypt(self, message):
     message = cryptutils.convertInput(message,
                                       ['upper', 'nospace', 'charonly'])
     digraphs = self.createDigraphs(message)
     #encode each digraph
     ciphergraphs = []
     for dg in digraphs:
         pair = self.transposeDigraph(dg)
         ciphergraphs.append(''.join(pair))
     return ''.join(ciphergraphs)
Example #13
0
 def decrypt(self, ciphertext):
     key = self.key
     ciphertext = cryptutils.convertInput(ciphertext,
                                          ['upper', 'nospaces', 'charonly'])
     plaintext = []
     for i in range(0, len(ciphertext)):
         ch = ciphertext[i]
         plaintext.append(
             cryptutils.rotateChar(ch, key, direction='backward'))
     return ''.join(plaintext)
Example #14
0
    def encrypt(self, message):
        key = self.key
        message = cryptutils.convertInput(message,
                                          ['upper', 'nospaces', 'charonly'])
        outmessage = []
        for i in range(0, len(message)):
            ch = message[i]
            outmessage.append(cryptutils.rotateChar(ch, key))

        return ''.join(outmessage)
Example #15
0
 def decrypt(self,ciphertext):
     ciphertext = cryptutils.convertInput(ciphertext,['upper','nospace','charonly'])
     plaintext = ''
     for ch in ciphertext:
         if ch.isupper(): #this is a key
             self.rotateMovingRing(ch)
             continue
         idx = self.MovingRing.find(ch)
         plaintext = plaintext + self.StableRing[idx]
     return(plaintext)
Example #16
0
 def decrypt(self, ciphertext):
     ciphertext = cryptutils.convertInput(ciphertext,
                                          ['upper', 'nospace', 'charonly'])
     plaintext = ''
     for ch in ciphertext:
         if ch.isupper():  #this is a key
             self.rotateMovingRing(ch)
             continue
         idx = self.MovingRing.find(ch)
         plaintext = plaintext + self.StableRing[idx]
     return (plaintext)
Example #17
0
 def __init__(self,key):
     key = cryptutils.convertInput(key,['upper','nospace','charonly'])
     #first, make sure that the key has no repeated letters
     if len(cryptutils.unique(key)) != len(key):
         raise ValueError,'Key must not have any repeated characters.'
     #next, figure out the sort order of the letters in the key
     keylist = list(key)
     keylist.sort()
     keyorder = []
     for ch in key:
         keyorder.append(keylist.index(ch))
     
     #make that list of key orders the key
     self.key = keyorder
Example #18
0
 def encrypt(self,message):
     message = cryptutils.convertInput(message,['upper','nospace','charonly'])
     letters = []
     #Choose an initial configuration for the moving ring, and start the ciphertext with that character
     ciphertext = self.getLetter(self.RandomGen.randrange(0,self.NLETTERS))
     self.rotateMovingRing(ciphertext)
     for ch in message:
         if ch in letters:
             newch = self.getLetter(self.RandomGen.randrange(0,self.NLETTERS))
             self.rotateMovingRing(newch)
             ciphertext = ciphertext + newch
         else:
             letters.append(ch)
         idx = self.StableRing.find(ch)
         ciphertext = ciphertext + self.MovingRing[idx]
     
     return(ciphertext)
Example #19
0
 def __init__(self,key):
     key = cryptutils.unique(cryptutils.convertInput(key,['upper','nospace','charonly']))
     if len(key) > 25:
         raise ValueError,'Key Length must be 25 characters or less.'
     remchars = 25 - len(key)
     keylist = list(key)
     for ch in ascii_uppercase:
         if ch == 'J':
             continue
         if ch not in key:
             keylist.append(ch)
     key = ''.join(keylist)
     self.key = []
     self.key.append(key[0:5])
     self.key.append(key[5:10])
     self.key.append(key[10:15])
     self.key.append(key[15:20])
     self.key.append(key[20:25])
Example #20
0
 def __init__(self, key):
     key = cryptutils.unique(
         cryptutils.convertInput(key, ['upper', 'nospace', 'charonly']))
     if len(key) > 25:
         raise ValueError, 'Key Length must be 25 characters or less.'
     remchars = 25 - len(key)
     keylist = list(key)
     for ch in ascii_uppercase:
         if ch == 'J':
             continue
         if ch not in key:
             keylist.append(ch)
     key = ''.join(keylist)
     self.key = []
     self.key.append(key[0:5])
     self.key.append(key[5:10])
     self.key.append(key[10:15])
     self.key.append(key[15:20])
     self.key.append(key[20:25])
Example #21
0
    def encrypt(self, message):
        message = cryptutils.convertInput(message,
                                          ['upper', 'nospace', 'charonly'])
        letters = []
        #Choose an initial configuration for the moving ring, and start the ciphertext with that character
        ciphertext = self.getLetter(self.RandomGen.randrange(0, self.NLETTERS))
        self.rotateMovingRing(ciphertext)
        for ch in message:
            if ch in letters:
                newch = self.getLetter(
                    self.RandomGen.randrange(0, self.NLETTERS))
                self.rotateMovingRing(newch)
                ciphertext = ciphertext + newch
            else:
                letters.append(ch)
            idx = self.StableRing.find(ch)
            ciphertext = ciphertext + self.MovingRing[idx]

        return (ciphertext)
Example #22
0
    def encrypt(self,message):
        message = cryptutils.convertInput(message,['upper','nospace','charonly'])
        nrails = self.key
        nposts = len(message)
        block = []
        for i in range(0,nrails):
            rail = list(' '*nposts)
            block.append(rail)
        
        rows = self.getRows(nrails,len(message))
       
        for i in range(0,len(message)):
            block[rows[i]][i] = message[i]
        
        ciphertext = ''
        for b in block:
            ciphertext = ciphertext + ''.join(b).replace(' ','')

        return ciphertext
Example #23
0
    def encrypt(self, message):
        message = cryptutils.convertInput(message,
                                          ['upper', 'nospace', 'charonly'])
        nrails = self.key
        nposts = len(message)
        block = []
        for i in range(0, nrails):
            rail = list(' ' * nposts)
            block.append(rail)

        rows = self.getRows(nrails, len(message))

        for i in range(0, len(message)):
            block[rows[i]][i] = message[i]

        ciphertext = ''
        for b in block:
            ciphertext = ciphertext + ''.join(b).replace(' ', '')

        return ciphertext
Example #24
0
 def decrypt(self,ciphertext):
     ciphertext = cryptutils.convertInput(ciphertext,['upper','nospace','charonly'])
     nrows = len(ciphertext)//len(self.key)
     rem = len(ciphertext) % len(self.key)
     block = []
     for row in range(0,nrows):
         rowlist = list('X'*len(self.key))
         block.append(rowlist)
     if rem:
         rowlist = list('X'*rem)
         block.append(rowlist)
     idx = 0
     for i in range(0,len(self.key)):
         col = self.key.index(i)
         for row in range(0,nrows):
             block[row][col] = ciphertext[idx]
             idx += 1
         if col <= rem-1:
             block[row+1][col] = ciphertext[idx]
             idx += 1
     plaintext = ''
     for chunk in block:
         plaintext = plaintext + ''.join(chunk)
     return plaintext
Example #25
0
    def encrypt(self,message):
        message = cryptutils.convertInput(message,['upper','nospace','charonly'])
        if len(message) < len(self.key):
            raise ValueError,'Message must be longer than key'
        ncols = len(self.key)
        nrows = len(message)//ncols
        block = []
        nrem = len(message) % ncols
        for i in range(0,nrows):
            istart = i*ncols
            iend = istart+ncols
            block.append(message[istart:iend])
        
        block.append(message[iend:])
        #now read from the columns in the order specified by the key
        ciphertext = []
        for i in range(0,len(self.key)):
            col = self.key.index(i)
            for j in range(0,nrows):
                ciphertext.append(block[j][col])
            if col < nrem:
                ciphertext.append(block[j+1][col])

        return ''.join(ciphertext)
Example #26
0
    def __init__(self, key):
        key = cryptutils.convertInput(key, ['upper', 'nospace', 'charonly'])
        keyvalue = sum([self.getNumber(ch) for ch in key])
        r = random.Random()
        r.seed(keyvalue)
        values = []
        movring = ['x'] * self.NLETTERS
        chidx = 0
        while len(values) < self.NLETTERS:
            ch = string.ascii_lowercase[chidx]
            idx = r.randint(0, self.NLETTERS - 1)
            if idx in values:
                continue
            movring[idx] = ch
            values.append(idx)
            chidx += 1

        self.MovingRing = ''.join(movring)

        if not isinstance(key, str) and len(key) != 1:
            raise ValueError, 'Key must be a single character'
        self.key = key[0].lower()
        self.RandomGen = random.Random()
        self.RandomGen.seed()
Example #27
0
    def __init__(self,key):
        key = cryptutils.convertInput(key,['upper','nospace','charonly'])
        keyvalue = sum([self.getNumber(ch) for ch in key])
        r = random.Random()
        r.seed(keyvalue)
        values = []
        movring = ['x']*self.NLETTERS
        chidx = 0
        while len(values) < self.NLETTERS:
            ch = string.ascii_lowercase[chidx]
            idx = r.randint(0,self.NLETTERS-1)
            if idx in values:
                continue
            movring[idx] = ch
            values.append(idx)
            chidx += 1
            
        self.MovingRing = ''.join(movring)

        if not isinstance(key,str) and len(key) != 1:
            raise ValueError, 'Key must be a single character'
        self.key = key[0].lower()
        self.RandomGen = random.Random()
        self.RandomGen.seed()
Example #28
0
 def __init__(self,key):
     key = cryptutils.convertInput(key,['upper','nospace','charonly'])
     if len(key) < self.KEYLENGTH:
         raise ValueError,'Key Length must be %i characters or more.' % (self.KEYLENGTH)
     self.key = self.orderKey(key[0:self.KEYLENGTH])
Example #29
0
 def __init__(self, key):
     key = cryptutils.convertInput(key, ['upper', 'nospace', 'charonly'])
     if len(key) < self.KEYLENGTH:
         raise ValueError, 'Key Length must be %i characters or more.' % (
             self.KEYLENGTH)
     self.key = self.orderKey(key[0:self.KEYLENGTH])