django后台进行加密手机号字段,加密存储,解密显示

发布时间:2024年01月17日

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

需求:
1 :员工在填写用户的手机号时,直接填写,在django后台中输入
2:当员工在后台确认要存储到数据库时,后台将会把手机号进行加密存储,当数据库被黑之后,手机号字段为加密字符
3:员工在后台中进行查看客户手机号时,需要正常显示,非加密状态
4:直接把公钥和私钥存放到代码里面,不要额外读取存储公钥私钥
5:为何有该需求,请参考《个人信息保护法》

1安装加密插件

pip install rsa

该命令将会安装(2024年1月17日)
1:插件名=pyasn1,版本=pyasn1-0.5.1
2:插件名=rsa,版本=rsa-4.9

2生成加密文件

请自行创建a.py文件,运行以下代码 python a.py

from Crypto.PublicKey import RSA  
from Crypto.Cipher import PKCS1_OAEP  
import binascii  
  
# 生成RSA密钥对  
key = RSA.generate(2048)  
public_key = key.publickey()  
  
# 将公钥和私钥保存到本地文件  
with open('public_key.pem', 'wb') as f:  
    f.write(public_key.export_key())  
with open('private_key.pem', 'wb') as f:  
    f.write(key.export_key())

这时已经在a.py的相同路径中创建了公钥和私钥的文件

3安装读取加密插件

pip install cryptography

4:在django项目中应用

models.py文件中代码如下

# -------------------------------------------------------------------------------------
# 手机号加密插件
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization, hashes
from cryptography.hazmat.primitives.asymmetric import padding
import base64

# -------------------------------------------------------------------------------------
class Order(models.Model):

    id = models.BigAutoField(primary_key=True, unique=True, verbose_name='订单id', )
    shoujihao = models.TextField(verbose_name='客户手机号码', max_length=30000, blank=True)
    # -------------------------------------------------------------------------------------
    # 手机号解密
    def get_shoujihao(self):
        # 1:获取到数据库中的字符串 a
        # 2:把字符串转换成字节类型 b
        # 3:对字节b进行解密为 c
        # 4:返回c
        if len(self.shoujihao)>=20:
            shoujihao1=self.shoujihao
            shoujihao1 = base64.b64decode(shoujihao1)

            # 读取私钥文件
            private_key="""-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAp4vQEgBPcCy09x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EY-----"""
            key = serialization.load_pem_private_key(private_key.encode(), password=None, backend=default_backend())
            decrypted_data = key.decrypt(shoujihao1, padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),
                                                                      algorithm=hashes.SHA256(), label=None))
            # decrypted_data 的类型为字节
            decrypted_data=decrypted_data.decode('utf-8')
            # decrypted_data.decode()之后的类型为字符串
            shoujihao1=decrypted_data
            print("类型")
            print(type(decrypted_data))
            print(decrypted_data)

            # 打印解密后的消息
            color_code = 'green'
            return format_html('<span style="color:{};">{}</span>', color_code, shoujihao1)
        else:
            color_code = 'green'
            return format_html('<span style="color:{};">-</span>', color_code, )

    get_shoujihao.short_description = '手机号'

admin.py文件中代码如下


# -------------------------------------------------------------------------------------
# 手机号加密插件
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization, hashes
from cryptography.hazmat.primitives.asymmetric import padding
import base64

# -------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------
class Order_salesmanAdmin(admin.ModelAdmin):
    # -------------------------------------------------------------------------------------
    # 手机号加密显示 和手机号显示
    # 设置录入后台中展示的数据字段
    list_display = ['id',
                    'shoujihao', 
                    'get_shoujihao',
                    ]

    def save_model(self, request, obj, form, change):
        if not obj.pk:
            obj.userid = UserProfile.objects.get(user=request.user)
            # 客户手机号加密存储
            shoujihao1 = obj.shoujihao
            if len(shoujihao1) == 11:
                # 1:在admin后台中,允许用户输入的是字符串类型的数据
                # 2:加密之后得到的是字节类型,
                # 3:把字节类型的编码为utf-8的字符串类型 存储在数据库中

                # 读取公钥文件
                public_key = """-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAp4vQEgBPcCy09xmJDVFx
juYk+USqBNgUxIHF4+E9Vot1rDWUncFSIERsEqTq9wIlVsgiVtRJ/0FxHLIgZPlF
TkP6qoKbTRI9ySoBHs4SJXy8r63rxPRgkjEsVR/hcsZAt7LHjNnZKWd8b1yi0AO5
6QIDAQAB
-----END PUBLIC KEY-----"""

                key = serialization.load_pem_public_key(public_key.encode(), backend=default_backend())
                encrypted_data = key.encrypt(shoujihao1.encode(),
                                                padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),
                                                            algorithm=hashes.SHA256(), label=None))
                # 加密后的数据类型 encrypted_data <class 'bytes'> 字节类型
                encrypted_data=base64.b64encode(encrypted_data).decode('utf-8')

                # decode为utf-8之后的数据类型  为字符串类型,这样才可以在字符字段中存储
                # print("decode之后的数据类型")
                # print(type(encrypted_data))
                # print(encrypted_data)

                obj.shoujihao =encrypted_data
            # -------------------------------------------------------------------------------------

        if not change:
            obj.userid = UserProfile.objects.get(user=request.user)
        else:
            obj.updated_userid = UserProfile.objects.get(user=request.user)
        obj.save()
        super().save_model(request, obj, form, change)

    # -------------------------------------------------------------------------------------
文章来源:https://blog.csdn.net/weixin_47021806/article/details/135657748
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。