当前位置: 首页>>代码示例>>Python>>正文


Python RSA.construct方法代码示例

本文整理汇总了Python中Crypto.PublicKey.RSA.construct方法的典型用法代码示例。如果您正苦于以下问题:Python RSA.construct方法的具体用法?Python RSA.construct怎么用?Python RSA.construct使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Crypto.PublicKey.RSA的用法示例。


在下文中一共展示了RSA.construct方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: testSign1

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import construct [as 别名]
def testSign1(self):
                for i in range(len(self._testData)):
                        # Build the key
                        comps = [ long(rws(self._testData[i][0][x]),16) for x in ('n','e','d') ]
                        key = MyKey(RSA.construct(comps))
                        # Hash function
                        h = self._testData[i][4].new()
                        # Data to sign
                        h.update(t2b(self._testData[i][1]))
                        # Salt
                        test_salt = t2b(self._testData[i][3])
                        key._randfunc = lambda N: test_salt
                        # The real test
                        signer = PKCS.new(key)
                        self.failUnless(signer.can_sign())
                        s = signer.sign(h)
                        self.assertEqual(s, t2b(self._testData[i][2])) 
开发者ID:mortcanty,项目名称:earthengine,代码行数:19,代码来源:test_pkcs1_pss.py

示例2: testVerify1

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import construct [as 别名]
def testVerify1(self):
               for i in range(len(self._testData)):
                        # Build the key
                        comps = [ long(rws(self._testData[i][0][x]),16) for x in ('n','e') ]
                        key = MyKey(RSA.construct(comps))
                        # Hash function
                        h = self._testData[i][4].new()
                        # Data to sign
                        h.update(t2b(self._testData[i][1]))
                        # Salt
                        test_salt = t2b(self._testData[i][3])
                        # The real test
                        key._randfunc = lambda N: test_salt
                        verifier = PKCS.new(key)
                        self.failIf(verifier.can_sign())
                        result = verifier.verify(h, t2b(self._testData[i][2]))
                        self.failUnless(result) 
开发者ID:mortcanty,项目名称:earthengine,代码行数:19,代码来源:test_pkcs1_pss.py

示例3: testSign1

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import construct [as 别名]
def testSign1(self):
                for i in range(len(self._testData)):
                        row = self._testData[i]
                        # Build the key
                        if isStr(row[0]):
                                key = RSA.importKey(row[0])
                        else:
                                comps = [ long(rws(row[0][x]),16) for x in ('n','e','d') ]
                                key = RSA.construct(comps)
                        h = row[3].new()
                        # Data to sign can either be in hex form or not
                        try:
                            h.update(t2b(row[1]))
                        except:
                            h.update(b(row[1]))
                        # The real test
                        signer = PKCS.new(key)
                        self.failUnless(signer.can_sign())
                        s = signer.sign(h)
                        self.assertEqual(s, t2b(row[2])) 
开发者ID:mortcanty,项目名称:earthengine,代码行数:22,代码来源:test_pkcs1_15.py

示例4: testVerify1

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import construct [as 别名]
def testVerify1(self):
                for i in range(len(self._testData)):
                        row = self._testData[i]
                        # Build the key
                        if isStr(row[0]):
                                key = RSA.importKey(row[0]).publickey()
                        else:
                                comps = [ long(rws(row[0][x]),16) for x in ('n','e') ]
                                key = RSA.construct(comps)
                        h = row[3].new()
                        # Data to sign can either be in hex form or not
                        try:
                            h.update(t2b(row[1]))
                        except:
                            h.update(b(row[1]))
                        # The real test
                        verifier = PKCS.new(key)
                        self.failIf(verifier.can_sign())
                        result = verifier.verify(h, t2b(row[2]))
                        self.failUnless(result) 
开发者ID:mortcanty,项目名称:earthengine,代码行数:22,代码来源:test_pkcs1_15.py

示例5: testEncrypt1

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import construct [as 别名]
def testEncrypt1(self):
                # Verify encryption using all test vectors
                for test in self._testData:
                        # Build the key
                        comps = [ long(rws(test[0][x]),16) for x in ('n','e') ]
                        key = RSA.construct(comps)
                        # RNG that takes its random numbers from a pool given
                        # at initialization
                        class randGen:
                            def __init__(self, data):
                                self.data = data
                                self.idx = 0
                            def __call__(self, N):
                                r = self.data[self.idx:N]
                                self.idx += N
                                return r
                        # The real test
                        key._randfunc = randGen(t2b(test[3]))
                        cipher = PKCS.new(key, test[4])
                        ct = cipher.encrypt(t2b(test[1]))
                        self.assertEqual(ct, t2b(test[2])) 
开发者ID:mortcanty,项目名称:earthengine,代码行数:23,代码来源:test_pkcs1_oaep.py

示例6: encryptPassword

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import construct [as 别名]
def encryptPassword(email, password):
    gdpk = "AAAAgMom/1a/v0lblO2Ubrt60J2gcuXSljGFQXgcyZWveWLEwo6prwgi3iJIZdodyhKZQrNWp5nKJ3srRXcUW+F1BD3baEVGcmEgqaLZUNBjm057pKRI16kB0YppeGx5qIQ5QjKzsR8ETQbKLNWgRY0QRNVz34kMJR3P/LgHax/6rmf5AAAAAwEAAQ=="
    binaryKey = b64decode(gdpk).encode('hex')
    
    half = binaryKey[8:264]
    modulus = long(half, 16)
    
    half = binaryKey[272:278]
    exponent = long(half, 16)
    
    sha1hash = sha1(b64decode(gdpk)).digest()
    signature = "00" + sha1hash[:4].encode('hex')
    
    key = RSA.construct((modulus, exponent))
    cipher = PKCS1_OAEP.new(key)
    plain = email + "\x00" + password
    encrypted = cipher.encrypt(plain).encode('hex')
    ste = signature + encrypted
    output = unhexlify(ste)
    
    encryptedPassword = b64encode(output).encode('ascii').replace("+","-").replace("/","_")
    return encryptedPassword 
开发者ID:rxw,项目名称:snapy,代码行数:24,代码来源:utils.py

示例7: getDSAKey

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import construct [as 别名]
def getDSAKey(self):
        """
        Return a PyCrypto DSA key to support the tests.

        @return: The DSA key to support the tests.
        @rtype: C{Crypto.PublicKey.DSA}
        """
        # Use lazy import as PyCrypto will be deprecated.
        from Crypto.PublicKey import DSA

        return DSA.construct((
            keydata.DSAData['y'],
            keydata.DSAData['g'],
            keydata.DSAData['p'],
            keydata.DSAData['q'],
            keydata.DSAData['x'],
            )) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:19,代码来源:test_keys.py

示例8: test_keyObjectSetRSAPublic

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import construct [as 别名]
def test_keyObjectSetRSAPublic(self):
        """
        Setting the L{keys.Key.keyObject} property to a PyCrypto public RSA key
        instance updates the internal key.
        """
        key = keys.Key.fromString(keydata.publicDSA_openssh)
        newPyCryptoKey = Crypto.PublicKey.RSA.construct((
            keydata.RSAData['n'],
            keydata.RSAData['e'],
            ))
        self.assertEqual('DSA', key.type())

        key.keyObject = newPyCryptoKey
        [warning] = self.flushWarnings([
            KeyKeyObjectTests.test_keyObjectSetRSAPublic])
        self.assertIs(warning['category'], DeprecationWarning)

        self.assertEqual('RSA', key.type())
        self.assertEqual({
            'n': keydata.RSAData['n'],
            'e': keydata.RSAData['e'],
            },
            key.data()) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:25,代码来源:test_keys.py

示例9: test_keyObjectSetDSAPublic

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import construct [as 别名]
def test_keyObjectSetDSAPublic(self):
        """
        Setting the L{keys.Key.keyObject} property to a PyCrypto public DSA key
        instance updates the internal key.
        """
        key = keys.Key.fromString(keydata.publicRSA_openssh)
        newPyCryptoKey = Crypto.PublicKey.DSA.construct((
            keydata.DSAData['y'],
            keydata.DSAData['g'],
            keydata.DSAData['p'],
            keydata.DSAData['q'],
            ))
        self.assertEqual('RSA', key.type())

        key.keyObject = newPyCryptoKey

        self.assertEqual('DSA', key.type())
        self.assertEqual({
            'y': keydata.DSAData['y'],
            'g': keydata.DSAData['g'],
            'p': keydata.DSAData['p'],
            'q': keydata.DSAData['q'],
            },
            key.data()) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:26,代码来源:test_keys.py

示例10: test_constructorPyCrypto

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import construct [as 别名]
def test_constructorPyCrypto(self):
        """
        Passing a PyCrypto key object to L{keys.Key} is deprecated.
        """
        pycryptoKey = Crypto.PublicKey.RSA.construct((
            keydata.RSAData['n'],
            keydata.RSAData['e']))
        key = self.callDeprecated(
            (Version('Twisted', 16, 0, 0),
             'passing a cryptography key object'),
            keys.Key,
            pycryptoKey)
        self.assertEqual('RSA', key.type())
        self.assertEqual({
            'n': keydata.RSAData['n'],
            'e': keydata.RSAData['e'],
            },
            key.data()) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:20,代码来源:test_keys.py

示例11: signer

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import construct [as 别名]
def signer(M):
	message = M
	p = getPrime(512)
	q = getPrime(512)
	n = p*q
	phin = (p-1)*(q-1)
	e = 65537
	
	assert GCD(e, phin) == 1
	key = RSA.construct((long(n), long(e)))

	h = MD5.new(M)
	M = PKCS1_v1_5.EMSA_PKCS1_V1_5_ENCODE(h, size(key.n)/8)
	print "Padded M: ", M.encode("hex")
	M = bytes_to_long(M)

	d = inverse(e, phin)
	s = pow(M, d, n)
	s = long_to_bytes(s)
	return (key, s, message) 
开发者ID:ashutosh1206,项目名称:Crypton,代码行数:22,代码来源:example.py

示例12: auth_digital

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import construct [as 别名]
def auth_digital(self, title_id, title_version, device_token, ticket):
		self.verify_ticket(ticket, title_id)
		
		plain_key = get_random_bytes(16)
		
		aes = AES.new(plain_key, AES.MODE_CBC, iv=bytes(16))
		encrypted_ticket = aes.encrypt(pad(ticket, 16))
		
		rsa_key = RSA.construct((RSA_MODULUS, RSA_EXPONENT))
		rsa = PKCS1_OAEP.new(rsa_key, SHA256)
		encrypted_key = rsa.encrypt(plain_key)
	
		req = HTTPRequest.post("/v3/application_auth_token")
		req.form["application_id"] = "%016x" %title_id
		req.form["application_version"] = "%08x" %title_version
		req.form["device_auth_token"] = device_token
		req.form["media_type"] = "DIGITAL"
		
		req.form["cert"] = b64encode(encrypted_ticket)
		req.form["cert_key"] = b64encode(encrypted_key)
	
		response = self.request(req, True)
		return response.json 
开发者ID:Kinnay,项目名称:NintendoClients,代码行数:25,代码来源:aauth.py

示例13: construct_private_key

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import construct [as 别名]
def construct_private_key(
    n: int, e: int, d: int, format: str = "PEM", passphrase: str = None
) -> str:  # pragma: no cover
    """Construct a private key given n, e and d
    
    Args:
        n (int): n
        e (int): e
        d (int): d
        format (str, optional): Supports PEM, DER and OpenSSH. Defaults to "PEM".
        passphrase (str, optional): [description]. Defaults to None.
    
    Returns:
        str: Private key
    """
    valid_formats = ["PEM", "DER", "OpenSSH"]
    assert format in valid_formats, "Valid formats are {}".format(
        " ".join(valid_formats)
    )
    priv = RSA.construct((n, e, d))
    return priv.export_key(format=format, passphrase=passphrase) 
开发者ID:securisec,项目名称:chepy,代码行数:23,代码来源:crypto_extras.py

示例14: common_primes

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import construct [as 别名]
def common_primes(keys):
    """Find common prime in keys modules

    Args:
        keys(list): RSAKeys

    Returns:
        list: RSAKeys for which factorization of n was found
    """
    priv_keys = []
    for pair in itertools.combinations(keys, 2):
        prime = gmpy2.gcd(pair[0].n, pair[1].n)
        if prime != 1:
            log.success("Found common prime in: {}, {}".format(pair[0].identifier, pair[1].identifier))
            for key_no in range(2):
                if pair[key_no] not in priv_keys:
                    d = int(invmod(pair[key_no].e, (prime - 1) * (pair[key_no].n // prime - 1)))
                    new_key = RSAKey.construct(int(pair[key_no].n), int(pair[key_no].e), int(d),
                                               identifier=pair[key_no].identifier + '-private')
                    new_key.texts = pair[key_no].texts[:]
                    priv_keys.append(new_key)
                else:
                    log.debug("Key {} already in priv_keys".format(pair[key_no].identifier))
    return priv_keys 
开发者ID:GrosQuildu,项目名称:CryptoAttacks,代码行数:26,代码来源:rsa.py

示例15: wiener

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import construct [as 别名]
def wiener(key):
    """Wiener small private exponent attack
     If d < (1/3)*(N**(1/4)), d can be effectively recovered using continuous fractions

     Args:
        key(RSAKey): public rsa key to break

    Returns:
        NoneType/RSAKey: None if didn't break key, private key otherwise
    """
    en_fractions = continued_fractions(key.e, key.n)
    for k, d in convergents(en_fractions):
        if k != 0 and (key.e * d - 1) % k == 0:
            phi = (key.e * d - 1) // k
            """ p**2 - p*(n - phi + 1) + n == 0 """
            b = key.n - phi + 1
            delta = b * b - 4 * key.n
            if delta > 0:
                sqrt_delta = gmpy2.isqrt(delta)
                if sqrt_delta * sqrt_delta == delta and sqrt_delta % 2 == 0:
                    log.debug("Found private key (d={}) for {}".format(d, key.identifier))
                    new_key = RSAKey.construct(key.n, key.e, d, identifier=key.identifier + '-private')
                    new_key.texts = key.texts[:]
                    return new_key
    return None 
开发者ID:GrosQuildu,项目名称:CryptoAttacks,代码行数:27,代码来源:rsa.py


注:本文中的Crypto.PublicKey.RSA.construct方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。