本文整理汇总了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方法的26个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: generateQ
点赞 6
# 需要导入模块: 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')
开发者ID:mortcanty,项目名称:earthengine,代码行数:19,
示例2: change_key
点赞 6
# 需要导入模块: 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
开发者ID:bkerler,项目名称:edl,代码行数:20,
示例3: decrypt
点赞 6
# 需要导入模块: 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)
开发者ID:GoSecure,项目名称:pyrdp,代码行数:20,
示例4: writePublicKey
点赞 6
# 需要导入模块: 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()
开发者ID:GoSecure,项目名称:pyrdp,代码行数:18,
示例5: derive_key
点赞 6
# 需要导入模块: 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()
开发者ID:The-Art-of-Hacking,项目名称:h4cker,代码行数:21,
示例6: pubKeyXML
点赞 6
# 需要导入模块: 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 = ''
xml += ''
xml += standard_b64encode(number.long_to_bytes(publicKey.n))
xml += ''
xml += ''
xml += standard_b64encode(number.long_to_bytes(publicKey.e))
xml += ''
xml += ''
fileName = basename(pemPublicKeyFile)
with open (fileName+'.xml', 'w') as pkFile:
pkFile.write(xml)
return
#
# CreateXMLPrivKey
#
开发者ID:MisterDaneel,项目名称:PemToXml,代码行数:21,
示例7: _noise
点赞 6
# 需要导入模块: 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)
开发者ID:kuri65536,项目名称:python-for-android,代码行数:19,
示例8: generateQ
点赞 6
# 需要导入模块: 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')
开发者ID:kuri65536,项目名称:python-for-android,代码行数:19,
示例9: _noise
点赞 6
# 需要导入模块: 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)
开发者ID:kuri65536,项目名称:python-for-android,代码行数:19,
示例10: generateQ
点赞 6
# 需要导入模块: 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'
开发者ID:kuri65536,项目名称:python-for-android,代码行数:19,
示例11: pack
点赞 6
# 需要导入模块: 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
开发者ID:kenorb-contrib,项目名称:BitTorrent,代码行数:24,
示例12: monty_pow
点赞 6
# 需要导入模块: 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
开发者ID:vcheckzen,项目名称:FODI,代码行数:25,
示例13: _compute_mac
点赞 6
# 需要导入模块: 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
开发者ID:vcheckzen,项目名称:FODI,代码行数:18,
示例14: _compute_mac
点赞 6
# 需要导入模块: 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
开发者ID:vcheckzen,项目名称:FODI,代码行数:23,
示例15: generate_RSA
点赞 6
# 需要导入模块: 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()
开发者ID:projectarkc,项目名称:arkc-client,代码行数:22,
示例16: export_privatekeyblob
点赞 6
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [as 别名]
def export_privatekeyblob(self):
key = self.key.key
n = key.n
e = key.e
d = key.d
p = key.p
q = key.q
n_bytes = long_to_bytes(n)[::-1]
key_len = len(n_bytes) * 8
result = PUBLICKEYSTRUC_s.pack(bType_PRIVATEKEYBLOB, CUR_BLOB_VERSION, CALG_RSA_KEYX)
result += RSAPUBKEY_s.pack(PRIVATEKEYBLOB_MAGIC, key_len, e)
result += n_bytes
result += long_to_bytes(p, key_len / 16)[::-1]
result += long_to_bytes(q, key_len / 16)[::-1]
result += long_to_bytes(d % (p - 1), key_len / 16)[::-1]
result += long_to_bytes(d % (q - 1), key_len / 16)[::-1]
result += long_to_bytes(inverse(q, p), key_len / 16)[::-1]
result += long_to_bytes(d, key_len / 8)[::-1]
return result
开发者ID:crappycrypto,项目名称:wincrypto,代码行数:22,
示例17: extractSecretKey
点赞 5
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [as 别名]
def extractSecretKey(self, globalSalt, masterPassword, entrySalt):
(globalSalt, masterPassword, entrySalt) = self.is_masterpassword_correct(masterPassword)
if unhexlify('f8000000000000000000000000000001') not in self.key3:
return None
privKeyEntry = self.key3[unhexlify('f8000000000000000000000000000001')]
saltLen = ord(privKeyEntry[1])
nameLen = ord(privKeyEntry[2])
privKeyEntryASN1 = decoder.decode(privKeyEntry[3 + saltLen + nameLen:])
data = privKeyEntry[3 + saltLen + nameLen:]
self.printASN1(data, len(data), 0)
# see https://github.com/philsmd/pswRecovery4Moz/blob/master/pswRecovery4Moz.txt
entrySalt = privKeyEntryASN1[0][0][1][0].asOctets()
privKeyData = privKeyEntryASN1[0][1].asOctets()
privKey = self.decrypt3DES(globalSalt, masterPassword, entrySalt, privKeyData)
self.printASN1(privKey, len(privKey), 0)
privKeyASN1 = decoder.decode(privKey)
prKey = privKeyASN1[0][2].asOctets()
self.printASN1(prKey, len(prKey), 0)
prKeyASN1 = decoder.decode(prKey)
id = prKeyASN1[0][1]
key = long_to_bytes(prKeyASN1[0][3])
return key
# --------------------------------------------
# Get the path list of the firefox profiles
开发者ID:mehulj94,项目名称:Radium,代码行数:33,
示例18: _shift_bytes
点赞 5
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [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):]
开发者ID:mortcanty,项目名称:earthengine,代码行数:5,
示例19: _lengthOctets
点赞 5
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [as 别名]
def _lengthOctets(self, payloadLen):
"""Return a byte string that encodes the given payload length (in
bytes) in a format suitable for a DER length tag (L).
"""
if payloadLen>127:
encoding = long_to_bytes(payloadLen)
return bchr(len(encoding)+128) + encoding
return bchr(payloadLen)
开发者ID:mortcanty,项目名称:earthengine,代码行数:10,
示例20: encode
点赞 5
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [as 别名]
def encode(self):
"""Return a complete INTEGER DER element, fully encoded as a TLV."""
self.payload = long_to_bytes(self.value)
if bord(self.payload[0])>127:
self.payload = bchr(0x00) + self.payload
return DerObject.encode(self)
开发者ID:mortcanty,项目名称:earthengine,代码行数:8,
示例21: decryptKey
点赞 5
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [as 别名]
def decryptKey(encryptedKey):
"""Decrypts an RSA-encrypted key"""
rsa = RSA.construct((long(constants.rsaModulus), long(constants.rsaExponent)))
try:
return rsa.encrypt(encryptedKey, 0)[0]
except NotImplementedError:
# pycryptodome
return long_to_bytes(rsa._encrypt(bytes_to_long(encryptedKey)))
开发者ID:ma1co,项目名称:Sony-PMCA-RE,代码行数:10,
示例22: MGF1
点赞 5
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [as 别名]
def MGF1(mgfSeed, maskLen, hash):
"""Mask Generation Function, described in B.2.1"""
T = b("")
for counter in xrange(ceil_div(maskLen, hash.digest_size)):
c = long_to_bytes(counter, 4)
T = T + hash.new(mgfSeed + c).digest()
assert(len(T)>=maskLen)
return T[:maskLen]
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:10,
示例23: lsbitoracle
点赞 5
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [as 别名]
def lsbitoracle(flag_enc, _decrypt, e, N, upper_limit, lower_limit):
"""
Reference: https://crypto.stackexchange.com/questions/11053/rsa-least-significant-bit-oracle-attack
Function implementing LSBit Oracle Attack
*Warning*: Function does not return the last byte of the final plaintext
:parameters:
flag_enc : str
Ciphertext you want to decrypt
_decrypt : function
Function interacting with the server for decryption
e : int/long
Public Key exponent
N : long
Public Key Modulus
upper_limit: long
Maximum value of corresponding plaintext of flag_enc
lower_limit: long
Minimum value of corresponding plaintext of flag_enc
Since the attack messes up with the last byte of the plaintext, lsbitoracle
function returns only flag[:-1]. It returns -1 in case of any Exception
"""
flag = ""
i = 1
while lower_limit < upper_limit:
chosen_ct = long_to_bytes((bytes_to_long(flag_enc)*pow(2**i, e, N)) % N)
output = _decrypt(chosen_ct)
if ord(output[-1]) == 0:
upper_limit = (upper_limit + lower_limit)/2
elif ord(output[-1]) == 1:
lower_limit = (lower_limit + upper_limit)/2
else:
return -1
i += 1
# clearing the last byte from the flag
flag = lower_limit & (~0xff)
return long_to_bytes(flag)
开发者ID:ashutosh1206,项目名称:Crypton,代码行数:42,
示例24: hastad_unpadded
点赞 5
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [as 别名]
def hastad_unpadded(ct_list, mod_list, e):
"""
Implementing Hastad's Broadcast Attack
"""
m_expo = crt(ct_list, mod_list)
if m_expo != -1:
eth_root = gmpy2.iroot(m_expo, e)
if eth_root[1] == False:
print "[+] Cannot calculate e'th root!"
return -1
elif eth_root[1] == True:
return long_to_bytes(eth_root)
else:
print "[+] Cannot calculate CRT"
return -1
开发者ID:ashutosh1206,项目名称:Crypton,代码行数:17,
示例25: curve25519
点赞 5
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [as 别名]
def curve25519(secret, basepoint):
a = ord(secret[0])
a &= 248
b = ord(secret[31])
b &= 127
b |= 64
s = chr(a) + secret[1:-1] + chr(b)
s = number.bytes_to_long(s[::-1])
basepoint = number.bytes_to_long(basepoint[::-1])
x, z = curve25519_mult(s, basepoint)
zmone = number.inverse(z, CURVE_P)
z = x * zmone % CURVE_P
return number.long_to_bytes(z)[::-1]
开发者ID:n0fate,项目名称:iChainbreaker,代码行数:17,
示例26: pkcs_i2osp
点赞 5
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import long_to_bytes [as 别名]
def pkcs_i2osp(x,xLen):
"""
Converts a long (the first parameter) to the associated byte string
representation of length l (second parameter). Basically, the length
parameters allow the function to perform the associated padding.
Input : x nonnegative integer to be converted
xLen intended length of the resulting octet string
Output: x corresponding nonnegative integer
Reverse function is pkcs_os2ip().
"""
z = number.long_to_bytes(x)
padlen = max(0, xLen-len(z))
return '\x00'*padlen + z
# for every hash function a tuple is provided, giving access to
# - hash output length in byte
# - associated hash function that take data to be hashed as parameter
# XXX I do not provide update() at the moment.
# - DER encoding of the leading bits of digestInfo (the hash value
# will be concatenated to create the complete digestInfo).
#
# Notes:
# - MD4 asn.1 value should be verified. Also, as stated in
# PKCS#1 v2.1, MD4 should not be used.
# - hashlib is available from http://code.krypto.org/python/hashlib/
# - 'tls' one is the concatenation of both md5 and sha1 hashes used
# by SSL/TLS when signing/verifying things
开发者ID:theralfbrown,项目名称:smod-1,代码行数:32,
注:本文中的Crypto.Util.number.long_to_bytes方法示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。