def test_encipher(self): keys = ('GERMAN', 'CIPHERS') plaintext = ('abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz') ciphertext = ('gdpjwiaxjdqcurdxkwolxreqifrlykczlfsewtfzmyqnztgskhtn', 'chneammvagxtffotzqmyyhmsjfrraflcykktyevrddmrxokwwfkq') for i, key in enumerate(keys): enc = Beaufort(key).encipher(plaintext[i]) self.assertEqual(enc.upper(), ciphertext[i].upper())
def test_encipher(self): keys = ('GERMAN', 'CIPHERS') plaintext = ('abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz') ciphertext = ('gdpjwiaxjdqcurdxkwolxreqifrlykczlfsewtfzmyqnztgskhtn', 'chneammvagxtffotzqmyyhmsjfrraflcykktyevrddmrxokwwfkq') for i,key in enumerate(keys): enc = Beaufort(key).encipher(plaintext[i]) self.assertEqual(enc.upper(), ciphertext[i].upper())
def beaufortDecipher(key, message): decipher = '' for letter in message: if (letter != ' '): decipher += Beaufort(key).decipher(letter) else: decipher += ' ' return decipher
def beaufortCipher(key, message): cipher = '' for letter in message: if (letter != ' '): cipher += Beaufort(key).encipher(letter) else: cipher += ' ' return cipher
def BeaufortBF(ctext, dictionary): f = open(dictionary, 'r') keys = f.readlines() for i in keys: try: out = Beaufort(i[:-1]).decipher(ctext) print('\033[1;34m[*]\033[0m Key = ' + i[:-1] + ' ; Out = ' + out) except: print('\033[1;34m[*]\033[0m Key = ' + i[:-1] + ' ; Err.: KeyError') print('\033[1;32m[+]\033[0m Brute Force finished.')
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
def decode(self, message, key): try: return Beaufort(key).decipher(message) except: return None
from pycipher import Beaufort from pycipher import Vigenere from pycipher import SimpleSubstitution from pycipher import Playfair from pycipher import Railfence from pycipher import Enigma print(Beaufort('NEU').encipher("WANGWENJIE")) # 加密:密钥 ,明文 # print(Beaufort('GIRL').decipher("YVYHVXJFCVY")) # 解密:密钥 ,密文 # print(Playfair().encipher("")) # print(Playfair().decipher("")) # print(Vigenere('hanxu').encipher('')) #加密 # print(Vigenere('NEU').decipher('qinrgnvsh fcmgig')) #解密 # print(SimpleSubstitution('hanxu').encipher('')) # print(SimpleSubstitution('hanxu').decipher('')) # print(Railfence().encipher('')) # print(Railfence().decipher(''))
def beaufortencode(importx, infilepath, outfilepath, inputformat, export, raw, key): 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 = Beaufort(key).encipher(iput) if export == 'file': f = open(outfilepath, 'w') f.write(output) f.close() return True elif export == 'print': return output else: print('\033[1;31m[-]\033[0m Unknown error.') return False