本文整理汇总了Python中Crypto.Util.number.long_to_bytes方法的典型用法代码示例。如果您正苦于以下问题:Python number.long_to_bytes方法的具体用法?Python number.long_to_bytes怎么用?Python number.long_to_bytes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Crypto.Util.number
的用法示例。
在下文中一共展示了number.long_to_bytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: derive_key
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [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()
示例2: generateQ
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [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')
示例3: change_key
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [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
示例4: decrypt
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [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)
示例5: writePublicKey
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [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()
示例6: pubKeyXML
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [as 别名]
def pubKeyXML(pemPublicKeyFile):
with open (pemPublicKeyFile, 'rb') as pkFile:
pemPublicKey = pkFile.read()
publicKey = RSA.importKey(pemPublicKey)
xml = '<RSAKeyValue>'
xml += '<Modulus>'
xml += standard_b64encode(number.long_to_bytes(publicKey.n))
xml += '</Modulus>'
xml += '<Exponent>'
xml += standard_b64encode(number.long_to_bytes(publicKey.e))
xml += '</Exponent>'
xml += '</RSAKeyValue>'
fileName = basename(pemPublicKeyFile)
with open (fileName+'.xml', 'w') as pkFile:
pkFile.write(xml)
return
#
# CreateXMLPrivKey
#
示例7: _noise
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [as 别名]
def _noise(self):
# Adds a bit of noise to the random pool, by adding in the
# current time and CPU usage of this process.
# The difference from the previous call to _noise() is taken
# in an effort to estimate the entropy.
t=time.time()
delta = (t - self._lastcounter)/self._ticksize*1e6
self._lastcounter = t
self._addBytes(long_to_bytes(int(1000*time.time())))
self._addBytes(long_to_bytes(int(1000*time.clock())))
self._addBytes(long_to_bytes(int(1000*time.time())))
self._addBytes(long_to_bytes(int(delta)))
# Reduce delta to a maximum of 8 bits so we don't add too much
# entropy as a result of this call.
delta=delta % 0xff
return int(delta)
示例8: generateQ
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [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: _noise
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [as 别名]
def _noise(self):
# Adds a bit of noise to the random pool, by adding in the
# current time and CPU usage of this process.
# The difference from the previous call to _noise() is taken
# in an effort to estimate the entropy.
t=time.time()
delta = (t - self._lastcounter)/self._ticksize*1e6
self._lastcounter = t
self._addBytes(long_to_bytes(long(1000*time.time())))
self._addBytes(long_to_bytes(long(1000*time.clock())))
self._addBytes(long_to_bytes(long(1000*time.time())))
self._addBytes(long_to_bytes(long(delta)))
# Reduce delta to a maximum of 8 bits so we don't add too much
# entropy as a result of this call.
delta=delta % 0xff
return int(delta)
示例10: generateQ
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [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'
示例11: pack
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [as 别名]
def pack(data):
ret = ''
for part in data:
if type(part) in (type(()), type([])):
partData = pack(part)
partType = SEQUENCE|0x20
elif type(part) in (type(1), type(1L)):
partData = number.long_to_bytes(part)
if ord(partData[0])&(0x80):
partData = '\x00' + partData
partType = INTEGER
else:
raise 'unknown type %s' % type(part)
ret += chr(partType)
if len(partData) > 127:
l = number.long_to_bytes(len(partData))
ret += chr(len(l)|0x80) + l
else:
ret += chr(len(partData))
ret += partData
return ret
示例12: monty_pow
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [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
示例13: _compute_mac
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [as 别名]
def _compute_mac(self):
"""Compute MAC without any FSM checks."""
if self._tag:
return self._tag
# Step 5 in NIST SP 800-38D, Algorithm 4 - Compute S
self._pad_cache_and_update()
self._update(long_to_bytes(8 * self._auth_len, 8))
self._update(long_to_bytes(8 * self._msg_len, 8))
s_tag = self._signer.digest()
# Step 6 - Compute T
self._tag = self._tag_cipher.encrypt(s_tag)[:self._mac_len]
return self._tag
示例14: _compute_mac
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [as 别名]
def _compute_mac(self):
"""Finalize the cipher (if not done already) and return the MAC."""
if self._mac_tag:
assert(self._status == _CipherStatus.PROCESSING_DONE)
return self._mac_tag
assert(self._status != _CipherStatus.PROCESSING_DONE)
if self._status == _CipherStatus.PROCESSING_AUTH_DATA:
self._pad_aad()
if self._len_ct & 0x0F:
self._authenticator.update(b'\x00' * (16 - (self._len_ct & 0x0F)))
self._status = _CipherStatus.PROCESSING_DONE
self._authenticator.update(long_to_bytes(self._len_aad, 8)[::-1])
self._authenticator.update(long_to_bytes(self._len_ct, 8)[::-1])
self._mac_tag = self._authenticator.digest()
return self._mac_tag
示例15: generate_RSA
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [as 别名]
def generate_RSA(pridir, pubdir):
key = RSA.generate(2048)
with open(pridir, 'wb') as content_file:
content_file.write(key.exportKey('PEM'))
print("Private key written to home directory " + pridir)
with open(pubdir, 'wb') as content_file:
# Ugly hack to introduce pycrypto v2.7a1
# Original: .exportKey('OpenSSH')
eb = long_to_bytes(key.e)
nb = long_to_bytes(key.n)
if bord(eb[0]) & 0x80:
eb = bchr(0x00) + eb
if bord(nb[0]) & 0x80:
nb = bchr(0x00) + nb
keyparts = [b('ssh-rsa'), eb, nb]
keystring = b('').join(
[struct.pack(">I", len(kp)) + kp for kp in keyparts])
content_file.write(b('ssh-rsa ') + binascii.b2a_base64(keystring)[:-1])
print("Public key written to home directory " + pubdir)
return sha1(key.exportKey('PEM')).hexdigest()