def get_max_length(self):
     pubkey = rsa.PublicKey(int(self.module, 16), int(self.empoent, 16))
     key_length = int(common.bit_size(pubkey.n) / 8)
     reverSize = 0  #RSA_PKCS1_PADDING填充模式
     max_length = key_length - reverSize
     # print(max_length)
     return max_length
Beispiel #2
0
def encrypt_bigfile(infile, outfile, pub_key):
    if not isinstance(pub_key, key.PublicKey):
        raise TypeError('Public key required, but got %r' % pub_key)
    key_bytes = common.bit_size(pub_key.n) // 8
    blocksize = key_bytes - 11
    outfile.write(byte(varblock.VARBLOCK_VERSION))
    for block in varblock.yield_fixedblocks(infile, blocksize):
        crypto = pkcs1.encrypt(block, pub_key)
        varblock.write_varint(outfile, len(crypto))
        outfile.write(crypto)
Beispiel #3
0
def encrypt_bigfile(infile, outfile, pub_key):
    if not isinstance(pub_key, key.PublicKey):
        raise TypeError('Public key required, but got %r' % pub_key)
    key_bytes = common.bit_size(pub_key.n) // 8
    blocksize = key_bytes - 11
    outfile.write(byte(varblock.VARBLOCK_VERSION))
    for block in varblock.yield_fixedblocks(infile, blocksize):
        crypto = pkcs1.encrypt(block, pub_key)
        varblock.write_varint(outfile, len(crypto))
        outfile.write(crypto)
Beispiel #4
0
def block_size(n):
    '''Returns the block size in bytes, given the public key.

    The block size is determined by the 'n=p*q' component of the key.
    '''

    # Set aside 2 bits so setting of safebit won't overflow modulo n.
    nbits = common.bit_size(n) - 2
    nbytes = nbits // 8

    return nbytes
Beispiel #5
0
def block_size(n):
    '''Returns the block size in bytes, given the public key.

    The block size is determined by the 'n=p*q' component of the key.
    '''

    # Set aside 2 bits so setting of safebit won't overflow modulo n.
    nbits = common.bit_size(n) - 2
    nbytes = nbits // 8

    return nbytes
Beispiel #6
0
def randint(maxvalue):
    bit_size = common.bit_size(maxvalue)
    tries = 0
    while True:
        value = read_random_int(bit_size)
        if value <= maxvalue:
            break
        if tries and tries % 10 == 0:
            bit_size -= 1
        tries += 1

    return value
Beispiel #7
0
def randint(maxvalue):
    bit_size = common.bit_size(maxvalue)
    tries = 0
    while True:
        value = read_random_int(bit_size)
        if value <= maxvalue:
            break
        if tries and tries % 10 == 0:
            bit_size -= 1
        tries += 1

    return value
Beispiel #8
0
 def test_negative_values(self):
     self.assertEqual(bit_size(-1023), 10)
     self.assertEqual(bit_size(-1024), 11)
     self.assertEqual(bit_size(-1025), 11)
     self.assertEqual(bit_size(-1 << 1024), 1025)
     self.assertEqual(bit_size(-((1 << 1024) + 1)), 1025)
     self.assertEqual(bit_size(-((1 << 1024) - 1)), 1024)
Beispiel #9
0
 def test_values(self):
     self.assertEqual(bit_size(1023), 10)
     self.assertEqual(bit_size(1024), 11)
     self.assertEqual(bit_size(1025), 11)
     self.assertEqual(bit_size(1 << 1024), 1025)
     self.assertEqual(bit_size((1 << 1024) + 1), 1025)
     self.assertEqual(bit_size((1 << 1024) - 1), 1024)
Beispiel #10
0
 def test_negative_values(self):
     self.assertEqual(bit_size(-1023), 10)
     self.assertEqual(bit_size(-1024), 11)
     self.assertEqual(bit_size(-1025), 11)
     self.assertEqual(bit_size(-1 << 1024), 1025)
     self.assertEqual(bit_size(-((1 << 1024) + 1)), 1025)
     self.assertEqual(bit_size(-((1 << 1024) - 1)), 1024)
Beispiel #11
0
 def test_values(self):
     self.assertEqual(bit_size(1023), 10)
     self.assertEqual(bit_size(1024), 11)
     self.assertEqual(bit_size(1025), 11)
     self.assertEqual(bit_size(1 << 1024), 1025)
     self.assertEqual(bit_size((1 << 1024) + 1), 1025)
     self.assertEqual(bit_size((1 << 1024) - 1), 1024)
def encrypt_bigfile(infile, outfile, pub_key):
    """Encrypts a file, writing it to 'outfile' in VARBLOCK format.
    
    :param infile: file-like object to read the cleartext from
    :param outfile: file-like object to write the crypto in VARBLOCK format to
    :param pub_key: :py:class:`rsa.PublicKey` to encrypt with
    
    """
    if not isinstance(pub_key, key.PublicKey):
        raise TypeError('Public key required, but got %r' % pub_key)
    key_bytes = common.bit_size(pub_key.n) // 8
    blocksize = key_bytes - 11
    outfile.write(byte(varblock.VARBLOCK_VERSION))
    for block in varblock.yield_fixedblocks(infile, blocksize):
        crypto = pkcs1.encrypt(block, pub_key)
        varblock.write_varint(outfile, len(crypto))
        outfile.write(crypto)
Beispiel #13
0
def encrypt_bigfile(infile, outfile, pub_key):
    """Encrypts a file, writing it to 'outfile' in VARBLOCK format.

    .. deprecated:: 3.4
        This function was deprecated in Python-RSA version 3.4 due to security issues
        in the VARBLOCK format. See the documentation_ for more information.

    .. _documentation: https://stuvel.eu/python-rsa-doc/usage.html#working-with-big-files

    :param infile: file-like object to read the cleartext from
    :param outfile: file-like object to write the crypto in VARBLOCK format to
    :param pub_key: :py:class:`rsa.PublicKey` to encrypt with

    """

    warnings.warn(
        "The 'rsa.bigfile.encrypt_bigfile' function was deprecated in Python-RSA version "
        "3.4 due to security issues in the VARBLOCK format. See "
        "https://stuvel.eu/python-rsa-doc/usage.html#working-with-big-files "
        "for more information.",
        DeprecationWarning,
        stacklevel=2)

    if not isinstance(pub_key, key.PublicKey):
        raise TypeError('Public key required, but got %r' % pub_key)

    key_bytes = common.bit_size(pub_key.n) // 8
    blocksize = key_bytes - 11  # keep space for PKCS#1 padding

    # Write the version number to the VARBLOCK file
    outfile.write(byte(varblock.VARBLOCK_VERSION))

    # Encrypt and write each block
    for block in varblock.yield_fixedblocks(infile, blocksize):
        crypto = pkcs1.encrypt(block, pub_key)

        varblock.write_varint(outfile, len(crypto))
        outfile.write(crypto)
Beispiel #14
0
def randint(maxvalue):
    """Returns a random integer x with 1 <= x <= maxvalue
    May take a very long time in specific situations. If maxvalue needs N bits
    to store, the closer maxvalue is to (2 ** N) - 1, the faster this function
    is.
    """

    bit_size = common.bit_size(maxvalue)

    tries = 0
    while True:
        value = read_random_int(bit_size)
        if value <= maxvalue:
            break

        if tries % 10 == 0 and tries:
            # After a lot of tries to get the right number of bits but still
            # smaller than maxvalue, decrease the number of bits by 1. That'll
            # dramatically increase the chances to get a large enough number.
            bit_size -= 1
        tries += 1

    return value
Beispiel #15
0
def randint(maxvalue):
    """Returns a random integer x with 1 <= x <= maxvalue

    May take a very long time in specific situations. If maxvalue needs N bits
    to store, the closer maxvalue is to (2 ** N) - 1, the faster this function
    is.
    """

    bit_size = common.bit_size(maxvalue)

    tries = 0
    while True:
        value = read_random_int(bit_size)
        if value <= maxvalue:
            break

        if tries and tries % 10 == 0:
            # After a lot of tries to get the right number of bits but still
            # smaller than maxvalue, decrease the number of bits by 1. That'll
            # dramatically increase the chances to get a large enough number.
            bit_size -= 1
        tries += 1

    return value
def encrypt_bigfile(infile, outfile, pub_key):
    '''Encrypts a file, writing it to 'outfile' in VARBLOCK format.
    
    :param infile: file-like object to read the cleartext from
    :param outfile: file-like object to write the crypto in VARBLOCK format to
    :param pub_key: :py:class:`rsa.PublicKey` to encrypt with

    '''

    if not isinstance(pub_key, key.PublicKey):
        raise TypeError('Public key required, but got %r' % pub_key)

    key_bytes = common.bit_size(pub_key.n) // 8
    blocksize = key_bytes - 11 # keep space for PKCS#1 padding

    # Write the version number to the VARBLOCK file
    outfile.write(chr(varblock.VARBLOCK_VERSION))

    # Encrypt and write each block
    for block in varblock.yield_fixedblocks(infile, blocksize):
        crypto = pkcs1.encrypt(block, pub_key)

        varblock.write_varint(outfile, len(crypto))
        outfile.write(crypto)
Beispiel #17
0
def encrypt_bigfile(infile, outfile, pub_key):
    '''Encrypts a file, writing it to 'outfile' in VARBLOCK format.
    
    :param infile: file-like object to read the cleartext from
    :param outfile: file-like object to write the crypto in VARBLOCK format to
    :param pub_key: :py:class:`rsa.PublicKey` to encrypt with

    '''

    if not isinstance(pub_key, key.PublicKey):
        raise TypeError('Public key required, but got %r' % pub_key)

    key_bytes = common.bit_size(pub_key.n) // 8
    blocksize = key_bytes - 11 # keep space for PKCS#1 padding

    # Write the version number to the VARBLOCK file
    outfile.write(byte(varblock.VARBLOCK_VERSION))

    # Encrypt and write each block
    for block in varblock.yield_fixedblocks(infile, blocksize):
        crypto = pkcs1.encrypt(block, pub_key)

        varblock.write_varint(outfile, len(crypto))
        outfile.write(crypto)
Beispiel #18
0
def encrypt_bigfile(infile, outfile, pub_key):
    """Encrypts a file, writing it to 'outfile' in VARBLOCK format.

    .. deprecated:: 3.4
        This function was deprecated in Python-RSA version 3.4 due to security issues
        in the VARBLOCK format. See the documentation_ for more information.

    .. _documentation: https://stuvel.eu/python-rsa-doc/usage.html#working-with-big-files

    :param infile: file-like object to read the cleartext from
    :param outfile: file-like object to write the crypto in VARBLOCK format to
    :param pub_key: :py:class:`rsa.PublicKey` to encrypt with

    """

    warnings.warn("The 'rsa.bigfile.encrypt_bigfile' function was deprecated in Python-RSA version "
                  "3.4 due to security issues in the VARBLOCK format. See "
                  "https://stuvel.eu/python-rsa-doc/usage.html#working-with-big-files "
                  "for more information.",
                  DeprecationWarning, stacklevel=2)

    if not isinstance(pub_key, key.PublicKey):
        raise TypeError('Public key required, but got %r' % pub_key)

    key_bytes = common.bit_size(pub_key.n) // 8
    blocksize = key_bytes - 11  # keep space for PKCS#1 padding

    # Write the version number to the VARBLOCK file
    outfile.write(byte(varblock.VARBLOCK_VERSION))

    # Encrypt and write each block
    for block in varblock.yield_fixedblocks(infile, blocksize):
        crypto = pkcs1.encrypt(block, pub_key)

        varblock.write_varint(outfile, len(crypto))
        outfile.write(crypto)
 def test_zero(self):
     self.assertEqual(bit_size(0), 0)
Beispiel #20
0
 def test_zero(self):
     self.assertEqual(bit_size(0), 0)