We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Keyword Transposition Cipher
Keyword Transposition Cipher
+ 0 comments PHP Solution
<?php $_fp = fopen("php://stdin", "r"); $num_tests = intval(trim(fgets(STDIN))); for ($t = 0; $t < $num_tests; $t++) { $keyword = trim(fgets(STDIN)); $ciphertext = trim(fgets(STDIN)); echo decode($keyword, $ciphertext) . "\n"; } function decode($keyword, $ciphertext) { #Step 1: Generate the substitution cipher based on the keyword //Remove duplicate letter from key and sort it. $keyword = array_values(array_unique(str_split($keyword))); asort($keyword); // Get aplhabet without letter in key $alphabet = array_diff(range('A', 'Z'), $keyword); // Chunk alphabet $chunk = array_chunk($alphabet, count($keyword)); foreach($keyword as $k=>$v) { $substitution[] = $v; foreach ($chunk as $arr) { if (isset($arr[$k])) { $substitution[] = $arr[$k]; } } } // Create dictionary $dictionary = array_combine($substitution, range('A', 'Z')); # Step 2: Decode (Apply the substitution cipher to the ciphertext) $decoded = ''; $ciphertext = str_split($ciphertext); foreach ($ciphertext as $char) { if (array_key_exists($char, $dictionary)) { $decoded .= $dictionary[$char]; } else { $decoded .= " "; } } return $decoded; } ?>
+ 0 comments python 3: from string import ascii_uppercase as aaa
def decode(k, m): # Removes duplicates from key key = ''.join([x for i, x in enumerate(k) if k.index(x) == i])
# Get aplhabet without chars in key alph = ''.join([x for x in aaa if x not in key]) # Get base ordered char dec = key + alph # Creates columns by indexing: range() with steps of len(key) and # Increases the start-value from 0 to len(key) + sorts them columns = sorted([''.join([dec[x] for x in range(n, len(dec), len(key))]) for n in range(len(key))]) # Creates a decoding dict d = {a: b for b, a in zip(aaa, ''.join(columns))} # Decode return ''.join(d[x] if x in d else ' ' for x in m)
for _ in range(int(input())): key = input() mes = input()
print(decode(key, mes))
+ 0 comments Just another solution: Python3, given description implemented staightforward ...
from itertools import cycle def make_cipher(key): 'Build monoalphabetic substitution list.' seed = list(dict.fromkeys(key)) # remove duplicate letters # build columns cols = [[c] for c in seed] col_it = cycle(cols) for c in map(chr, range(ord('A'), ord('Z') +1)): if c not in seed: next(col_it).append(c) # sort columns cols.sort() # join all return ''.join(''.join(col) for col in cols) def encrypt(key, plain): cipher = make_cipher(key) encrypt1 = (lambda c: ' ' if c == ' ' else cipher[ord(c)-ord('A')] ) return ''.join(map(encrypt1, plain)) def decrypt(key, crypt): cipher = make_cipher(key) decrypt1 = (lambda c: ' ' if c == ' ' else chr(ord('A')+cipher.index(c)) ) return ''.join(map(decrypt1, crypt)) if __name__ == '__main__': n = int(input()) for _ in range(n): print(decrypt(input(), input()))
+ 0 comments Is this algorithm help me to detect keyword stuffing in my blog? Or anyhow help me in me website's SEO?
[deleted] + 0 comments Python 3
from string import ascii_uppercase as aaa def decode(k, m): # Removes duplicates from key key = ''.join([x for i, x in enumerate(k) if k.index(x) == i]) # Get aplhabet without chars in key alph = ''.join([x for x in aaa if x not in key]) # Get base ordered char dec = key + alph # Creates columns by indexing: range() with steps of len(key) and # Increases the start-value from 0 to len(key) + sorts them columns = sorted([''.join([dec[x] for x in range(n, len(dec), len(key))]) for n in range(len(key))]) # Creates a decoding dict d = {a: b for b, a in zip(aaa, ''.join(columns))} # Decode return ''.join(d[x] if x in d else ' ' for x in m) for _ in range(int(input())): key = input() mes = input() print(decode(key, mes))
Load more conversations
Sort 50 Discussions, By:
Please Login in order to post a comment