Python Crypto.Util.Counter.new() Examples
The following are 30
code examples of Crypto.Util.Counter.new().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
Crypto.Util.Counter
, or try the search function
.
Example #1
Source File: convergence.py From file-encryptor with MIT License | 7 votes |
def iter_transform(filename, key): """Generate encrypted file with given key. This generator function reads the file in chunks and encrypts them using AES-CTR, with the specified key. :param filename: The name of the file to encrypt. :type filename: str :param key: The key used to encrypt the file. :type key: str :returns: A generator that produces encrypted file chunks. :rtype: generator """ # We are not specifying the IV here. aes = AES.new(key, AES.MODE_CTR, counter=Counter.new(128)) with open(filename, 'rb+') as f: for chunk in iter(lambda: f.read(CHUNK_SIZE), b''): yield aes.encrypt(chunk), f
Example #2
Source File: mega.py From mega.py with Apache License 2.0 | 6 votes |
def rename(self, file, new_name): file = file[1] # create new attribs attribs = {'n': new_name} # encrypt attribs encrypt_attribs = base64_url_encode(encrypt_attr(attribs, file['k'])) encrypted_key = a32_to_base64(encrypt_key(file['key'], self.master_key)) # update attributes return self._api_request([{ 'a': 'a', 'attr': encrypt_attribs, 'key': encrypted_key, 'n': file['h'], 'i': self.request_id }])
Example #3
Source File: cryptutils.py From edl with MIT License | 6 votes |
def change_key(self, master_key): if master_key >= (1 << 128): raise InvalidInputException('Master key should be 128-bit') self.__master_key = long_to_bytes(master_key, 16) self.__aes_ecb = AES.new(self.__master_key, AES.MODE_ECB) self.__auth_key = bytes_to_long(self.__aes_ecb.encrypt(b'\x00' * 16)) # precompute the table for multiplication in finite field table = [] # for 8-bit for i in range(16): row = [] for j in range(256): row.append(self.gf_2_128_mul(self.__auth_key, j << (8 * i))) table.append(tuple(row)) self.__pre_table = tuple(table) self.prev_init_value = None # reset
Example #4
Source File: FortunaGenerator.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def _pseudo_random_data(self, bytes): if not (0 <= bytes <= self.max_bytes_per_request): raise AssertionError("You cannot ask for more than 1 MiB of data per request") num_blocks = ceil_shift(bytes, self.block_size_shift) # num_blocks = ceil(bytes / self.block_size) # Compute the output retval = self._generate_blocks(num_blocks)[:bytes] # Switch to a new key to avoid later compromises of this output (i.e. # state compromise extension attacks) self._set_key(self._generate_blocks(self.blocks_per_key)) assert len(retval) == bytes assert len(self.key) == self.key_size return retval
Example #5
Source File: cryptutils.py From edl with MIT License | 5 votes |
def aes_cbc(self,key,iv,data,decrypt=True): if decrypt: return AES.new(key, AES.MODE_CBC, IV=iv).decrypt(data) else: return AES.new(key, AES.MODE_CBC, IV=iv).encrypt(data)
Example #6
Source File: common.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def runTest(self): self.assertRaises(ValueError, self.module.new, a2b_hex(self.key), self.module.MODE_CBC, "") self.assertRaises(ValueError, self.module.new, a2b_hex(self.key), self.module.MODE_CFB, "") self.assertRaises(ValueError, self.module.new, a2b_hex(self.key), self.module.MODE_OFB, "") self.assertRaises(ValueError, self.module.new, a2b_hex(self.key), self.module.MODE_OPENPGP, "") self.module.new(a2b_hex(self.key), self.module.MODE_ECB, "") self.module.new(a2b_hex(self.key), self.module.MODE_CTR, "", counter=self._dummy_counter)
Example #7
Source File: FortunaGenerator.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def __init__(self): self.counter = Counter.new(nbits=self.block_size*8, initial_value=0, little_endian=True) self.key = None # Set some helper constants self.block_size_shift = exact_log2(self.block_size) assert (1 << self.block_size_shift) == self.block_size self.blocks_per_key = exact_div(self.key_size, self.block_size) assert self.key_size == self.blocks_per_key * self.block_size self.max_bytes_per_request = self.max_blocks_per_request * self.block_size
Example #8
Source File: FortunaGenerator.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def _set_key(self, key): self.key = key self._cipher = AES.new(key, AES.MODE_CTR, counter=self.counter)
Example #9
Source File: cursor.py From addon with GNU General Public License v3.0 | 5 votes |
def prepare_decoder(self,offset): initial_value = self.initial_value + int(offset/16) try: from Cryptodome.Cipher import AES from Cryptodome.Util import Counter self.decryptor = AES.new(self._file._client.a32_to_str(self.k), AES.MODE_CTR, counter = Counter.new(128, initial_value = initial_value)) except: from Crypto.Cipher import AES from Crypto.Util import Counter self.decryptor = AES.new(self._file._client.a32_to_str(self.k), AES.MODE_CTR, counter = Counter.new(128, initial_value = initial_value)) rest = offset - int(offset/16)*16 if rest: self.decode(str(0)*rest)
Example #10
Source File: pycrypto.py From malwareHunter with GNU General Public License v2.0 | 5 votes |
def aesEncrypt(data, key): cipher = AES.new(key, AES.MODE_CTR, counter=Counter.new(128, initial_value=0)) return cipher.encrypt(data)
Example #11
Source File: cryptutils.py From edl with MIT License | 5 votes |
def aes_ctr(self,key,counter,enc_data,decrypt=True): ctr = Counter.new(128, initial_value=counter) # Create the AES cipher object and decrypt the ciphertext, basically this here is just aes ctr 256 :) cipher = AES.new(key, AES.MODE_CTR, counter=ctr) data = b"" if decrypt: data = cipher.decrypt(enc_data) else: data = cipher.encrypt(enc_data) return data
Example #12
Source File: cryptutils.py From edl with MIT License | 5 votes |
def aes_ccm(self, key, nounce, tag_auth, data, decrypt=True): cipher = AES.new(key, AES.MODE_CCM, nounce) if decrypt: plaintext = cipher.decrypt(data) try: cipher.verify(tag_auth) return plaintext except ValueError: return None else: ciphertext = cipher.encrypt(data) return ciphertext
Example #13
Source File: cryptutils.py From edl with MIT License | 5 votes |
def aes_cmac_verify(key,plain,compare): ctx = CMAC.new(key, ciphermod=AES) ctx.update(plain) result=ctx.hexdigest() if result!=compare: print("AES-CMAC failed !") else: print("AES-CMAC ok !")
Example #14
Source File: pycrypto.py From malwareHunter with GNU General Public License v2.0 | 5 votes |
def aesDecrypt(data, key): cipher = AES.new(key, AES.MODE_CTR, counter=Counter.new(128, initial_value=0)) return cipher.decrypt(data)
Example #15
Source File: pycrypto.py From malwareHunter with GNU General Public License v2.0 | 5 votes |
def aesEncrypt(data, key): cipher = AES.new(key, AES.MODE_CTR, counter=Counter.new(128, initial_value=0)) return cipher.encrypt(data)
Example #16
Source File: cryptutils.py From edl with MIT License | 5 votes |
def aes_gcm(self,ciphertext,nounce,aes_key, hdr, tag_auth, decrypt=True): cipher = AES.new(aes_key, AES.MODE_GCM, nounce) if hdr!=None: cipher.update(hdr) if decrypt: plaintext = cipher.decrypt(ciphertext) try: cipher.verify(tag_auth) return plaintext except ValueError: return None
Example #17
Source File: keyfile.py From etheno with GNU Affero General Public License v3.0 | 5 votes |
def encrypt_aes_ctr(value, key, iv): ctr = Counter.new(128, initial_value=iv, allow_wraparound=True) encryptor = AES.new(key, AES.MODE_CTR, counter=ctr) ciphertext = encryptor.encrypt(value) return ciphertext # # Utility #
Example #18
Source File: common.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def runTest(self): self.assertRaises(ValueError, self.module.new, a2b_hex(self.key), self.module.MODE_PGP)
Example #19
Source File: common.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def runTest(self): for mode in (self.module.MODE_ECB, self.module.MODE_CBC, self.module.MODE_CFB, self.module.MODE_OFB, self.module.MODE_OPENPGP): encryption_cipher = self.module.new(a2b_hex(self.key), mode, self.iv) ciphertext = encryption_cipher.encrypt(self.plaintext) if mode != self.module.MODE_OPENPGP: decryption_cipher = self.module.new(a2b_hex(self.key), mode, self.iv) else: eiv = ciphertext[:self.module.block_size+2] ciphertext = ciphertext[self.module.block_size+2:] decryption_cipher = self.module.new(a2b_hex(self.key), mode, eiv) decrypted_plaintext = decryption_cipher.decrypt(ciphertext) self.assertEqual(self.plaintext, decrypted_plaintext)
Example #20
Source File: common.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def runTest(self): """Regression test: m.new(key, m.MODE_CFB, segment_size=N) should require segment_size to be a multiple of 8 bits""" for i in range(1, 8): self.assertRaises(ValueError, self.module.new, a2b_hex(self.key), self.module.MODE_CFB, segment_size=i) self.module.new(a2b_hex(self.key), self.module.MODE_CFB, "\0"*self.module.block_size, segment_size=8) # should succeed
Example #21
Source File: common.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def runTest(self): self.assertRaises(TypeError, self.module.new, a2b_hex(self.key), self.module.MODE_CTR)
Example #22
Source File: common.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def shortDescription(self): return """Regression test: %s.new(key, %s.MODE_CTR) should raise TypeError, not segfault""" % (self.module_name, self.module_name)
Example #23
Source File: common.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def _new(self, do_decryption=0): params = self.extra_params.copy() # Handle CTR mode parameters. By default, we use Counter.new(self.module.block_size) if hasattr(self.module, "MODE_CTR") and self.mode == self.module.MODE_CTR: from Crypto.Util import Counter ctr_class = _extract(params, 'ctr_class', Counter.new) ctr_params = _extract(params, 'ctr_params', {}).copy() if ctr_params.has_key('prefix'): ctr_params['prefix'] = a2b_hex(b(ctr_params['prefix'])) if ctr_params.has_key('suffix'): ctr_params['suffix'] = a2b_hex(b(ctr_params['suffix'])) if not ctr_params.has_key('nbits'): ctr_params['nbits'] = 8*(self.module.block_size - len(ctr_params.get('prefix', '')) - len(ctr_params.get('suffix', ''))) params['counter'] = ctr_class(**ctr_params) if self.mode is None: # Stream cipher return self.module.new(a2b_hex(self.key), **params) elif self.iv is None: # Block cipher without iv return self.module.new(a2b_hex(self.key), self.mode, **params) else: # Block cipher with iv if do_decryption and self.mode == self.module.MODE_OPENPGP: # In PGP mode, the IV to feed for decryption is the *encrypted* one return self.module.new(a2b_hex(self.key), self.mode, a2b_hex(self.encrypted_iv), **params) else: return self.module.new(a2b_hex(self.key), self.mode, a2b_hex(self.iv), **params)
Example #24
Source File: test_Counter.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_LE8_carry(self): """8-bit, Little endian, carry attribute""" c = Counter.new(8, little_endian=True) for i in xrange(1, 256): self.assertEqual(0, c.carry) self.assertEqual(i, c.next_value()) self.assertEqual(bchr(i), c()) self.assertEqual(1, c.carry)
Example #25
Source File: test_Counter.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_BE8_carry(self): """8-bit, Big endian, carry attribute""" c = Counter.new(8) for i in xrange(1, 256): self.assertEqual(0, c.carry) self.assertEqual(i, c.next_value()) self.assertEqual(bchr(i), c()) self.assertEqual(1, c.carry)
Example #26
Source File: test_Counter.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_BE8_wraparound_allowed(self): """8-bit, Big endian, wraparound with allow_wraparound=True""" c = Counter.new(8, allow_wraparound=True) for i in xrange(1, 256): self.assertEqual(i, c.next_value()) self.assertEqual(bchr(i), c()) self.assertEqual(0, c.next_value()) self.assertEqual(b("\x00"), c()) self.assertEqual(1, c.next_value())
Example #27
Source File: test_Counter.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_LE8_wraparound(self): """8-bit, Little endian, wraparound""" c = Counter.new(8, little_endian=True) for i in xrange(1, 256): self.assertEqual(i, c.next_value()) self.assertEqual(bchr(i), c()) self.assertRaises(OverflowError, c.next_value) self.assertRaises(OverflowError, c) self.assertRaises(OverflowError, c.next_value) self.assertRaises(OverflowError, c)
Example #28
Source File: test_Counter.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_BE8_wraparound(self): """8-bit, Big endian, wraparound""" c = Counter.new(8) for i in xrange(1, 256): self.assertEqual(i, c.next_value()) self.assertEqual(bchr(i), c()) self.assertRaises(OverflowError, c.next_value) self.assertRaises(OverflowError, c) self.assertRaises(OverflowError, c.next_value) self.assertRaises(OverflowError, c)
Example #29
Source File: test_Counter.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_LE_defaults(self): """128-bit, Little endian, defaults""" c = Counter.new(128, little_endian=True) self.assertEqual(1, c.next_value()) self.assertEqual(b("\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"), c()) self.assertEqual(2, c.next_value()) self.assertEqual(b("\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"), c()) for i in xrange(3, 256): self.assertEqual(i, c.next_value()) self.assertEqual(bchr(i)+b("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"), c()) self.assertEqual(256, c.next_value()) self.assertEqual(b("\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"), c())
Example #30
Source File: test_Counter.py From earthengine with MIT License | 5 votes |
def test_BE_shortcut(self): """Big endian, shortcut enabled""" c = Counter.new(128) self.assertEqual(c.__PCT_CTR_SHORTCUT__,True) # assert_ c = Counter.new(128, little_endian=False) self.assertEqual(c.__PCT_CTR_SHORTCUT__,True) # assert_ c = Counter.new(128, disable_shortcut=False) self.assertEqual(c.__PCT_CTR_SHORTCUT__,True) # assert_ c = Counter.new(128, little_endian=False, disable_shortcut=False) self.assertEqual(c.__PCT_CTR_SHORTCUT__,True) # assert_