学堂 学堂 学堂公众号手机端

Python中常见的字符串加密方法包括

lewis 2年前 (2023-11-07) 阅读数 7 #技术

Python中常见的字符串加密方法包括:

  1. 使用hashlib模块进行哈希加密,如MD5、SHA-1、SHA-256等加密算法。 示例代码:
importhashlib text="Hello,World!" hashed_text=hashlib.md5(text.encode()).hexdigest() print(hashed_text)
  • 使用base64模块进行base64编码。 示例代码:
  • importbase64 text="Hello,World!" encoded_text=base64.b64encode(text.encode()).decode() print(encoded_text)
  • 使用cryptography模块进行对称加密或非对称加密。 示例代码:
  • fromcryptography.fernetimportFernet key=Fernet.generate_key() cipher=Fernet(key) text="Hello,World!" encrypted_text=cipher.encrypt(text.encode()) print(encrypted_text) decrypted_text=cipher.decrypt(encrypted_text).decode() print(decrypted_text)

    以上是几种常见的字符串加密方法,具体选择哪种方法取决于加密需求和安全性要求。


    版权声明

    本文仅代表作者观点,不代表博信信息网立场。

    热门