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


Python RSA.importKey方法代码示例

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


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

示例1: create

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import importKey [as 别名]
def create(vek, keySizeBytes, certificatePath):
        #print("VEK: " + str(binascii.hexlify(vek)))
        publicKeyPem = open(certificatePath).read()
        publicKey = RSA.importKey(publicKeyPem)
        # Convert from PEM to DER

        lines = publicKeyPem.replace(" ", '').split()
        publicKeyDer = binascii.a2b_base64(''.join(lines[1:-1]))

        cert = x509.load_pem_x509_certificate(SmartStr(publicKeyPem), default_backend())
        subjectName = cert.subject.rfc4514_string()
        serial = cert.serial_number

        cipher = PKCS1_OAEP.new(key=publicKey, hashAlgo=SHA256, mgfunc=lambda x, y: pss.MGF1(x, y, SHA1))
        wrapped_key = cipher.encrypt(vek)
        #print("WrappedKey: " + str(binascii.hexlify(wrapped_key)))

        return CertEncryptedKeyBag(subjectName, serial, keySizeBytes, wrapped_key) 
开发者ID:aff4,项目名称:pyaff4,代码行数:20,代码来源:keybag.py

示例2: from_string

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import importKey [as 别名]
def from_string(key_pem, is_x509_cert):
      """Construct a Verified instance from a string.

      Args:
        key_pem: string, public key in PEM format.
        is_x509_cert: bool, True if key_pem is an X509 cert, otherwise it is
          expected to be an RSA key in PEM format.

      Returns:
        Verifier instance.
      """
      if is_x509_cert:
        pemLines = key_pem.replace(' ', '').split()
        certDer = _urlsafe_b64decode(''.join(pemLines[1:-1]))
        certSeq = DerSequence()
        certSeq.decode(certDer)
        tbsSeq = DerSequence()
        tbsSeq.decode(certSeq[0])
        pubkey = RSA.importKey(tbsSeq[6])
      else:
        pubkey = RSA.importKey(key_pem)
      return PyCryptoVerifier(pubkey) 
开发者ID:mortcanty,项目名称:earthengine,代码行数:24,代码来源:crypt.py

示例3: testVerify1

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import importKey [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

示例4: testEncrypt1

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import importKey [as 别名]
def testEncrypt1(self):
                for test in self._testData:
                        # Build the key
                        key = RSA.importKey(test[0])
                        # 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)
                        ct = cipher.encrypt(b(test[1]))
                        self.assertEqual(ct, t2b(test[2])) 
开发者ID:mortcanty,项目名称:earthengine,代码行数:21,代码来源:test_pkcs1_15.py

示例5: from_string

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import importKey [as 别名]
def from_string(key_pem, is_x509_cert):
      """Construct a Verified instance from a string.

      Args:
        key_pem: string, public key in PEM format.
        is_x509_cert: bool, True if key_pem is an X509 cert, otherwise it is
          expected to be an RSA key in PEM format.

      Returns:
        Verifier instance.

      Raises:
        NotImplementedError if is_x509_cert is true.
      """
      if is_x509_cert:
        raise NotImplementedError(
            'X509 certs are not supported by the PyCrypto library. '
            'Try using PyOpenSSL if native code is an option.')
      else:
        pubkey = RSA.importKey(key_pem)
      return PyCryptoVerifier(pubkey) 
开发者ID:splunk,项目名称:splunk-ref-pas-code,代码行数:23,代码来源:crypt.py

示例6: from_string

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import importKey [as 别名]
def from_string(key_pem, is_x509_cert):
      """Construct a Verified instance from a string.

      Args:
        key_pem: string, public key in PEM format.
        is_x509_cert: bool, True if key_pem is an X509 cert, otherwise it is
          expected to be an RSA key in PEM format.

      Returns:
        Verifier instance.

      Raises:
        NotImplementedError if is_x509_cert is true.
      """
      if is_x509_cert:
        # raise NotImplementedError(
        #     'X509 certs are not supported by the PyCrypto library. '
        #     'Try using PyOpenSSL if native code is an option.')
        key_pem = x509.get_pubkey(key_pem)
      pubkey = RSA.importKey(key_pem)
      return PyCryptoVerifier(pubkey) 
开发者ID:Schibum,项目名称:sndlatr,代码行数:23,代码来源:crypt.py

示例7: check_rsa_key

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import importKey [as 别名]
def check_rsa_key(sample):
   """
   Returns a 3-tuple (is_rsa_key, has_private_component, n_bit_length)
   
   is_rsa_key - a bool indicating that the sample is, in fact, an RSA key
      in a format readable by Crypto.PublicKey.RSA.importKey
   has_private_component - a bool indicating whether or not d was in the
      analyzed key, or false if the sample is not an RSA key
   n_bit_length - an int representing the bit length of the modulus found
      in the analyzed key, or False if the sample is not an RSA key
   """
   is_rsa_key = has_private_component = n_bit_length = False

   try:
      rsakey = RSA.importKey(sample.strip())
      is_rsa_key = True
      if rsakey.has_private():
         has_private_component = True
      n_bit_length = bit_length(rsakey.n)
   # Don't really care why it fails, just want to see if it did
   except:
      is_rsa_key = False
   return (is_rsa_key, has_private_component, n_bit_length) 
开发者ID:nccgroup,项目名称:featherduster,代码行数:25,代码来源:helpers.py

示例8: testSign1

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import importKey [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:proxysh,项目名称:Safejumper-for-Desktop,代码行数:22,代码来源:test_pkcs1_15.py

示例9: decrypt

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import importKey [as 别名]
def decrypt(cls, encrypted_file, key_file,
                out_file='output_dec', passphrase=''):
        """
        解密
        :param out_file:
        :param encrypted_file:
        :param key_file:
        :param passphrase:
        :return:
        """
        print('decrypt')
        with open(key_file, "r") as kf:
            rsa = RSA.importKey(kf.read(), passphrase=passphrase)
            with open(encrypted_file, 'rb') as df:
                data = rsa.decrypt(df.read())
                print('data:\n')
                print(data)
                print('hex:')
                print(data.encode('hex'))
                with open(out_file, "wb") as of:
                    of.write(data) 
开发者ID:restran,项目名称:hacker-scripts,代码行数:23,代码来源:rsa_helper.py

示例10: encrypt

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import importKey [as 别名]
def encrypt(cls, raw_file, key_file, out_file='output_enc', passphrase=''):
        """
        加密
        :param out_file:
        :param raw_file:
        :param key_file:
        :param passphrase:
        :return:
        """
        print('encrypt')
        with open(key_file, "r") as kf:
            rsa = RSA.importKey(kf.read(), passphrase=passphrase)
            with open(raw_file, 'rb') as df:
                data = rsa.encrypt(df.read(), 0)
                print('data:')
                print(data)
                print('hex:')
                print(data.encode('hex'))
                with open(out_file, "wb") as of:
                    of.write(data[0]) 
开发者ID:restran,项目名称:hacker-scripts,代码行数:22,代码来源:rsa_helper.py

示例11: prepare_key

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import importKey [as 别名]
def prepare_key(self, key):

        if isinstance(key, RSA._RSAobj):
            return key

        if isinstance(key, string_types):
            if isinstance(key, text_type):
                key = key.encode('utf-8')

            key = RSA.importKey(key)
        else:
            raise TypeError('Expecting a PEM- or RSA-formatted key.')

        return key 
开发者ID:danielecook,项目名称:gist-alfred,代码行数:16,代码来源:pycrypto.py

示例12: parseN

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import importKey [as 别名]
def parseN(argv,index):
    file = open(argv[index],'r')
    fileInput = ''.join(file.readlines()).strip()
    try:
        fileInput = long(fileInput)
        return fileInput
    except ValueError:
        from Crypto.PublicKey import RSA
        return long(RSA.importKey(fileInput).__getattr__('n'))
        pass 
开发者ID:JulesDT,项目名称:RSA-Hastad,代码行数:12,代码来源:rsaHastad.py

示例13: import_key

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import importKey [as 别名]
def import_key(key_path: str) -> RSA:
    with open(key_path, 'r') as f:
        return RSA.importKey(f.read()) 
开发者ID:Salamek,项目名称:gitlab-tools,代码行数:5,代码来源:crypto.py

示例14: verify_incoming_request

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import importKey [as 别名]
def verify_incoming_request(form_data: dict) -> bool:
    """verify the incoming form_data"""
    # copy form data
    input_data = form_data.copy()

    signature = input_data["p_signature"]

    # Remove the p_signature parameter
    del input_data["p_signature"]

    # Ensure all the data fields are strings
    for field in input_data:
        input_data[field] = str(input_data[field])

    # Sort the data
    sorted_data = collections.OrderedDict(sorted(input_data.items()))

    # and serialize the fields
    serialized_data = phpserialize.dumps(sorted_data)

    # verify the data
    key = RSA.importKey(public_key_der)
    digest = SHA1.new()
    digest.update(serialized_data)
    verifier = PKCS1_v1_5.new(key)
    signature = base64.b64decode(signature)
    if verifier.verify(digest, signature):
        return True
    return False 
开发者ID:simple-login,项目名称:app,代码行数:31,代码来源:paddle_utils.py

示例15: decrypt_rsa

# 需要导入模块: from Crypto.PublicKey import RSA [as 别名]
# 或者: from Crypto.PublicKey.RSA import importKey [as 别名]
def decrypt_rsa(key, data):
    rsa_key = RSA.importKey(key)
    return rsa_key.decrypt(data)


# XOR 
开发者ID:kevthehermit,项目名称:RATDecoders,代码行数:8,代码来源:crypto.py


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