Esempio n. 1
0
 def test_encipher(self):
     keys = (('ypgknveqzxmdhwcblfisrauto','GERMAN'),
             ('haoqkzdmpieslycbwvgufntrx','CIPHERS'))
     plaintext = ('abcdefghiiklmnopqrstuvwxyzabcdefghiiklmnopqrstuvwxyz',
                  'abcdefghiiklmnopqrstuvwxyzabcdefghiiklmnopqrstuvwxyz')
     ciphertext = ('FGGGXXXDXFAGFAGDADDFGADXAAADFGXFGGGXFAGFAGDAGDFAADXFDADFGXFGGGXFGDXAFXXFGDXAFXDDFGADXAAGDFAADXFDFGGGXXXD',
                   'ADAXAGGGFADFAFXFADFAFXDFAFFFFDAXXDGXDADAXAGGGDFAFFFFDXGXXGGXGADFDDGADGADXXFXGXXGGXGADFDDGADGADXXFAXXDGXD')
     for i,key in enumerate(keys):
         enc = ADFGX(*key).encipher(plaintext[i])
         self.assertEqual(enc.upper(), ciphertext[i].upper())
Esempio n. 2
0
 def test_encipher(self):
     keys = (('ypgknveqzxmdhwcblfisrauto', 'GERMAN'),
             ('haoqkzdmpieslycbwvgufntrx', 'CIPHERS'))
     plaintext = ('abcdefghiiklmnopqrstuvwxyzabcdefghiiklmnopqrstuvwxyz',
                  'abcdefghiiklmnopqrstuvwxyzabcdefghiiklmnopqrstuvwxyz')
     ciphertext = (
         'FGGGXXXDXFAGFAGDADDFGADXAAADFGXFGGGXFAGFAGDAGDFAADXFDADFGXFGGGXFGDXAFXXFGDXAFXDDFGADXAAGDFAADXFDFGGGXXXD',
         'ADAXAGGGFADFAFXFADFAFXDFAFFFFDAXXDGXDADAXAGGGDFAFFFFDXGXXGGXGADFDDGADGADXXFXGXXGGXGADFDDGADGADXXFAXXDGXD'
     )
     for i, key in enumerate(keys):
         enc = ADFGX(*key).encipher(plaintext[i])
         self.assertEqual(enc.upper(), ciphertext[i].upper())
Esempio n. 3
0
def adfgx_de(Val, key):
    flag = ADFGX('phqgmeaynofdxkrcvszwbutil', key).decipher(Val)
    cprint('ADFGX密文结果为:' + Val, 'yellow')
    cprint('ADFGX密钥为:' + key, 'yellow')
    cprint('ADFGX解密后结果为:' + flag, 'yellow')
Esempio n. 4
0
def adfgx_en(Val, key):
    flag = ADFGX('phqgmeaynofdxkrcvszwbutil', key).encipher(Val)
    cprint('ADFGX明文结果为:' + Val, 'yellow')
    cprint('ADFGX密钥为:' + key, 'yellow')
    cprint('ADFGX加密后结果为:' + flag, 'yellow')
Esempio n. 5
0
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
Esempio n. 6
0
 def decipher(self):
     return ADFGX(self.key, self.keyword).decipher(self.text)
Esempio n. 7
0
 def encipher(self):
     return ADFGX(self.key, self.keyword).encipher(self.text)
Esempio n. 8
0
def ADFGXencode(importx, infilepath, outfilepath, inputformat, exportx, key,
                keyword, raw):

    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 = ADFGX(key, keyword).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
Esempio n. 9
0
from pycipher import ADFGX

key = 'phqgmeaylnofdxkrcvszwbuti'
keyword = 'HELLO'
plaintext = "Attack at once"
ciphertext = ADFGX(key, keyword).encipher(plaintext)
print(ciphertext)
plaintext = ADFGX(key, keyword).decipher(ciphertext)
print(plaintext)