Ejemplo n.º 1
0
def main(argv):
    if len(argv) > 2:
        die("Too many arguments.", show_usage=True)
    try:
        opts, args = getopt.getopt(argv, "h", ["help"])
    except getopt.GetoptError:
        usage()
        sys.exit(2)
    for opt in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit()

    key = False
    source = False
    destination = False

    if len(args) > 0:
        source = args[0]
        if len(args) > 1:
            destination = args[1]
    else:
        usage()
        sys.exit()

    while not key:
        k = raw_input(
            "Enter the key to use for decryption. If the key is unknown, press enter, and the program will attempt to find the key for you: "
        )
        if k == '':
            key = None
            break
        try:
            k = int(k)
        except ValueError:
            print "Invalid input (enter an integer between 0 and 25)"
            continue
        if (0 <= int(k) <= 25):
            key = int(k)
        else:
            print "Invalid input (enter an integer between 0 and 25)"

    cipher = load_file(source)
    if not cipher:
        cipher = source

    if cipher:
        plain = caesar.decipher(cipher, key)
    else:
        die("Could not determine source.")
    if not plain:
        die("Could not decipher.")
    if destination:
        save_to_file(destination, plain)
    else:
        print plain
Ejemplo n.º 2
0
def test_1():
    """Test every three-character-long combination of printables for every 4th key. Return True if success, False if failure.

    """
    for word in [a + b + c for a in string.printable for b in string.printable for c in string.printable]:
        for i in range(0, 26, 4):
            if caesar.decipher(caesar.encipher(word, i), i) != word:
                print "TEST 1 FAILED"
                return False
    print "TEST 1 SUCCEEDED"
    return True
Ejemplo n.º 3
0
def main(argv):
    if len(argv) > 2:
        die("Too many arguments.", show_usage=True)
    try:
        opts, args = getopt.getopt(argv, "h", ["help"])
    except getopt.GetoptError:
        usage()
        sys.exit(2)
    for opt in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit()

    key = False
    source = False
    destination = False

    if len(args) > 0:
        source = args[0]
        if len(args) > 1:
            destination = args[1]
    else:
        usage()
        sys.exit()

    while not key:
        k = raw_input("Enter the key to use for decryption. If the key is unknown, press enter, and the program will attempt to find the key for you: ")
        if k == '':
            key = None
            break
        try:
            k = int(k)
        except ValueError:
            print "Invalid input (enter an integer between 0 and 25)"
            continue
        if (0 <= int(k) <= 25):
            key = int(k)
        else:
            print "Invalid input (enter an integer between 0 and 25)"

    cipher = load_file(source)
    if not cipher:
        cipher = source

    if cipher:
        plain = caesar.decipher(cipher, key)
    else:
        die("Could not determine source.")
    if not plain:
        die("Could not decipher.")
    if destination:
        save_to_file(destination, plain)
    else:
        print plain
Ejemplo n.º 4
0
def test_3():
    """Test random key encipherment, and unknown key decipherment."""
    words = caesar.get_words()
    random.shuffle(words)
    plain = ''
    for i in range(100):
        plain += words[i]
        plain += ' '
    if caesar.decipher(caesar.encipher(plain)) != plain:
        print "TEST 3 FAILED"
        return False
    print "TEST 3 SUCCEEDED"
    return True
Ejemplo n.º 5
0
def test_3():
    """Test random key encipherment, and unknown key decipherment."""
    words = caesar.get_words()
    random.shuffle(words)
    plain = ""
    for i in range(100):
        plain += words[i]
        plain += " "
    if caesar.decipher(caesar.encipher(plain)) != plain:
        print "TEST 3 FAILED"
        return False
    print "TEST 3 SUCCEEDED"
    return True
def test_1():
    """Test every three-character-long combination of printables for every 4th key. Return True if success, False if failure.
    """
    for word in [
            a + b + c for a in string.printable for b in string.printable
            for c in string.printable
    ]:
        for i in range(0, 26, 4):
            if caesar.decipher(caesar.encipher(word, i), i) != word:
                print "TEST 1 FAILED"
                return False
    print "TEST 1 SUCCEEDED"
    return True
Ejemplo n.º 7
0
def test_2():
    """Test a long random string.  Return True if success, False if failure."""
    long_string = ''
    numbers = range(100)
    random.shuffle(numbers)
    j = 0
    for i in range(100001):
        if j == 99:
            random.shuffle(numbers)
            j = 0
        long_string += string.printable[numbers[0]]
        j += 1
    for i in range(26):
        if caesar.decipher(caesar.encipher(long_string, i), i) == long_string:
            print "TEST 2 SUCCEEDED"
            return True
    print "TEST 2 FAILED"
    return False
Ejemplo n.º 8
0
def test_2():
    """Test a long random string.  Return True if success, False if failure."""
    long_string = ""
    numbers = range(100)
    random.shuffle(numbers)
    j = 0
    for i in range(100001):
        if j == 99:
            random.shuffle(numbers)
            j = 0
        long_string += string.printable[numbers[0]]
        j += 1
    for i in range(26):
        if caesar.decipher(caesar.encipher(long_string, i), i) == long_string:
            print "TEST 2 SUCCEEDED"
            return True
    print "TEST 2 FAILED"
    return False
Ejemplo n.º 9
0
try:
    while True:
        messages = vk.method('messages.getConversations', {'offset': 0, 'count': 20, 'filter': 'unread'})
        if messages['count'] > 0:
            id = messages['items'][0]['last_message']['from_id']
            body = messages['items'][0]['last_message']['text']  # текст сообщения
            body = list(str(body.lower()).split()) 
            if len(body) != 0:
                if body[0] in module.caesar:
                   if len(body) >  3:
                        try:
                           if body[1] in module.cipher:
                               send(caesar.cipher(str(body[3:]), int(body[2])), id)

                           elif body[1] in module.decipher:
                               send(caesar.decipher(str(body[3:]), int(body[2])), id)

                           else:
                               send(module.caesar_help, id)

                        except:
                            send(module.caesar_help, id)

                   else:
                       send(module.caesar_help, id)

                elif body[0] in module.cipher:
                    send(module.caesar_help, id)

                elif body[0] in module.vigenere:
                    if len(body) >  3 and (alphabet.lang(body[2]) == 'rus' or alphabet.lang(body[2]) == 'eng'):