def test_encipher(self): keys = (('tgcmpfyxuiewdhbzrvalknqso',5), ('ezrxdkuatgvncmiwhsqpyfblo',6)) plaintext = ('abcdefghiiklmnopqrstuvwxyzabcdefghiiklmnopqrstuvwxyz', 'abcdefghiiklmnopqrstuvwxyzabcdefghiiklmnopqrstuvwxyz') ciphertext = ('vchqefwfuospksiplpwzuwuwwaeeldwcfglizoprksoqugvfvxuf', 'gvdciztcgfoxclwhoshawmkxygvzcidtczfogclxhowhasmkwyxz') for i,key in enumerate(keys): enc = Bifid(*key).encipher(plaintext[i]) self.assertEqual(enc.upper(), ciphertext[i].upper())
def test_encipher(self): keys = (('tgcmpfyxuiewdhbzrvalknqso', 5), ('ezrxdkuatgvncmiwhsqpyfblo', 6)) plaintext = ('abcdefghiiklmnopqrstuvwxyzabcdefghiiklmnopqrstuvwxyz', 'abcdefghiiklmnopqrstuvwxyzabcdefghiiklmnopqrstuvwxyz') ciphertext = ('vchqefwfuospksiplpwzuwuwwaeeldwcfglizoprksoqugvfvxuf', 'gvdciztcgfoxclwhoshawmkxygvzcidtczfogclxhowhasmkwyxz') for i, key in enumerate(keys): enc = Bifid(*key).encipher(plaintext[i]) self.assertEqual(enc.upper(), ciphertext[i].upper())
def test_decipher(self): keys = (("tgcmpfyxuiewdhbzrvalknqso", 5), ("ezrxdkuatgvncmiwhsqpyfblo", 6)) plaintext = ( "abcdefghiiklmnopqrstuvwxyzabcdefghiiklmnopqrstuvwxyz", "abcdefghiiklmnopqrstuvwxyzabcdefghiiklmnopqrstuvwxyz", ) ciphertext = ( "vchqefwfuospksiplpwzuwuwwaeeldwcfglizoprksoqugvfvxuf", "gvdciztcgfoxclwhoshawmkxygvzcidtczfogclxhowhasmkwyxz", ) for i, key in enumerate(keys): dec = Bifid(*key).decipher(ciphertext[i]) self.assertEqual(dec.upper(), plaintext[i].upper())
regex = re.compile('[^a-zA-Z]') line = "".join(line.strip().split()) line = regex.sub('', line) line = line.replace('j', '') line = line.replace('J', '') fpwrite.write("%d | %s\n" % (len(line), line)) line = fp.readline() ciphertext = "" with open('message.txt', 'r') as fp: ciphertext = fp.readline().strip() with open('ramblings_size.txt', 'r') as fp: with open('results.txt', 'w') as res: line = fp.readline() while line: line = line.strip() print("using line ", line) splits = line.split("|") line = splits[1] line = line.strip() if len(line) != 25: line = fp.readline() continue attempt1 = Bifid(line, 5).decipher(ciphertext) res.write(attempt1 + "\n") attempt1 = Bifid(line[::-1], 5).decipher(ciphertext) res.write(attempt1 + "\n") print("tried line ", line) line = fp.readline()
ciphertext = 'snbwmuotwodwvcywfgmruotoozaiwghlabvuzmfobhtywftopmtawyhifqgtsiowetrksrzgrztkfctxnrswnhxshylyehtatssukfvsnztyzlopsv' keys = [ 'mrocktvquizphdbagsfewlynx', 'ocknymphswaqfdrugvexblitz', 'crwthvoxzapsqigymfeldbunk', 'hmfordwaltzcinqbuskpyxveg', 'phavfyxbugstonqmilkzdcrew', 'hesaidbcfgklmnopqrtuvwxyz', 'enqvahlbidgumkrwcfpostxyz', 'emilyqungschwarzkopfxtvbd', 'ohnfezcamrwsputyxigkqblvd', 'qtipforsuvnzxylemdcbaghwk', 'umblingvextfrowzyhackspdq', 'qvandzstruckmybigfoxwhelp', 'lumpydrabcgqvzinksfoxthew', 'heyiamnopqrstuvwxzbcdfgkl', 'quizvbmwlynxstockderpaghf', 'pledbigczarunksmyvwfoxthq', 'waltzgbquickfordsvexnymph', 'qwertyuioplkhgfdsazxcvbnm', 'zyxwvutsrqponmlkihgfedcba', 'aquickbrownfxmpsvethlzydg', ] for key in keys[::-1]: ciphertext = Bifid(key, 5).decipher(ciphertext) # Fill in the x print(str.lower(ciphertext).replace('x', ' '))
def encrypt(keycodeLines, encryptionDirection, plaintextContents): for i in range(len(keycodeLines)): #Iterate over the keycode. if (encryptionDirection == "encrypt"): splitLine = keycodeLines[i].split() else: splitLine = keycodeLines[len(keycodeLines) - 1 - i].split( ) #This ensures that if the encryption direction is set to decrypt, that this for loop reads the keycode.txt from end to beginning. # print("Line " + str(i) + " is " + keycodeLines[i] + " and the split line is: " + str(splitLine)) # This was an old debugging line that may be useful in the future. if (splitLine[0] == "caesar"): if (int(splitLine[1]) > 25 or int(splitLine[1]) < 1): print("Keycode line: " + str(i + 1) + ": Caesar shift detected on keycode line " + str(i) + " attempting to shift by value " + splitLine[1] + ".") sys.exit() else: originalPlaintext = plaintextContents if (debug): print("Keycode line: " + str(i + 1) + ": Caesar shift detected with an argument of " + splitLine[1] + ".") if (encryptionDirection == "encrypt"): plaintextContents = Caesar(int( splitLine[1])).encipher(plaintextContents) else: plaintextContents = Caesar(int( splitLine[1])).decipher(plaintextContents) if (debug): print("Keycode line: " + str(i + 1) + ": Caesar shifted " + originalPlaintext + " by " + splitLine[1] + " with a result of " + plaintextContents + ".") elif (splitLine[0] == "vigenere"): if (type(splitLine[1] != str)): originalPlaintext = plaintextContents if (debug): print("Keycode line: " + str(i + 1) + ": Vigenère shift detected with an argument of " + splitLine[1] + ".") if (encryptionDirection == "encrypt"): plaintextContents = Vigenere( splitLine[1]).encipher(plaintextContents) else: plaintextContents = Vigenere( splitLine[1]).decipher(plaintextContents) if (debug): print("Keycode line: " + str(i + 1) + ": Vigenère shifted " + originalPlaintext + " by " + splitLine[1] + " with a result of " + plaintextContents + ".") else: print("Keycode line: " + str(i + 1) + ": Vigenère shift detected on keycode line " + str(i) + " attempting to use key that is not a string.") elif (splitLine[0] == "porta"): if (type(splitLine[1] != str)): originalPlaintext = plaintextContents if (debug): print("Keycode line: " + str(i + 1) + ": Porta cipher detected with an argument of " + splitLine[1] + ".") if (encryptionDirection == "encrypt"): plaintextContents = Porta( splitLine[1]).encipher(plaintextContents) else: plaintextContents = Porta( splitLine[1]).decipher(plaintextContents) if (debug): print("Keycode line: " + str(i + 1) + ": Vigenère shifted " + originalPlaintext + " by " + splitLine[1] + " with a result of " + plaintextContents + ".") else: print("Keycode line: " + str(i + 1) + ": Vigenère shift detected on keycode line " + str(i) + " attempting to use key that is not a string.") elif (splitLine[0] == "adfgx"): if ( len(splitLine[1]) != 25 ): # This makes sure that the keysquare's length is exactly 25. print( "Keycode line: " + str(i + 1) + ": ADFGX cipher detected on keycode line " + str(i) + " attempting to use keysquare that is not 25 characters long." ) sys.exit() else: originalPlaintext = plaintextContents if (debug): print("Keycode line: " + str(i + 1) + ": ADFGX cipher detected with a keysquare of " + splitLine[1] + " and a keyword of " + splitLine[2] + ".") if (encryptionDirection == "encrypt"): plaintextContents = ADFGX( splitLine[1], splitLine[2]).encipher(plaintextContents) else: plaintextContents = ADFGX( splitLine[1], splitLine[2]).decipher(plaintextContents) if (debug): print("Keycode line: " + str(i + 1) + ": ADFGX ciphered " + originalPlaintext + " by a keysquare of " + splitLine[1] + " and a keyword of " + splitLine[2] + " with a result of " + plaintextContents + ".") elif ( splitLine[0] == "adfgvx" ): #The first argument is the keysquare, and the second argument is the keyword. if ( len(splitLine[1]) != 36 ): # This makes sure that the keysquare's length is exactly 36. print( "Keycode line: " + str(i) + ": ADFGVX cipher detected on keycode line " + str(i) + " attempting to use keysquare that is not 25 characters long, but is instead " + str(len(splitLine[1])) + " characters long.") sys.exit() else: originalPlaintext = plaintextContents if (debug): print("Keycode line: " + str(i + 1) + ": ADFGVX cipher detected with a keysquare of " + splitLine[1] + " and a keyword of " + splitLine[2] + ".") if (encryptionDirection == "encrypt"): plaintextContents = ADFGVX( splitLine[1], splitLine[2]).encipher(plaintextContents) else: plaintextContents = ADFGVX( splitLine[1], splitLine[2]).decipher(plaintextContents) if (debug): print("Keycode line: " + str(i + 1) + ": ADFGVX ciphered " + originalPlaintext + " by a keysquare of " + splitLine[1] + " and a keyword of " + splitLine[2] + " with a result of " + plaintextContents + ".") elif (splitLine[0] == "affine"): if ((int(splitLine[2]) < 1) or (int(splitLine[2]) > 25)): print( "Keycode line: " + str(i + 1) + ": Affine cipher detected on keycode line " + str(i) + " attempting to use b value outside of the range of 1-25.") sys.exit() elif ((int(splitLine[1]) == 13) or (int(splitLine[1]) % 2 != 1) or (int(splitLine[1]) > 25) or (int(splitLine[1]) < 1)): print( "Keycode line: " + str(i + 1) + ": Affine cipher detected on keycode line " + str(i) + " attempting to use an a value outside of the range of 1-25, that is even, or that is 13, all of which are not permitted." ) sys.exit() else: originalPlaintext = plaintextContents if (debug): print("Keycode line: " + str(i + 1) + ": Affine cipher detected with an a value of " + splitLine[1] + " and a b value of " + splitLine[2] + ".") if (encryptionDirection == "encrypt"): plaintextContents = Affine( int(splitLine[1]), int(splitLine[2])).encipher(plaintextContents) else: plaintextContents = Affine( int(splitLine[1]), int(splitLine[2])).decipher(plaintextContents) if (debug): print("Keycode line: " + str(i + 1) + ": Affine ciphered " + originalPlaintext + " by value a " + splitLine[1] + " and value b " + splitLine[2] + " with a result of " + plaintextContents + ".") elif ( splitLine[0] == "autokey" ): #TODO: The autokey cipher actually doesn't have any requirements for the key, but will be configured to set off a ton of warnings assuming the config flags allow for it. originalPlaintext = plaintextContents if (debug): print("Keycode line: " + str(i + 1) + ": Autokey cipher detected with an key of " + splitLine[1] + ".") if (encryptionDirection == "encrypt"): plaintextContents = Autokey( splitLine[1]).encipher(plaintextContents) else: plaintextContents = Autokey( splitLine[1]).decipher(plaintextContents) if (debug): print("Keycode line: " + str(i + 1) + ": Autokey ciphered " + originalPlaintext + " by key of " + splitLine[1] + " for a result of " + plaintextContents + ".") elif (splitLine[0] == "atbash"): originalPlaintext = plaintextContents if (debug): print("Keycode line: " + str(i + 1) + ": Autokey cipher detected.") if (encryptionDirection == "encrypt"): plaintextContents = Affine(25, 25).encipher(plaintextContents) else: plaintextContents = Affine(25, 25).decipher(plaintextContents) if (debug): print("Keycode line: " + str(i + 1) + ": Atbash ciphered " + originalPlaintext + " for a result of " + plaintextContents + ".") elif (splitLine[0] == "beaufort"): if (type(splitLine[1] == str)): originalPlaintext = plaintextContents if (debug): print("Keycode line: " + str(i + 1) + ": Beaufort shift detected with an argument of " + splitLine[1] + ".") if (encryptionDirection == "encrypt"): plaintextContents = Beaufort( splitLine[1]).encipher(plaintextContents) else: plaintextContents = Beaufort( splitLine[1]).decipher(plaintextContents) if (debug): print("Keycode line: " + str(i + 1) + ": Beaufort shifted " + originalPlaintext + " by " + splitLine[1] + " with a result of " + plaintextContents + ".") else: print("Keycode line: " + str(i + 1) + ": Beaufort shift detected on keycode line " + str(i) + " attempting to use key that is not a string.") elif (splitLine[0] == "bifid"): if ( len(splitLine[1]) != 25 ): # This makes sure that the keysquare's length is exactly 25. print( "Keycode line: " + str(i + 1) + ": Bifid cipher detected on keycode line " + str(i) + " attempting to use keysquare that is not 25 characters long." ) sys.exit() else: originalPlaintext = plaintextContents if (debug): print("Keycode line: " + str(i + 1) + ": Bifid cipher detected with a keysquare of " + splitLine[1] + " and a keyword of " + splitLine[2] + ".") if (encryptionDirection == "encrypt"): plaintextContents = Bifid(splitLine[1], int( splitLine[2])).encipher(plaintextContents) else: plaintextContents = Bifid(splitLine[1], int( splitLine[2])).decipher(plaintextContents) if (debug): print("Keycode line: " + str(i + 1) + ": Bifid ciphered " + originalPlaintext + " by a keysquare of " + splitLine[1] + " and a keyword of " + splitLine[2] + " with a result of " + plaintextContents + ".") elif (splitLine[0] == "coltrans"): if (type(splitLine[1] != str) ): # Check that the encryption key is a string. originalPlaintext = plaintextContents if (debug): print( "Keycode line: " + str(i + 1) + ": Columnar transposition shift detected with an argument of " + splitLine[1] + ".") if (encryptionDirection == "encrypt"): plaintextContents = ColTrans( splitLine[1]).encipher(plaintextContents) else: plaintextContents = ColTrans( splitLine[1]).decipher(plaintextContents) if (debug): print("Keycode line: " + str(i + 1) + ": Columnar transposition shifted " + originalPlaintext + " by " + splitLine[1] + " with a result of " + plaintextContents + ".") else: print( "Keycode line: " + str(i + 1) + ": Columnar transposition shift detected on keycode line " + str(i) + " attempting to use key that is not a string.") elif (splitLine[0] == "foursquare"): if ( len(splitLine[1]) != 25 ): # This makes sure that the keysquare's length is exactly 25. print( "Foursquare cipher detected on keycode line " + str(i) + " attempting to use keysquare that is not 25 characters long." ) sys.exit() elif (len(splitLine[2]) != 25): print( "Foursquare cipher detected on keycode line " + str(i) + " attempting to use keysquare that is not 25 characters long." ) sys.exit() else: originalPlaintext = plaintextContents if (debug): print("Foursquare cipher detected with a keysquare of " + splitLine[1] + " and a keyword of " + splitLine[2] + ".") if (encryptionDirection == "encrypt"): plaintextContents = Foursquare( key1=splitLine[1], key2=splitLine[2]).encipher(plaintextContents) else: plaintextContents = Foursquare( key1=splitLine[1], key2=splitLine[2]).decipher(plaintextContents) if (debug): print("Foursquare ciphered " + originalPlaintext + " by a keysquare of " + splitLine[1] + " and a keyword of " + splitLine[2] + " with a result of " + plaintextContents + ".") elif (splitLine[0] == "playfair"): if ( len(splitLine[1]) != 25 ): # This makes sure that the keysquare's length is exactly 25. print( "Keycode line: " + str(i + 1) + ": Playfair cipher detected on keycode line " + str(i) + " attempting to use keysquare that is not 25 characters long." ) sys.exit() else: originalPlaintext = plaintextContents if (encryptionDirection == "encrypt"): plaintextContents = Playfair( splitLine[1]).encipher(plaintextContents) else: plaintextContents = Playfair( splitLine[1]).decipher(plaintextContents) if (debug): print("Keycode line: " + str(i + 1) + ": Playfair ciphered " + originalPlaintext + " by a keysquare of " + splitLine[1] + " with a result of " + plaintextContents + ".") elif ( splitLine[0] == "railfence" ): #TODO: Fix this so that it throws an error if the key is a bad length relative to the plaintext. if (splitLine[1].isdigit() == False): print("Keycode line: " + str(i + 1) + ": Railfence cipher detected on keycode line " + str(i) + " with a non-numerical key.") sys.exit() elif ( int(splitLine[1]) < 1 ): # This makes sure that the keysquare's length is exactly 25. print("Keycode line: " + str(i + 1) + ": Railfence cipher detected on keycode line " + str(i) + " attempting to use a key less than 0.") sys.exit() else: originalPlaintext = plaintextContents if (encryptionDirection == "encrypt"): plaintextContents = Railfence(int( splitLine[1])).encipher(plaintextContents) else: plaintextContents = Railfence(int( splitLine[1])).decipher(plaintextContents) if (debug): print("Keycode line: " + str(i + 1) + ": Railfence ciphered " + originalPlaintext + " by a key of " + splitLine[1] + " with a result of " + plaintextContents + ".") elif (splitLine[0] == "rot13"): originalPlaintext = plaintextContents if (debug): print("Keycode line: " + str(i + 1) + ": Rot13 cipher detected.") if (encryptionDirection == "encrypt"): plaintextContents = Rot13().encipher(plaintextContents) else: plaintextContents = Rot13().decipher(plaintextContents) if (debug): print("Keycode line: " + str(i + 1) + ": Rot13 ciphered " + originalPlaintext + " with a result of " + plaintextContents + ".") elif (splitLine[0] == "simplesub"): if ( len(splitLine[1]) != 26 ): # This makes sure that the keysquare's length is exactly 25. print( "Keycode line: " + str(i + 1) + ": Simple substitution cipher detected on keycode line " + str(i) + " attempting to use key that is not 26 characters long.") sys.exit() else: originalPlaintext = plaintextContents if (debug): print( "Keycode line: " + str(i + 1) + ": Simple substitution cipher detected with a key of " + splitLine[1] + ".") if (encryptionDirection == "encrypt"): plaintextContents = SimpleSubstitution( splitLine[1]).encipher(plaintextContents) else: plaintextContents = SimpleSubstitution( splitLine[1]).decipher(plaintextContents) if (debug): print("Keycode line: " + str(i + 1) + ": Simple substitution ciphered " + originalPlaintext + " by a key of " + splitLine[1] + " with a result of " + plaintextContents + ".") if (i == (len(keycodeLines) - 1)): # print(plaintextContents) #A debug catch that you may find useful later. return plaintextContents
from pycipher import Bifid msg = 'snbwmuotwodwvcywfgmruotoozaiwghlabvuzmfobhtywftopmtawyhifqgtsiowetrksrzgrztkfctxnrswnhxshylyehtatssukfvsnztyzlopsv' keys = ['mrocktvquizphdbagsfewlynx', 'ocknymphswaqfdrugvexblitz', 'crwthvoxzapsqigymfeldbunk', 'hmfordwaltzcinqbuskpyxveg', 'phavfyxbugstonqmilkzdcrew', 'hesaidbcfgklmnopqrtuvwxyz', 'enqvahlbidgumkrwcfpostxyz', 'emilyqungschwarzkopfxtvbd', 'ohnfezcamrwsputyxigkqblvd', 'qtipforsuvnzxylemdcbaghwk', 'umblingvextfrowzyhackspdq', 'qvandzstruckmybigfoxwhelp', 'lumpydrabcgqvzinksfoxthew', 'heyiamnopqrstuvwxzbcdfgkl', 'quizvbmwlynxstockderpaghf', 'pledbigczarunksmyvwfoxthq', 'waltzgbquickfordsvexnymph', 'qwertyuioplkhgfdsazxcvbnm', 'zyxwvutsrqponmlkihgfedcba', 'aquickbrownfxmpsvethlzydg', ] for i in range(len(keys)): key = keys[len(keys)-i-1] msg = Bifid(key,5).decipher(msg) print(str.lower(msg).replace('x', ' '))
def bifidencode(importx, infilepath, outfilepath, inputformat, exportx, raw, key, n): if importx == 'file': f = open(infilepath, 'r') raw = f.read() f.close() elif importx == 'print': raw = raw else: print('\033[1;31m[-]\033[0m Unknown error.') return False inp = raw if inputformat == 'base64': iput = base64.b64decode(inp) elif inputformat == 'raw': iput = inp elif inputformat == 'base32': iput = base64.b32decode(inp) elif inputformat == 'base16': iput = base64.b16decode(inp) elif inputformat == 'base58': iput = base58.b58decode(inp) elif inputformat == 'base85': print('\033[1;31m[-]\033[0m Option not available yet') elif inputformat == 'hex': iput = inp.decode('hex') elif inputformat == 'dec': print('\033[1;31m[-]\033[0m Option not available yet') elif inputformat == 'octal': print('\033[1;31m[-]\033[0m Option not available yet') elif inputformat == 'binary': iput = text_from_bits(inp) else: print('\033[1;31m[-]\033[0m Unknown error.') return False output = Bifid(key, n).encipher(iput) if exportx == 'file': f = open(outfilepath, 'w') f.write(output) f.close() return True elif exportx == 'print': return output else: print('\033[1;31m[-]\033[0m Unknown error.') return False
#opening the given file and creating a list with open("ramblings", "r") as file: pangrams = file.readlines() for pangram in pangrams: #removing punctuation, spaces, new lines, and "j" as Bifid ciphers do not involve j x = pangram.lower().translate(str.maketrans( "", "", punctuation)).replace(" ", "").replace("\n", "").replace("j", "") rambls.append(x) #as the challenge name(difib=>reverse of bifid) suggest the real stuff in this challenge is to reverse the key for rambl in rambls[::-1]: #the keys should be less than 25 characters without j if len(rambl) < 26: #Bifid(<key>, <period(box grid)>).decipher(<cipher>) message = Bifid(rambl, 5).decipher(message).lower() #consider x as spaces print( message.replace("x", " ") ) #================> ust some unnecessary te t that holds absolutely no meaning whatsoever and bears no significance to you in any way #so by guessing we can arrive at "just some unnecessary text that holds absolutely no meaning whatsoever and bears no significance to you in any way" #submit to the gaurd and get the flag \0/ connection = remote("crypto.chal.csaw.io", "5004") connection.sendlineafter( "!", "just some unnecessary text that holds absolutely no meaning whatsoever and bears no significance to you in any way" ) print(connection.recvall())