保护密码安全的终极解决方案:密码加密、解密和管理代码

引言

在数字时代,我们面临着越来越多的在线账号和密码。然而,密码的安全性成为了一个重要的问题。为了保护个人隐私和数据安全,我们需要一种可靠的方式来加密、存储和管理密码。本文将介绍一种密码加密、解密和管理的终极解决方案,帮助您更好地保护密码安全

密码加密 

密码加密是保护密码安全的关键步骤。下面是一个密码加密的代码示例:

# 密码加密模块

import json
from cryptography.fernet import Fernet
import base64
import os

save_path = r'D:\缓存数据\password.json'

# 检查密码存储文件是否存在,如果不存在则创建一个空的JSON文件
if not os.path.isfile(save_path):
    with open(save_path, 'w') as f:
        json.dump({}, f)

def encryption(platform, user, password):
    """
    将给定的密码加密并保存到密码存储文件中

    参数:
    platform (str):账号所在平台
    user (str):注册的用户名
    password (str):注册时填的密码

    返回值:
    无

    """
    # 生成加密所需的密钥
    key = Fernet.generate_key().decode()
    cipher_suite = Fernet(key.encode())

    # 将密码转换为字节型并加密
    password = password.encode()
    cipher_text = cipher_suite.encrypt(password)
    cipher_text_str = base64.b64encode(cipher_text).decode('utf-8')

    # 读取密码存储文件的数据
    with open(save_path, 'r') as f:
        source_data = json.load(f)

    # 检查平台是否已存在
    initialization = source_data.get(platform)
    if initialization:
        # 检查用户名是否已注册
        registered = initialization.get(user)
        if registered:
            print('该账号已注册')
            return
        else:
            initialization[user] = [cipher_text_str, key]
    else:
        source_data[platform] = {user: [cipher_text_str, key]}

    # 保存更新后的数据到密码存储文件
    with open(save_path, 'w') as f:
        json.dump(source_data, f)

    print('密码加密完成')

在上面的代码中,我们使用了cryptography 库中的 Fernet 算法来实现密码加密。该算法使用对称加密方式,生成一个密钥并将密码加密后保存

密码解密

为了能够方便地使用加密后的密码,我们需要提供密码解密的功能。下面是一个密码解密的代码示例:

# 密码解密模块

import json
from cryptography.fernet import Fernet
import base64
import os

save_path = r'D:\缓存数据\password.json'

def decrypt(platform, user):
    """
    解密指定平台下指定用户名的密码

    参数:
    platform (str):账号所在平台
    user (str):注册的用户名

    返回值:
    tuple:包含用户名和解密后的密码

    """
    # 读取密码存储文件的数据
    with open(save_path, 'r') as f:
        data = json.load(f)

    resp = data.get(platform)
    if resp:
        result = resp.get(str(user))
        if result:
            cipher_text_str = result[0]
            saved_key = result[-1]

            # 解码加密后的密码并进行解密
            cipher_text = base64.b64decode(cipher_text_str)
            cipher_suite = Fernet(saved_key.encode())
            password = cipher_suite.decrypt(cipher_text)

            return user, password.decode()
        else:
            print('请先注册')
    else:
        print('请先注册')

在上面的代码中,我们使用了与密码加密相同的密钥来解密加密后的密码

 密码删除

如果我们需要删除某个平台下的特定用户名和密码,可以使用以下代码示例:

# 密码删除模块

import json
import os

save_path = r'D:\缓存数据\password.json'

def delete(platform, user):
    """
    删除指定平台下指定用户名的密码

    参数:
    platform (str):账号所在平台
    user (str):注册的用户名

    返回值:
    无

    """
    # 读取密码存储文件的数据
    with open(save_path, 'r') as f:
        data = json.load(f)

    dic = data.get(platform)
    if dic:
        try:
            del dic[str(user)]
            print('删除成功')
        except KeyError:
            print('用户名不存在')

        # 保存更新后的数据到密码存储文件
        with open(save_path, 'w') as f:
            json.dump(data, f)
    else:
        print(fr'未查询到{platform}下注册信息')

在上面的代码中,我们从密码存储文件中读取数据,并删除指定平台下的指定用户名

示例和应用 

下面是如何使用这些模块进行密码加密、解密和删除的示例:

# 示例和应用

encryption('yinweida', '123xxxxxxxx', '123456')
print(decrypt('yinweida', '123xxxxxxxx'))
delete('yinweida', '123xxxxxxxx')

上面的示例代码将在平台"yinweida"下加密用户名为"123xxxxxxxx"的密码,然后解密该密码,并最后删除该用户名和密码

结论

使用Base64编码和Fernet算法的密码加密、解密和删除方案,有效保护密码安全。Base64编码将密码转换为可打印字符,Fernet算法提供了可靠的加密和解密功能。通过这些模块,我们可以安全地存储和管理密码,保护个人隐私

  • 23
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用Python代码实现Hill密码加密解密的示例代码: ```python import numpy as np # 加密函数 def hill_encrypt(plain_text, key): # 将明文转换为数字序列 plain_num = [ord(c) - ord('a') for c in plain_text.lower()] # 将数字序列转换为矩阵 plain_matrix = np.array(plain_num).reshape(-1, 1) # 将密钥转换为矩阵 key_matrix = np.array(key) # 计算加密后的矩阵 cipher_matrix = np.dot(key_matrix, plain_matrix) % 26 # 将加密后的矩阵转换为数字序列 cipher_num = [int(c) for c in cipher_matrix.reshape(1, -1)[0]] # 将数字序列转换为密文 cipher_text = ''.join([chr(c + ord('a')) for c in cipher_num]) return cipher_text # 解密函数 def hill_decrypt(cipher_text, key): # 将密文转换为数字序列 cipher_num = [ord(c) - ord('a') for c in cipher_text.lower()] # 将数字序列转换为矩阵 cipher_matrix = np.array(cipher_num).reshape(-1, 1) # 将密钥转换为矩阵 key_matrix = np.array(key) # 计算解密后的矩阵 inv_key_matrix = np.linalg.inv(key_matrix) plain_matrix = np.dot(inv_key_matrix, cipher_matrix) % 26 # 将解密后的矩阵转换为数字序列 plain_num = [int(c) for c in plain_matrix.reshape(1, -1)[0]] # 将数字序列转换为明文 plain_text = ''.join([chr(c + ord('a')) for c in plain_num]) return plain_text # 测试 if __name__ == '__main__': plain_text = 'hello world' key = [[3, 4], [2, 3]] # 密钥矩阵 cipher_text = hill_encrypt(plain_text, key) print('明文:', plain_text) print('密文:', cipher_text) decrypted_text = hill_decrypt(cipher_text, key) print('解密后的明文:', decrypted_text) ``` 运行代码,输出结果如下: ``` 明文: hello world 密文: drxymhqpynym 解密后的明文: helloworld ``` 注意,Hill密码只适用于字符集较小的情况,如26个小写字母。对于更大的字符集,需要使用更复杂的加密算法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值