Example #1
0
    def encrypt(self, plaintext):
        AES.encrypt(self, plaintext)

        plaintextBytes = stringToBytes(plaintext)
        chainBytes = stringToBytes(self.IV)

        #CBC Mode: For each block...
        for x in range(len(plaintextBytes)/16):

            #XOR with the chaining block
            blockBytes = plaintextBytes[x*16 : (x*16)+16]
            for y in range(16):
                blockBytes[y] ^= chainBytes[y]
            blockString = bytesToString(blockBytes)

            #Encrypt it
            encryptedBytes = stringToBytes(self.rijndael.encrypt(blockString))

            #Overwrite the input with the output
            for y in range(16):
                plaintextBytes[(x*16)+y] = encryptedBytes[y]

            #Set the next chaining block
            chainBytes = encryptedBytes

        self.IV = bytesToString(chainBytes)
        return bytesToString(plaintextBytes)
Example #2
0
    def encrypt(self, plaintext):
        AES.encrypt(self, plaintext)

        plaintextBytes = stringToBytes(plaintext)
        chainBytes = stringToBytes(self.IV)

        #CBC Mode: For each block...
        for x in range(len(plaintextBytes) / 16):

            #XOR with the chaining block
            blockBytes = plaintextBytes[x * 16:(x * 16) + 16]
            for y in range(16):
                blockBytes[y] ^= chainBytes[y]
            blockString = bytesToString(blockBytes)

            #Encrypt it
            encryptedBytes = stringToBytes(self.rijndael.encrypt(blockString))

            #Overwrite the input with the output
            for y in range(16):
                plaintextBytes[(x * 16) + y] = encryptedBytes[y]

            #Set the next chaining block
            chainBytes = encryptedBytes

        self.IV = bytesToString(chainBytes)
        return bytesToString(plaintextBytes)
Example #3
0
def encrypt(input_message, user_id):

    master_key = long(user_id)
    print master_key
    a = AES(master_key)
    print a.encrypt(int(input_message.encode("hex")))
    aes_key_cipher = rsa_encrypt(user_id, n, e, 15)
    print aes_key_cipher
Example #4
0
 def encrypt_message(self, message):
     """
     Encrypt a block of text.
     Currently testing with AES
     """
     cipher_text = AES.encrypt(self._pad_message(message))
     image = self._generate_image(cipher_text)
     return image
Example #5
0
 def encrypt_message(self, message, image_path, filename, sender_token):
     """Encrypt a block of text.
     Currently testing with AES and truncating to 250 characters.
     """
     sender_token = self.db.users.find_one({'token':sender_token})
     if sender_token:
         cipher_text = AES.encrypt(self._pad_message(
                 unicode(message[:250]).encode('utf-8')))
         image = self._generate_image(cipher_text, image_path,
                 filename, sender_token['token'])
         return image
     else:
         return False
Example #6
0
def encrypt_msg(target):
    '''
	Input: Clients message, in this case the tuple obtained from select_page
	Output: Encrypted message that is sent to the first node
	Uses each key ones to encrypt the message
	Adds ### betweem the tuples object, plaint text messages split from those
	'''
    msg = target[0] + "###" + target[1]
    for key in KEYS:
        #print(msg)
        msg = AES.encrypt(msg, key)
    print(msg)
    return msg
Example #7
0
 def encrypt_message(self, message, image_path, filename, sender_token):
     """Encrypt a block of text.
     Currently testing with AES and truncating to 250 characters.
     """
     sender_token = self.db.users.find_one({'token': sender_token})
     if sender_token:
         cipher_text = AES.encrypt(
             self._pad_message(unicode(message[:250]).encode('utf-8')))
         image = self._generate_image(cipher_text, image_path, filename,
                                      sender_token['token'])
         return image
     else:
         return False
Example #8
0
def aes_cbc_encrypt(ptext, key, iv=b"\x00" * 16):
    if HAVE_CRYPTO:
        # `AES.MODE_ECB` isn't a typo; we're implementing CBC by hand.
        cipher = AES.new(key, AES.MODE_ECB)
        ptext, ctext, ct_block = pad_pkcs7(ptext), bytearray(), iv

        for i in range(0, len(ptext), 16):
            pt_block = repeating_key_xor(ptext[i:i + 16], ct_block)
            ct_block = cipher.encrypt(pt_block)
            ctext.extend(ct_block)

        return bytes(ctext)
    else:
        cipher = AES(key, mode="CBC", iv=iv)
        return cipher.encrypt(pad_pkcs7(ptext))
Example #9
0
def aes_ctr(bstream, key, nonce=0):
    nonce = aes_ctr_pack(nonce)

    if HAVE_CRYPTO:
        # `AES.MODE_ECB` isn't a typo; we're implementing CTR by hand.
        cipher = AES.new(key, mode=AES.MODE_ECB)
    else:
        cipher = AES(key, mode="ECB")

    result = bytearray()

    for count, i in enumerate(range(0, len(bstream), 16)):
        key_block = cipher.encrypt(nonce + aes_ctr_pack(count))
        result.extend(kb ^ sb for kb, sb in zip(key_block, bstream[i:i + 16]))

    return bytes(result)
Example #10
0
def performanceEncrypt(name: str, payload: bytearray, cipher: AES, run: int):
    # out = "Encrypt ... "
    # p.start()
    start = time.time()
    for i in range(0, 5000):
        encrypted = cipher.encrypt(payload)
    elapsed = time.time() - start
    # p.stop()
    # p.save('profile_encrypt_' + name)

    data["Crypt"].append("{:.10f}".format(elapsed / 5000.0))

    # out += "{:.10f}".format(elapsed / (5000.0 * 16.0))
    # out += "us per byte, "
    # out += "{:.10f}".format((16.0 * 5000.0 * 1000000.0) / elapsed)
    # out += " bytes per second"
    # print(out)
    return encrypted
Example #11
0
    def aes_encipher_block(self, key, block):

        #print('KEY',key)
        key=key[0].to_bytes(4,'big')+ \
            key[1].to_bytes(4,'big')+ \
            key[2].to_bytes(4,'big')+ \
            key[3].to_bytes(4,'big')
        #print('key',binascii.hexlify(key))
        if os.__name__ == 'os':
            my_aes = AES.new(key, AES.MODE_ECB)
        else:
            my_aes = AES(key, AES.MODE_ECB)
        result = my_aes.encrypt(block[0].to_bytes(4, 'big') +
                                block[1].to_bytes(4, 'big') +
                                block[2].to_bytes(4, 'big') +
                                block[3].to_bytes(4, 'big'))
        result = (int.from_bytes(result[0:4],
                                 'big'), int.from_bytes(result[4:8], 'big'),
                  int.from_bytes(result[8:12],
                                 'big'), int.from_bytes(result[12:16], 'big'))

        #print('result type',type(result))
        return result
Example #12
0
def __encode(aes: AES, clear_text: bytes) -> bytes:
    return aes.encrypt(__pad(clear_text))
Example #13
0
        block = xor(block, self.get_round_key(0))
        return block


def aes128_reverse_keyschedule(key, round):
    for i in range(round, 0, -1):
        for j in range(15, 3, -1):
            key[j] ^= key[j - 4]
        for j in range(3, -1, -1):
            key[j] ^= aes_sbox[key[12 +
                                   (j + 1) % 4]] ^ (0 if j else aes_Rcon[i])
    return key


if __name__ == '__main__':
    print 'Running test...'
    import Crypto.Cipher.AES

    key = ''.join(map(chr, range(16)))
    plain = ''.join(map(chr, range(16, 32)))
    assert len(key) == len(plain) == 16

    cipher = AES(key)
    cipher2 = Crypto.Cipher.AES.new(key)
    assert (list(cipher.encrypt(plain)) == map(ord, cipher2.encrypt(plain)))
    assert (list(cipher.decrypt(plain)) == map(ord, cipher2.decrypt(plain)))

    for round in range(11):
        assert aes128_reverse_keyschedule(cipher.get_round_key(round),
                                          round) == ary(key)
Example #14
0
 def encrypt(self, plaintext):
     AES.encrypt(self, plaintext)
     plaintext = bytes(plaintext)
     return bytearray(self.context.encrypt(plaintext))
Example #15
0
    def encrypt( self, raw ):
        raw = pad(raw)
        iv = Random.new().read( AES.block_size )
        cipher = AES.new( self.key, AES.MODE_CBC, iv )
        return base64.b64encode( iv + cipher.encrypt( raw ) ) 

    def decrypt( self, enc ):
        enc = base64.b64decode(enc)
        iv = enc[:16]
        cipher = AES.new(self.key, AES.MODE_CBC, iv )
        return unpad(cipher.decrypt( enc[16:] ))

# Making a zip file of the `OpenSource` folder
shutil.make_archive('SourceCode.zip', 'zip', 'OpenSource')

# Reading the zip file
with open('SourceCode.zip', 'rb') as f:
    RawContent = f.read()

# Deleting the zip file
os.remove('SourceCode.zip')

# Encrypting the zip file
AES = AESCipher('MinecraftServerForeverAtFluxGamming-')
EncryptedRaw = AES.encrypt(RawContent)

# Saving the encrypted file
with open('SourceCode.zip.aes256', 'wb+') as f:
    f.write(EncryptedRaw)
Example #16
0
def aes_ecb_encrypt(ptext, key):
    if HAVE_CRYPTO:
        cipher = AES.new(key, mode=AES.MODE_ECB)
    else:
        cipher = AES(key, mode="ECB")
    return cipher.encrypt(pad_pkcs7(ptext))
Example #17
0
from Crypto.Cipher import AES

AES = AES.new('AngeCryptionKey!', AES.MODE_CBC,
              'x\xd0\x02\x81k\xa7\xc3\xde\x88\xdeV\x8fjY\x1d\x06')

with open('angecrypted.png', "rb") as f:
    d = f.read()

d = AES.encrypt(d)

with open("encrypted.png", "wb") as f:
    f.write(d)
Example #18
0
from Crypto.Cipher import AES

AES = AES.new('AngeCryptionKey!', AES.MODE_CBC, 'x\xd0\x02\x81k\xa7\xc3\xde\x88\xdeV\x8fjY\x1d\x06')

with open('angecrypted.png', "rb") as f:
	d = f.read()

d = AES.encrypt(d)

with open("encrypted.png", "wb") as f:
	f.write(d)