當前位置: 首頁>>代碼示例>>Python>>正文


Python Padding.pad方法代碼示例

本文整理匯總了Python中Crypto.Util.Padding.pad方法的典型用法代碼示例。如果您正苦於以下問題:Python Padding.pad方法的具體用法?Python Padding.pad怎麽用?Python Padding.pad使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Crypto.Util.Padding的用法示例。


在下文中一共展示了Padding.pad方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: encrypt

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import pad [as 別名]
def encrypt(self, plaintext, esn):
        """
        Encrypt the given Plaintext with the encryption key
        :param plaintext:
        :return: Serialized JSON String of the encryption Envelope
        """
        init_vector = get_random_bytes(16)
        cipher = AES.new(self.encryption_key, AES.MODE_CBC, init_vector)
        ciphertext = base64.standard_b64encode(
            cipher.encrypt(Padding.pad(plaintext.encode('utf-8'), 16))).decode('utf-8')
        encryption_envelope = {
            'ciphertext': ciphertext,
            'keyid': '_'.join((esn, str(self.sequence_number))),
            'sha256': 'AA==',
            'iv': base64.standard_b64encode(init_vector).decode('utf-8')
        }
        return json.dumps(encryption_envelope) 
開發者ID:CastagnaIT,項目名稱:plugin.video.netflix,代碼行數:19,代碼來源:default_crypto.py

示例2: encrypt_credential

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import pad [as 別名]
def encrypt_credential(raw):
    """
    Encodes data

    :param data: Data to be encoded
    :type data: str
    :returns:  string -- Encoded data
    """
    # pylint: disable=invalid-name,import-error
    import base64
    try:  # The crypto package depends on the library installed (see Wiki)
        from Crypto import Random
        from Crypto.Cipher import AES
        from Crypto.Util import Padding
    except ImportError:
        from Cryptodome import Random
        from Cryptodome.Cipher import AES
        from Cryptodome.Util import Padding
    raw = bytes(Padding.pad(data_to_pad=raw.encode('utf-8'), block_size=__BLOCK_SIZE__))
    iv = Random.new().read(AES.block_size)
    cipher = AES.new(get_crypt_key(), AES.MODE_CBC, iv)
    return base64.b64encode(iv + cipher.encrypt(raw)).decode('utf-8') 
開發者ID:CastagnaIT,項目名稱:plugin.video.netflix,代碼行數:24,代碼來源:credentials.py

示例3: auth_digital

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import pad [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

示例4: encrypt_item

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import pad [as 別名]
def encrypt_item(self, item, keys):
        uuid = item['uuid']
        content = json.dumps(item['content'])

        logging.debug('Encrypting item {} with content: {}'.format(uuid, content))

        # all this is to follow the Standard Notes spec
        item_key = hex(random.getrandbits(AES_KEY_LEN * 2))
        # remove '0x', pad with 0's, then split in half
        item_key = item_key[2:].rjust(AES_STR_KEY_LEN * 2, '0')
        item_ek = item_key[:AES_STR_KEY_LEN]
        item_ak = item_key[AES_STR_KEY_LEN:]

        enc_item = deepcopy(item)
        enc_item['content'] = self.encrypt_string_003(
                content, item_ek, item_ak, uuid)
        enc_item['enc_item_key'] = self.encrypt_string_003(
                item_key, keys['mk'], keys['ak'], uuid)

        return enc_item 
開發者ID:tannercollin,項目名稱:standardnotes-fs,代碼行數:22,代碼來源:crypt.py

示例5: encrypt_string_003

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import pad [as 別名]
def encrypt_string_003(self, string_to_encrypt, encryption_key,
                                auth_key, uuid):
        IV = hex(random.getrandbits(AES_IV_LEN))
        IV = IV[2:].rjust(AES_STR_IV_LEN, '0') # remove '0x', pad with 0's

        cipher = AES.new(unhexlify(encryption_key), AES.MODE_CBC, unhexlify(IV))
        pt = string_to_encrypt.encode()
        padded_pt = Padding.pad(pt, AES.block_size)
        ciphertext = b64encode(cipher.encrypt(padded_pt)).decode()

        string_to_auth = ':'.join(['003', uuid, IV, ciphertext])
        auth_hash = hmac.new(
                unhexlify(auth_key), string_to_auth.encode(), 'sha256').digest()
        auth_hash = hexlify(auth_hash).decode()

        result = ':'.join(['003', auth_hash, uuid, IV, ciphertext])

        return result 
開發者ID:tannercollin,項目名稱:standardnotes-fs,代碼行數:20,代碼來源:crypt.py

示例6: encrypt

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import pad [as 別名]
def encrypt(self, command):
        from Crypto.Util import Padding
        if not self._encryption_ready:
            return command
        if self._salt != "" and self.new_salt_needed():
            prev_salt = self._salt
            self._salt = self.genarate_salt()
            s = "nextSalt/{}/{}/{}\0".format(prev_salt, self._salt, command)
        else:
            if self._salt == "":
                self._salt = self.genarate_salt()
            s = "salt/{}/{}\0".format(self._salt, command)
        s = Padding.pad(bytes(s, "utf-8"), 16)
        aes_cipher = self.get_new_aes_chiper()
        encrypted = aes_cipher.encrypt(s)
        encoded = b64encode(encrypted)
        encoded_url = req.pathname2url(encoded.decode("utf-8"))
        return CMD_ENCRYPT_CMD + encoded_url 
開發者ID:JoDehli,項目名稱:PyLoxone,代碼行數:20,代碼來源:__init__.py

示例7: test1

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import pad [as 別名]
def test1(self):
        padded = pad(b(""), 4)
        self.failUnless(padded == uh(b("04040404")))
        padded = pad(b(""), 4, 'pkcs7')
        self.failUnless(padded == uh(b("04040404")))
        back = unpad(padded, 4)
        self.failUnless(back == b("")) 
開發者ID:mortcanty,項目名稱:earthengine,代碼行數:9,代碼來源:test_Padding.py

示例8: test2

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import pad [as 別名]
def test2(self):
        padded = pad(uh(b("12345678")), 4)
        self.failUnless(padded == uh(b("1234567804040404")))
        back = unpad(padded, 4)
        self.failUnless(back == uh(b("12345678"))) 
開發者ID:mortcanty,項目名稱:earthengine,代碼行數:7,代碼來源:test_Padding.py

示例9: test3

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import pad [as 別名]
def test3(self):
        padded = pad(uh(b("123456")), 4)
        self.failUnless(padded == uh(b("12345601")))
        back = unpad(padded, 4)
        self.failUnless(back == uh(b("123456"))) 
開發者ID:mortcanty,項目名稱:earthengine,代碼行數:7,代碼來源:test_Padding.py

示例10: test4

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import pad [as 別名]
def test4(self):
        padded = pad(uh(b("1234567890")), 4)
        self.failUnless(padded == uh(b("1234567890030303")))
        back = unpad(padded, 4)
        self.failUnless(back == uh(b("1234567890"))) 
開發者ID:mortcanty,項目名稱:earthengine,代碼行數:7,代碼來源:test_Padding.py

示例11: encrypt_string

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import pad [as 別名]
def encrypt_string(self, string_to_encrypt: str) -> str:
        # This is needed to remove the escaping added by Python. For example, if we find in smali the instruction
        # const-string v0, "\"message\"" Android will treat it as "message" while in Python it's \"message\",
        # so we need to encrypt "message" and not \"message\" (we have to remove the unnecessary escaping, otherwise
        # the backslashes would by encrypted as part of the string).
        string_to_encrypt = string_to_encrypt.encode(errors='replace').decode('unicode_escape')

        key = PBKDF2(password=self.encryption_secret, salt=self.encryption_secret.encode(), dkLen=32, count=128)
        encrypted_string = hexlify(AES.new(key=key, mode=AES.MODE_ECB)
                                   .encrypt(pad(string_to_encrypt.encode(errors='replace'), AES.block_size))).decode()
        return encrypted_string 
開發者ID:ClaudiuGeorgiu,項目名稱:Obfuscapk,代碼行數:13,代碼來源:const_string_encryption.py

示例12: encrypt_string

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import pad [as 別名]
def encrypt_string(self, string_to_encrypt: str) -> str:
        # This is needed to remove the escaping added by Python. For example, if we find in string resources
        # the string "\"message\"" Android will treat it as "message" while in Python it's \"message\", so we
        # need to encrypt "message" and not \"message\" (we have to remove the unnecessary escaping, otherwise
        # the backslashes would by encrypted as part of the string).
        string_to_encrypt = string_to_encrypt.encode(errors='replace').decode('unicode_escape')

        key = PBKDF2(password=self.encryption_secret, salt=self.encryption_secret.encode(), dkLen=32, count=128)
        encrypted_string = hexlify(AES.new(key=key, mode=AES.MODE_ECB)
                                   .encrypt(pad(string_to_encrypt.encode(errors='replace'), AES.block_size))).decode()
        return encrypted_string 
開發者ID:ClaudiuGeorgiu,項目名稱:Obfuscapk,代碼行數:13,代碼來源:res_string_encryption.py

示例13: encrypt_data

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import pad [as 別名]
def encrypt_data(cls, data, key, iv):
        rkey = "".join(reversed(key)).encode('utf8')
        riv = "".join(reversed(iv)).encode('utf8')

        fkey = SHA256.new(rkey).hexdigest()[:32].encode("utf8")

        cipher = AES.new(fkey, AES.MODE_CBC, riv)
        encrypted = cipher.encrypt(pad(data, 16, 'pkcs7'))
        return base64.b64encode(encrypted) 
開發者ID:streamlink,項目名稱:streamlink,代碼行數:11,代碼來源:ustvnow.py

示例14: encrypt

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import pad [as 別名]
def encrypt(hstr, payload, encryptionKey):
    paddedEncryptionKey = pad(encryptionKey, AES.block_size)
    key = base64.b64encode(paddedEncryptionKey)
    cipher = AES.new(key, AES.MODE_CTR)

    hstrBytes = base64.b64encode(cipher.encrypt(hstr)).decode('utf-8')

    payloadBytes = base64.b64encode(cipher.encrypt(payload)).decode('utf-8')
    nonce = base64.b64encode(cipher.nonce).decode('utf-8')

    return [hstrBytes, payloadBytes, nonce] 
開發者ID:oduwsdl,項目名稱:ipwb,代碼行數:13,代碼來源:indexer.py

示例15: mk_pad_bytes

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import pad [as 別名]
def mk_pad_bytes(data_to_pad, length=AES.block_size):
        """字節串補齊"""
        return Padding.pad(AESCipher.mk_bytes(data_to_pad), length) 
開發者ID:fufuok,項目名稱:FF.PyAdmin,代碼行數:5,代碼來源:asecipher.py


注:本文中的Crypto.Util.Padding.pad方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。