Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_correctness_against_base_implementation(self):
# Slow test.
values = [
1 << 512,
1 << 8192,
1 << 77,
]
for value in values:
self.assertEqual(bytes2int(int2bytes(value)),
value,
"Boom %d" % value)
def rVerify(message, signature, pub_key):
n, e = pub_key
blocksize = rsa.common.byte_size(n)
encrypted = rsa.transform.bytes2int(signature)
decrypted = rsa.core.decrypt_int(encrypted, e, n)
clearsig = rsa.transform.int2bytes(decrypted, blocksize)
try:
sep_idx = clearsig.index(('\x00'), 2)
except ValueError:
print ('How ugly your signature looks...More practice,OK?')
return False
signature = clearsig[sep_idx+1:]
# Compare the real hash to the hash in the signature
if message != signature:
print `message`
print `signature`
print ('wanna cheat me,ah?')
return False
def decryptRSA(message, privateKey):
"""
@summary: wrapper around rsa.core.decrypt_int function
@param message: {str} source message
@param publicKey: {rsa.PrivateKey}
"""
return rsa.transform.int2bytes(
rsa.core.decrypt_int(
rsa.transform.bytes2int(message), privateKey['d'], privateKey['n']))
lo = 0
hi = n
while lo < hi:
mid = (lo+hi)//2
if mid**3 < n:
lo = mid+1
else:
hi = mid
return lo
n = 99103278939331174405096046174826505890630650433457474512679503637107184969587849584143967014347754889469667043136895601008192434248630928076345525071962146097925698057299368797800220354529704116063015906135093873544219941584758892847007593809714204471472620455658479996846811490190888414921319427626842981521
e = 3
pub_key = n, e
blocksize = rsa.common.byte_size(n)
orig = rsa.transform.bytes2int('jinmo123\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
root = find_cube_root(orig)
orig = root ** 3
signature = root
clearsig = rsa.transform.int2bytes(orig, blocksize)
signature = rsa.transform.int2bytes(signature, blocksize)
sep_idx = clearsig.index(('\x00'), 2)
message = clearsig[sep_idx+1:]
print `message`
print `signature.encode('hex')`
assert rVerify(message, signature, pub_key)
s.send(message + '\n')
s.send(signature.encode('hex'))
while True:
d = s.recv(1024)
if d == '':
break
:raise OverflowError: if the private key is too small to contain the
requested hash.
"""
# Get the ASN1 code for this hash method
if hash_method not in HASH_ASN1:
raise ValueError('Invalid hash method: %s' % hash_method)
asn1code = HASH_ASN1[hash_method]
# Encrypt the hash with the private key
cleartext = asn1code + hash_value
keylength = common.byte_size(priv_key.n)
padded = _pad_for_signing(cleartext, keylength)
payload = transform.bytes2int(padded)
encrypted = priv_key.blinded_encrypt(payload)
block = transform.int2bytes(encrypted, keylength)
return block
def read_random_int(nbits):
"""Reads a random integer of approximately nbits bits.
"""
randomdata = read_random_bits(nbits)
value = transform.bytes2int(randomdata)
# Ensure that the number is large enough to just fill out the required
# number of bits.
value |= 1 << (nbits - 1)
return value
def verify(message, publicKey):
"""
@summary: return hash
@param message: {str} message to verify
@param publicKey : {rsa.publicKey} key use to sugn
"""
return rsa.transform.int2bytes(rsa.core.decrypt_int(rsa.transform.bytes2int(message), publicKey['e'], publicKey['n']))
>>> from rsa import key, common
>>> (pub_key, priv_key) = key.newkeys(256)
>>> message = b'hello'
>>> crypto = encrypt(message, pub_key)
The crypto text should be just as long as the public key 'n' component:
>>> len(crypto) == common.byte_size(pub_key.n)
True
"""
keylength = common.byte_size(pub_key.n)
padded = _pad_for_encryption(message, keylength)
payload = transform.bytes2int(padded)
encrypted = core.encrypt_int(payload, pub_key.e, pub_key.n)
block = transform.int2bytes(encrypted, keylength)
return block
def read_random_int(nbits):
"""Reads a random integer of approximately nbits bits.
"""
randomdata = read_random_bits(nbits)
value = transform.bytes2int(randomdata)
# Ensure that the number is large enough to just fill out the required
# number of bits.
value |= 1 << (nbits - 1)
return value