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


Python Padding.unpad方法代碼示例

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


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

示例1: decrypt_credential

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import unpad [as 別名]
def decrypt_credential(enc, secret=None):
    """
    Decodes data

    :param data: Data to be decoded
    :type data: str
    :returns:  string -- Decoded data
    """
    # pylint: disable=invalid-name,import-error
    import base64
    try:  # The crypto package depends on the library installed (see Wiki)
        from Crypto.Cipher import AES
        from Crypto.Util import Padding
    except ImportError:
        from Cryptodome.Cipher import AES
        from Cryptodome.Util import Padding
    enc = base64.b64decode(enc)
    iv = enc[:AES.block_size]
    cipher = AES.new(secret or get_crypt_key(), AES.MODE_CBC, iv)
    decoded = Padding.unpad(
        padded_data=cipher.decrypt(enc[AES.block_size:]),
        block_size=__BLOCK_SIZE__)
    return decoded 
開發者ID:CastagnaIT,項目名稱:plugin.video.netflix,代碼行數:25,代碼來源:credentials.py

示例2: get_cookie_data

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import unpad [as 別名]
def get_cookie_data(self, cookie):

        try:
            cookie = base64.b64decode(urllib.parse.unquote(cookie).split("--")[0])
            encrypted_data, iv = map(base64.b64decode, cookie.split("--".encode()))

            cipher = AES.new(self.secret, AES.MODE_CBC, iv)

            pt = unpad(cipher.decrypt(encrypted_data), AES.block_size)

            try:
                return json.loads(pt.decode())
            except:
                return json.loads(pt)
            
        except Exception as e: 
            return {"err": "Cookie decryption failed.", "code": str(e)} 
開發者ID:openstax,項目名稱:openstax-cms,代碼行數:19,代碼來源:auth.py

示例3: decrypt

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import unpad [as 別名]
def decrypt(cls, encrypted_text):
        """
        DES 解密
        :param encrypted_text: 待解密的字符串
        :return:  解密後的字符串
        """
        # "url safe"格式base64轉bytes
        base64_decrypted = base64.urlsafe_b64decode(encrypted_text)
        # AES初始化
        aes = AES.new(cls.secret_key, AES.MODE_ECB)
        # AES解密
        decrypted_text = aes.decrypt(base64_decrypted)
        # 去掉填充
        decrypted_text = unpad(decrypted_text, AES.block_size)
        # bytes轉字符串
        decrypted_text = decrypted_text.decode(encoding='utf-8')
        return decrypted_text 
開發者ID:gadfly0x,項目名稱:signature_algorithm,代碼行數:19,代碼來源:app.py

示例4: test1

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

示例5: test2

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

示例6: test3

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

示例7: test4

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

示例8: testn3

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import unpad [as 別名]
def testn3(self):
        self.assertRaises(ValueError, unpad, b("123456\x02"), 4)
        self.assertRaises(ValueError, unpad, b("123456\x00"), 4)
        self.assertRaises(ValueError, unpad, b("123456\x05\x05\x05\x05\x05"), 4) 
開發者ID:mortcanty,項目名稱:earthengine,代碼行數:6,代碼來源:test_Padding.py

示例9: testn1

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import unpad [as 別名]
def testn1(self):
        self.assertRaises(ValueError, unpad, b("123456\x81"), 4, 'iso7816') 
開發者ID:mortcanty,項目名稱:earthengine,代碼行數:4,代碼來源:test_Padding.py

示例10: decrypt

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import unpad [as 別名]
def decrypt(self, init_vector, ciphertext):
        """Decrypt a ciphertext"""
        cipher = AES.new(self.encryption_key, AES.MODE_CBC, init_vector)
        return Padding.unpad(cipher.decrypt(ciphertext), 16) 
開發者ID:CastagnaIT,項目名稱:plugin.video.netflix,代碼行數:6,代碼來源:default_crypto.py

示例11: decrypt_data

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import unpad [as 別名]
def decrypt_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)
        decrypted = cipher.decrypt(base64.b64decode(data))
        if decrypted:
            return unpad(decrypted, 16, 'pkcs7')
        else:
            return decrypted 
開發者ID:streamlink,項目名稱:streamlink,代碼行數:14,代碼來源:ustvnow.py

示例12: decrypt_aes_cbc

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import unpad [as 別名]
def decrypt_aes_cbc(self, encrypted, key, iv):
        key = AESCipher.mk_bytes(key)
        iv = AESCipher.mk_bytes(iv)
        res = AES.new(key, AES.MODE_CBC, iv=iv).decrypt(encrypted)
        res = Padding.unpad(res, AES.block_size)

        return res 
開發者ID:fufuok,項目名稱:FF.PyAdmin,代碼行數:9,代碼來源:asecipher.py

示例13: __DecryptContents

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import unpad [as 別名]
def __DecryptContents( self, contents: bytes, contentKeyBase64: str ) -> bytes:
		contentKey = base64.b64decode( contentKeyBase64 )
		keyAes = AES.new( self.DeviceIdUserIdKey, AES.MODE_ECB )
		decryptedContentKey = keyAes.decrypt( contentKey )

		contentAes = AES.new( decryptedContentKey, AES.MODE_ECB )
		decryptedContents = contentAes.decrypt( contents )
		return Padding.unpad( decryptedContents, AES.block_size, "pkcs7" ) 
開發者ID:TnS-hun,項目名稱:kobo-book-downloader,代碼行數:10,代碼來源:KoboDrmRemover.py

示例14: decrypt_AES256

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import unpad [as 別名]
def decrypt_AES256(data: bytes, key: bytes):
    # hmac should include IV
    mac = data[-32:]  # sha256 hmac at the end
    iv = data[:16]  # 16 Bytes for IV at the beginning
    message = data[16:-32]  # the rest is the message
    h = HMAC.new(key=key, msg=iv + message, digestmod=SHA256)
    h.verify(mac)
    decryption_cipher = AES.new(key, AES.MODE_CBC, iv=iv)
    decrypted_message = decryption_cipher.decrypt(message)
    # print(decrypted_message)
    # now to remove any padding that was added on to make it the right block size of 16
    return unpad(decrypted_message, 16) 
開發者ID:its-a-feature,項目名稱:Apfell,代碼行數:14,代碼來源:crypto.py

示例15: decrypt_string_003

# 需要導入模塊: from Crypto.Util import Padding [as 別名]
# 或者: from Crypto.Util.Padding import unpad [as 別名]
def decrypt_string_003(self, string_to_decrypt, encryption_key,
                                auth_key, uuid):
        components = string_to_decrypt.split(':')
        if len(components) == 6:
            version, auth_hash, local_uuid, IV, ciphertext, auth_params = components
        else:
            version, auth_hash, local_uuid, IV, ciphertext = components

        if local_uuid != uuid:
            print('Note UUID does not match.')
            print('This could be caused by a conflicted copy of a note.')
            print('Rename or delete the conflicted copy to fix.')
            print('Could also be a sign of tampering. Exiting for security...')
            logging.debug('UUID: {}, Local UUID: {}'.format(uuid, local_uuid))
            sys.exit(1)

        string_to_auth = ':'.join([version, uuid, IV, ciphertext])
        local_auth_hash = hmac.new(
                unhexlify(auth_key), string_to_auth.encode(), 'sha256').digest()

        auth_hash = unhexlify(auth_hash)
        if not hmac.compare_digest(local_auth_hash, auth_hash):
            print('Auth hash does not match. This could indicate tampering or '
                  'that something is wrong with the server. Exiting.')
            logging.debug('Auth Hash: {}, Local Auth Hash: {}'.format(auth_hash, local_auth_hash))
            sys.exit(1)

        cipher = AES.new(unhexlify(encryption_key), AES.MODE_CBC, unhexlify(IV))
        result = cipher.decrypt(b64decode(ciphertext))
        result = Padding.unpad(result, AES.block_size).decode()

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


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