Example #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 encryption. If you do not enter a key, one will be chosen for you at random: "
        )
        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)"

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

    if plain:
        cipher = caesar.encipher(plain, key)
    else:
        die("Could not determine source.")
    if not cipher:
        die("Could not encipher.")
    if destination:
        save_to_file(destination, cipher)
    else:
        print cipher
Example #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
Example #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 encryption. If you do not enter a key, one will be chosen for you at random: ")
        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)"

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

    if plain:
        cipher = caesar.encipher(plain, key)
    else:
        die("Could not determine source.")
    if not cipher:
        die("Could not encipher.")
    if destination:
        save_to_file(destination, cipher)
    else:
        print cipher
Example #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
Example #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
Example #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
Example #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