Example #1
0
    def random_encrypt(self):

        # randomly choose plaintext message from lines 0-9 in w8.txt
        line_num = os.urandom(1)[0] % 10
        f = open("w8.txt")
        message = f.readlines()[line_num]

        # un-base64 and encrypt message
        message = base64.b64decode(message)
        c_text = encrypt_cbc(message, self.key, self.iv)

        return c_text, self.iv
Example #2
0
def sanitizer(p):

    # concatenate input string with prepend and append strings
    prepend = "comment1=raining%20MCs;userdata="
    append = ";comment2=%20like%20a%20sunny%20day%20tomorrow"
    s = prepend + p + append

    # quote out ";" and "=" characters
    s = s.replace(";", "")
    s = s.replace("=", "")
    print("Sanitized plaintext: " + s)

    # return encrypted string
    c = encrypt_cbc(my_encode(s), key, iv)
    return c
Example #3
0
 def encrypt(self, plaintext):
     c_text = encrypt_cbc(plaintext, self.key, self.iv)
     return c_text, self.iv
Example #4
0
    b_array = bytearray(my_encode(plaintext))

    # randomly prepend 5-10 bytes
    random_prepend = os.urandom(1)
    num_prepend_bytes = 5 + random_prepend[0] % 6   # noOfPrependBytes
    for i in range(0, num_prepend_bytes):
        b_array.append(os.urandom(1)[0])

    # randomly append 5-10 bytes
    random_append = os.urandom(1)
    num_append_bytes = 5 + random_append[0] % 6     # noOfAppendBytes
    for j in range(0, num_append_bytes):
        b_array.insert(0, os.urandom(1)[0])

    # create modified plaintext
    plaintext = bytes(b_array)

    # encrypt
    if mode == 0:
        ciphertext = encrypt_ecb(plaintext, random_key, True)
    else:
        random_iv = os.urandom(16)
        ciphertext = bytes(my_encode(encrypt_cbc(plaintext, random_key, random_iv)))
        # ciphertext = bytes(test_encrypt(plaintext, random_key, random_iv))

    # print detected mode
    if detect_ecb(binascii.hexlify(ciphertext)):
        print("ECB mode detected")
    else:
        print("CBC mode detected")
Example #5
0
 def bad_encrypt(self, plaintext):
     return bytes(encrypt_cbc(plaintext, self.key, self.bad_iv), 'latin1')
Example #6
0
    # randomly prepend 5-10 bytes
    random_prepend = os.urandom(1)
    num_prepend_bytes = 5 + random_prepend[0] % 6  # noOfPrependBytes
    for i in range(0, num_prepend_bytes):
        b_array.append(os.urandom(1)[0])

    # randomly append 5-10 bytes
    random_append = os.urandom(1)
    num_append_bytes = 5 + random_append[0] % 6  # noOfAppendBytes
    for j in range(0, num_append_bytes):
        b_array.insert(0, os.urandom(1)[0])

    # create modified plaintext
    plaintext = bytes(b_array)

    # encrypt
    if mode == 0:
        ciphertext = encrypt_ecb(plaintext, random_key, True)
    else:
        random_iv = os.urandom(16)
        ciphertext = bytes(
            my_encode(encrypt_cbc(plaintext, random_key, random_iv)))
        # ciphertext = bytes(test_encrypt(plaintext, random_key, random_iv))

    # print detected mode
    if repeated_block(binascii.hexlify(ciphertext)):
        print("ECB mode detected")
    else:
        print("CBC mode detected")