本文整理汇总了Python中Crypto.Util.number.bytes_to_long方法的典型用法代码示例。如果您正苦于以下问题:Python number.bytes_to_long方法的具体用法?Python number.bytes_to_long怎么用?Python number.bytes_to_long使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Crypto.Util.number
的用法示例。
在下文中一共展示了number.bytes_to_long方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: decrypt
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import bytes_to_long [as 别名]
def decrypt(self, ciphertext: bytes) -> bytes:
c = bytes_to_long(ciphertext)
# compute c**d (mod n)
if (hasattr(self.key, 'p') and hasattr(self.key, 'q') and hasattr(self.key, 'u')):
m1 = pow(c, self.key.d % (self.key.p - 1), self.key.p)
m2 = pow(c, self.key.d % (self.key.q - 1), self.key.q)
h = m2 - m1
if (h < 0):
h = h + self.key.q
h = h * self.key.u % self.key.q
plaintext = h * self.key.p + m1
else:
plaintext = pow(c, self.key.d, self.key.n)
return long_to_bytes(plaintext)
示例2: writePublicKey
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import bytes_to_long [as 别名]
def writePublicKey(self, publicKey: RSA.RsaKey) -> bytes:
modulus = publicKey.n
publicExponent = publicKey.e
# Modulus must be reversed because bytes_to_long expects it to be in big endian format
modulusBytes = long_to_bytes(modulus)[:: -1]
stream = BytesIO()
stream.write(b"RSA1")
Uint32LE.pack(len(modulusBytes) + 8, stream)
Uint32LE.pack(2048, stream)
Uint32LE.pack(255, stream)
Uint32LE.pack(publicExponent, stream)
stream.write(modulusBytes)
stream.write(b"\x00" * 8)
return stream.getvalue()
示例3: decode
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import bytes_to_long [as 别名]
def decode(self, derEle, noLeftOvers=0):
"""Decode a complete INTEGER DER element, and re-initializes this
object with it.
@param derEle A complete INTEGER DER element. It must start with a DER
INTEGER tag.
@param noLeftOvers Indicate whether it is acceptable to complete the
parsing of the DER element and find that not all
bytes in derEle have been used.
@return Index of the first unused byte in the given DER element.
Raises a ValueError exception if the DER element is not a
valid non-negative INTEGER.
Raises an IndexError exception if the DER element is too short.
"""
tlvLength = DerObject.decode(self, derEle, noLeftOvers)
if self.typeTag!=self.typeTags['INTEGER']:
raise ValueError ("Not a DER INTEGER.")
if bord(self.payload[0])>127:
raise ValueError ("Negative INTEGER.")
self.value = bytes_to_long(self.payload)
return tlvLength
示例4: generateQ
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import bytes_to_long [as 别名]
def generateQ(randfunc):
S=randfunc(20)
hash1=SHA.new(S).digest()
hash2=SHA.new(long_to_bytes(bytes_to_long(S)+1)).digest()
q = bignum(0)
for i in range(0,20):
c=bord(hash1[i])^bord(hash2[i])
if i==0:
c=c | 128
if i==19:
c= c | 1
q=q*256+c
while (not isPrime(q)):
q=q+2
if pow(2,159L) < q < pow(2,160L):
return S, q
raise RuntimeError('Bad q value generated')
示例5: change_key
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import bytes_to_long [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: __ghash
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import bytes_to_long [as 别名]
def __ghash(self, aad, txt):
len_aad = len(aad)
len_txt = len(txt)
# padding
if 0 == len_aad % 16:
data = aad
else:
data = aad + b'\x00' * (16 - len_aad % 16)
if 0 == len_txt % 16:
data += txt
else:
data += txt + b'\x00' * (16 - len_txt % 16)
tag = 0
assert len(data) % 16 == 0
for i in range(len(data) // 16):
tag ^= bytes_to_long(data[i * 16: (i + 1) * 16])
tag = self.__times_auth_key(tag)
# print 'X\t', hex(tag)
tag ^= ((8 * len_aad) << 64) | (8 * len_txt)
tag = self.__times_auth_key(tag)
return tag
示例7: derive_key
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import bytes_to_long [as 别名]
def derive_key(password):
start = bytes_to_long(password)
#Making sure I am safe from offline bruteforce attack
for i in range(NB_ITERATIONS):
start = start ** e
start %= N
#We are never too cautious let's make it harder
key = 1
for i in range(NB_ITERATIONS):
key = key ** e
key %= N
key *= start
key %= N
return sha256(long_to_bytes(key)).digest()
示例8: generateQ
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import bytes_to_long [as 别名]
def generateQ(randfunc):
S=randfunc(20)
hash1=SHA.new(S).digest()
hash2=SHA.new(long_to_bytes(bytes_to_long(S)+1)).digest()
q = bignum(0)
for i in range(0,20):
c=ord(hash1[i])^ord(hash2[i])
if i==0:
c=c | 128
if i==19:
c= c | 1
q=q*256+c
while (not isPrime(q)):
q=q+2
if pow(2,159) < q < pow(2,160):
return S, q
raise error('Bad q value generated')
示例9: generateQ
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import bytes_to_long [as 别名]
def generateQ(randfunc):
S=randfunc(20)
hash1=SHA.new(S).digest()
hash2=SHA.new(long_to_bytes(bytes_to_long(S)+1)).digest()
q = bignum(0)
for i in range(0,20):
c=ord(hash1[i])^ord(hash2[i])
if i==0:
c=c | 128
if i==19:
c= c | 1
q=q*256+c
while (not isPrime(q)):
q=q+2
if pow(2,159L) < q < pow(2,160L):
return S, q
raise error, 'Bad q value generated'
示例10: parse
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import bytes_to_long [as 别名]
def parse(data):
things = []
while data:
t = ord(data[0])
assert (t & 0xc0) == 0, 'not a universal value: 0x%02x' % t
#assert t & 0x20, 'not a constructed value: 0x%02x' % t
l = ord(data[1])
assert data != 0x80, "shouldn't be an indefinite length"
if l & 0x80: # long form
ll = l & 0x7f
l = number.bytes_to_long(data[2:2+ll])
s = 2 + ll
else:
s = 2
body, data = data[s:s+l], data[s+l:]
t = t&(~0x20)
assert t in (SEQUENCE, INTEGER), 'bad type: 0x%02x' % t
if t == SEQUENCE:
things.append(parse(body))
elif t == INTEGER:
#assert (ord(body[0])&0x80) == 0, "shouldn't have negative number"
things.append(number.bytes_to_long(body))
if len(things) == 1:
return things[0]
return things
示例11: monty_pow
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import bytes_to_long [as 别名]
def monty_pow(base, exp, modulus):
max_len = len(long_to_bytes(max(base, exp, modulus)))
base_b, exp_b, modulus_b = [ long_to_bytes(x, max_len) for x in
(base, exp, modulus) ]
out = create_string_buffer(max_len)
error = _raw_montgomery.monty_pow(
out,
base_b,
exp_b,
modulus_b,
c_size_t(max_len),
c_ulonglong(32)
)
if error == 17:
raise ExceptionModulus()
if error:
raise ValueError("monty_pow failed with error: %d" % error)
result = bytes_to_long(get_raw_buffer(out))
return result
示例12: _check_public_key
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import bytes_to_long [as 别名]
def _check_public_key(self, dsaObj):
k = bytes_to_long(a2b_hex(self.k))
m_hash = bytes_to_long(a2b_hex(self.m_hash))
# Check capabilities
self.assertEqual(0, dsaObj.has_private())
self.assertEqual(1, dsaObj.can_sign())
self.assertEqual(0, dsaObj.can_encrypt())
# Check that private parameters are all missing
self.assertEqual(0, hasattr(dsaObj, 'x'))
# Sanity check key data
self.assertEqual(1, dsaObj.p > dsaObj.q) # p > q
self.assertEqual(160, size(dsaObj.q)) # size(q) == 160 bits
self.assertEqual(0, (dsaObj.p - 1) % dsaObj.q) # q is a divisor of p-1
# Public-only key objects should raise an error when .sign() is called
self.assertRaises(TypeError, dsaObj._sign, m_hash, k)
# Check __eq__ and __ne__
self.assertEqual(dsaObj.publickey() == dsaObj.publickey(),True) # assert_
self.assertEqual(dsaObj.publickey() != dsaObj.publickey(),False) # failIf
示例13: _shift_bytes
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import bytes_to_long [as 别名]
def _shift_bytes(bs, xor_lsb=0):
num = (bytes_to_long(bs)<<1) ^ xor_lsb
return long_to_bytes(num, len(bs))[-len(bs):]
示例14: _decodeLen
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import bytes_to_long [as 别名]
def _decodeLen(self, idx, der):
"""Given a (part of a) DER element, and an index to the first byte of
a DER length tag (L), return a tuple with the payload size,
and the index of the first byte of the such payload (V).
Raises a ValueError exception if the DER length is invalid.
Raises an IndexError exception if the DER element is too short.
"""
length = bord(der[idx])
if length<=127:
return (length,idx+1)
payloadLength = bytes_to_long(der[idx+1:idx+1+(length & 0x7F)])
if payloadLength<=127:
raise ValueError("Not a DER length tag.")
return (payloadLength, idx+1+(length & 0x7F))
示例15: setUp
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import bytes_to_long [as 别名]
def setUp(self):
global DSA, Random, bytes_to_long, size
from Crypto.PublicKey import DSA
from Crypto import Random
from Crypto.Util.number import bytes_to_long, inverse, size
self.dsa = DSA