Beispiel #1
0
    def test_randomness(self):
        """Encrypting the same message twice should result in different
        cryptos.
        """

        message = struct.pack('>IIII', 0, 0, 0, 1)
        encrypted1 = pkcs1.encrypt(message, self.pub)
        encrypted2 = pkcs1.encrypt(message, self.pub)

        self.assertNotEqual(encrypted1, encrypted2)
Beispiel #2
0
 def test_randomness(self):
     '''Encrypting the same message twice should result in different
     cryptos.
     '''
     
     message = struct.pack('>IIII', 0, 0, 0, 1)
     encrypted1 = pkcs1.encrypt(message, self.pub)
     encrypted2 = pkcs1.encrypt(message, self.pub)
     
     self.assertNotEqual(encrypted1, encrypted2)
Beispiel #3
0
    def test_decoding_failure(self):

        message = struct.pack('>IIII', 0, 0, 0, 1)
        encrypted = pkcs1.encrypt(message, self.pub)

        # Alter the encrypted stream
        encrypted = encrypted[:5] + chr(ord(encrypted[5]) + 1) + encrypted[6:]

        self.assertRaises(pkcs1.DecryptionError, pkcs1.decrypt, encrypted,
                          self.priv)
Beispiel #4
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 #5
0
    def test_decoding_failure(self):

        message = struct.pack('>IIII', 0, 0, 0, 1)
        encrypted = pkcs1.encrypt(message, self.pub)

        # Alter the encrypted stream
        encrypted = encrypted[:5] + chr(ord(encrypted[5]) + 1) + encrypted[6:]
        
        self.assertRaises(pkcs1.DecryptionError, pkcs1.decrypt, encrypted,
                          self.priv)
Beispiel #6
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 #7
0
    def test_enc_dec(self):
        message = struct.pack('>IIII', 0, 0, 0, 1)
        print("\tMessage:   %r" % message)

        encrypted = pkcs1.encrypt(message, self.pub)
        print("\tEncrypted: %r" % encrypted)

        decrypted = pkcs1.decrypt(encrypted, self.priv)
        print("\tDecrypted: %r" % decrypted)

        self.assertEqual(message, decrypted)
Beispiel #8
0
    def test_enc_dec(self):
        message = struct.pack('>IIII', 0, 0, 0, 1)
        print("\tMessage:   %r" % message)

        encrypted = pkcs1.encrypt(message, self.pub)
        print("\tEncrypted: %r" % encrypted)

        decrypted = pkcs1.decrypt(encrypted, self.priv)
        print("\tDecrypted: %r" % decrypted)

        self.assertEqual(message, decrypted)
Beispiel #9
0
    def test_decoding_failure(self):
        message = struct.pack('>IIII', 0, 0, 0, 1)
        encrypted = pkcs1.encrypt(message, self.pub)

        # Alter the encrypted stream
        a = encrypted[5]
        if is_bytes(a):
            a = ord(a)
        altered_a = (a + 1) % 256
        encrypted = encrypted[:5] + byte(altered_a) + encrypted[6:]

        self.assertRaises(pkcs1.DecryptionError, pkcs1.decrypt, encrypted,
                          self.priv)
Beispiel #10
0
    def test_decoding_failure(self):
        message = struct.pack('>IIII', 0, 0, 0, 1)
        encrypted = pkcs1.encrypt(message, self.pub)

        # Alter the encrypted stream
        a = encrypted[5]
        if is_bytes(a):
            a = ord(a)
        altered_a = (a + 1) % 256
        encrypted = encrypted[:5] + byte(altered_a) + encrypted[6:]

        self.assertRaises(pkcs1.DecryptionError, pkcs1.decrypt, encrypted,
                          self.priv)
Beispiel #11
0
    def test_decoding_failure(self):
        message = struct.pack(">IIII", 0, 0, 0, 1)
        encrypted = pkcs1.encrypt(message, self.pub)

        # Alter the encrypted stream
        a = encrypted[5]
        self.assertIsInstance(a, int)

        altered_a = (a + 1) % 256
        encrypted = encrypted[:5] + byte(altered_a) + encrypted[6:]

        self.assertRaises(pkcs1.DecryptionError, pkcs1.decrypt, encrypted,
                          self.priv)
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 fetchCalendar(publicKey, postalCode, houseNumber):
    rsaPublicKey = RSA.importKey(publicKey)
    requestBody = {
        'a': False,
        'Email': None,
        'Password': None,
        'PostalCode': postalCode,
        'HouseNumber': houseNumber
    }

    # Sign request with public key
    encryptedRequest = pkcs1.encrypt(
        json.dumps(requestBody).encode(), rsaPublicKey)
    # Encode request
    base64EncodedRequest = b64encode(encryptedRequest).decode("utf-8")

    response = doPost("https://api-omrin.freed.nl//Account/FetchAccount/" +
                      appId,
                      data='"' + base64EncodedRequest + '"')

    return response['CalendarHomeV2']
Beispiel #14
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 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 #16
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 #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(cleartext, public_key):
    ciphertext = cry.encrypt(cleartext, public_key)
    return ciphertext