Python Forum
Decryption not working if key has same symbol like text
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Decryption not working if key has same symbol like text
#1
I'm trying to decipher the text from ADFGV cipher where is matrix, alphabet and key, but I don't know how to fix it if the key has the same characters.

 def decrypt_ADFGVX(self, cod, key):
    """ Decrypt with the ADFGVX cipher. Does not depend on spacing of encoded text """
    matrices = list('ADFGVX')
    chars = [c for c in cod if c in matrices]
    key = list(key.upper())
    sortedkey = sorted(key)
    order = [key.index(ch) for ch in sortedkey]
    originalorder = [sortedkey.index(ch) for ch in key]
    base, extra = divmod(len(chars), len(key))
    strides = [base + (1 if extra > i else 0) for i in order]
    starts = list(accumulate(strides[:-1], lambda x, y: x + y))
    starts = [0] + starts
    ends = [starts[i] + strides[i] for i in range(len(key))]
    cols = [chars[starts[i]:ends[i]] for i in originalorder]
    pairs = []
    for i in range((len(chars) - 1) // len(key) + 1):
        for j in range(len(key)):
            if i * len(key) + j < len(chars):
                pairs.append(cols[j][i])

    decode = dict((v, k) for (k, v) in self.create_encode_dict().items())
    return ''.join([decode[pairs[i] + pairs[i + 1]] for i in range(0, len(pairs), 2)])

Attached Files

.py   main.py (Size: 16.9 KB / Downloads: 45)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Text conversion to lowercase is not working ineuw 3 481 Jan-16-2024, 02:42 AM
Last Post: ineuw
  while loop not working-I am using sublime text editor mma_python 4 1,149 Feb-05-2023, 06:26 PM
Last Post: deanhystad
  ANSI not working for change of text colors BliepMonster 10 3,424 Nov-10-2022, 09:28 AM
Last Post: BliepMonster
  Convert Excel file into csv with Pipe symbol.. mg24 4 1,348 Oct-18-2022, 02:59 PM
Last Post: Larz60+
  ImportError: /home/pybind11_example.cpython-37m-x86_64-linux-gnu.so: undefined symbol chaitra 2 5,132 Feb-03-2021, 05:14 AM
Last Post: chaitra
  encryption and decryption with python ibrahim 1 1,835 May-16-2020, 03:14 PM
Last Post: Larz60+
  Take particular symbol from textbox help samuelbachorik 3 2,179 Apr-14-2020, 03:55 AM
Last Post: steve_shambles
  cipher decryption tool nightfox82 0 1,344 Mar-25-2020, 06:36 AM
Last Post: nightfox82
  Hide symbol or characters in regular expression Gateux 0 2,276 Mar-21-2020, 10:25 AM
Last Post: Gateux
  loop through range until reach size and exclude specific symbol pino88 3 2,389 Sep-23-2019, 02:32 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020