本文整理汇总了Python中Crypto.Util.Counter.new方法的典型用法代码示例。如果您正苦于以下问题:Python Counter.new方法的具体用法?Python Counter.new怎么用?Python Counter.new使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Crypto.Util.Counter
的用法示例。
在下文中一共展示了Counter.new方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: iter_transform
# 需要导入模块: from Crypto.Util import Counter [as 别名]
# 或者: from Crypto.Util.Counter import new [as 别名]
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
示例2: dorecv
# 需要导入模块: from Crypto.Util import Counter [as 别名]
# 或者: from Crypto.Util.Counter import new [as 别名]
def dorecv(self):
msg = self.request.recv(1024)
assert len(msg)>0
if self.k!='':
self.r += 1
ctr_e=Counter.new(128, initial_value=self.r)
a=AES.new(key=self.k,mode=AES.MODE_CTR,counter=ctr_e)
cmsg = msg
msg = ''
assert len(cmsg)%16==0
for i in range(len(cmsg)/16):
msg += a.decrypt(cmsg[i*16:(i+1)*16])
tmp = ord(msg[-1])
for i in range(len(msg)-tmp,len(msg)):
assert ord(msg[i]) == tmp
msg = msg[:-ord(msg[-1])]
return msg
示例3: _pseudo_random_data
# 需要导入模块: from Crypto.Util import Counter [as 别名]
# 或者: from Crypto.Util.Counter import new [as 别名]
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
示例4: rename
# 需要导入模块: from Crypto.Util import Counter [as 别名]
# 或者: from Crypto.Util.Counter import new [as 别名]
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
}])
示例5: change_key
# 需要导入模块: from Crypto.Util import Counter [as 别名]
# 或者: from Crypto.Util.Counter import new [as 别名]
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
示例6: test_BE_shortcut
# 需要导入模块: from Crypto.Util import Counter [as 别名]
# 或者: from Crypto.Util.Counter import new [as 别名]
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_
示例7: test_LE_shortcut
# 需要导入模块: from Crypto.Util import Counter [as 别名]
# 或者: from Crypto.Util.Counter import new [as 别名]
def test_LE_shortcut(self):
"""Little endian, shortcut enabled"""
c = Counter.new(128, little_endian=True)
self.assertEqual(c.__PCT_CTR_SHORTCUT__,True) # assert_
c = Counter.new(128, little_endian=True, disable_shortcut=False)
self.assertEqual(c.__PCT_CTR_SHORTCUT__,True) # assert_
示例8: test_BE_no_shortcut
# 需要导入模块: from Crypto.Util import Counter [as 别名]
# 或者: from Crypto.Util.Counter import new [as 别名]
def test_BE_no_shortcut(self):
"""Big endian, shortcut disabled"""
c = Counter.new(128, disable_shortcut=True)
self.assertRaises(AttributeError, getattr, c, '__PCT_CTR_SHORTCUT__')
c = Counter.new(128, little_endian=False, disable_shortcut=True)
self.assertRaises(AttributeError, getattr, c, '__PCT_CTR_SHORTCUT__')
示例9: test_LE_no_shortcut
# 需要导入模块: from Crypto.Util import Counter [as 别名]
# 或者: from Crypto.Util.Counter import new [as 别名]
def test_LE_no_shortcut(self):
"""Little endian, shortcut disabled"""
c = Counter.new(128, little_endian=True, disable_shortcut=True)
self.assertRaises(AttributeError, getattr, c, '__PCT_CTR_SHORTCUT__')
示例10: test_BE_defaults
# 需要导入模块: from Crypto.Util import Counter [as 别名]
# 或者: from Crypto.Util.Counter import new [as 别名]
def test_BE_defaults(self):
"""128-bit, Big endian, defaults"""
c = Counter.new(128)
self.assertEqual(1, c.next_value())
self.assertEqual(b("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"), c())
self.assertEqual(2, c.next_value())
self.assertEqual(b("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02"), c())
for i in xrange(3, 256):
self.assertEqual(i, c.next_value())
self.assertEqual(b("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")+bchr(i), c())
self.assertEqual(256, c.next_value())
self.assertEqual(b("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00"), c())
示例11: test_BE8_wraparound
# 需要导入模块: from Crypto.Util import Counter [as 别名]
# 或者: from Crypto.Util.Counter import new [as 别名]
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)
示例12: test_LE8_wraparound
# 需要导入模块: from Crypto.Util import Counter [as 别名]
# 或者: from Crypto.Util.Counter import new [as 别名]
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)
示例13: test_BE8_wraparound_allowed
# 需要导入模块: from Crypto.Util import Counter [as 别名]
# 或者: from Crypto.Util.Counter import new [as 别名]
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())
示例14: test_LE8_wraparound_allowed
# 需要导入模块: from Crypto.Util import Counter [as 别名]
# 或者: from Crypto.Util.Counter import new [as 别名]
def test_LE8_wraparound_allowed(self):
"""8-bit, Little endian, wraparound with allow_wraparound=True"""
c = Counter.new(8, little_endian=True, 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())
示例15: test_BE8_carry
# 需要导入模块: from Crypto.Util import Counter [as 别名]
# 或者: from Crypto.Util.Counter import new [as 别名]
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)