需求:
1 :员工在填写用户的手机号时,直接填写,在django后台中输入
2:当员工在后台确认要存储到数据库时,后台将会把手机号进行加密存储,当数据库被黑之后,手机号字段为加密字符
3:员工在后台中进行查看客户手机号时,需要正常显示,非加密状态
4:直接把公钥和私钥存放到代码里面,不要额外读取存储公钥私钥
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:返回cif len(self.shoujihao)>=20:shoujihao1=self.shoujihaoshoujihao1 = base64.b64decode(shoujihao1)# 读取私钥文件private_key="""-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAp4vQEgBPcCy09xmJDVFxjuYk+USqBNgUxIHF4+E9Vot1rDWU
ncFSIERsEqTq9wIlVsgiVtRJ/0FxHLIgZPlFvRRUh0JjxkpddYAK+EKK2gySoiE8
kUwomeYpILg/pAxtSoRBbUqtE/sTQZM2sOM07FYrz/Q8oVBN1Au3/LwOOAFf6R/5
lFBJCutS/wAuvydwxlTjuNp4PF4Tc65qaF+35VE0IvcOByahAPRqI4TqFb/UOidJ
1Xc4HPIwmtkeYYRyqDgZun2tdQ0iQm7A7lN9TkP6qoKbTRI9ySoBHs4SJXy8r63r
xPRgkjEsVR/hcsZAt7LHjNnZKWd8b1yi0AO56QIDAQABAoIBABya/xUXtvheta3r
ju32bgY5Dm59le6aSt9rDbOUuPF+AYL7qepOP5yROgdr3075b5ETveMnvxOE45BD
7Ep5EbpQzobCY8EO4MqHKDhPOiA03dMvDAV9pH/0Hyj+NCVo1r8OehI0Gf1LlHPR
c9i7DgXcx4GrgC+Fg6g0sP7GR8IxWKkT1rHNWWjqaWh3HlBbJhdrZpxjKbCqTjKM
ImrgPKDi1An0NNHJyQe42XNgmOAMoCRUvP8skLveyYDQ2+LCeTjnmOD0WTMlwlt3
ulu4MjKh/wMAj7dxguonK97C01n0utP+k0KdkOP+rBNazEF2roY2FrcYr+n1MgTs
QDECG1JVtYVP1Tuyb3pgoQKBgC9SV/Ft1K4T8e/AiTrF07SCn8BEZOZ0BDI69+lB
cYh1CPXBt1M0a+T+M1mwCVWxYrapfVismesnIiVQO+n2AehdDyvE1krkHRTQCUYD
PkM7efPek9WBWHy6dbEPJ+mMDs1P5mVOV7MJEGQMtE01BO/KoQ74fePrbaFDwHkU
nzuBAoGBAJIpUUmDMnkMzr5HPw1rtZEjkR0k+EQXU3jeaKA1TjqRoli1ApUpBB11
ekDfPc/BqzRSIkACEijwdnf7NhQveCAiE+aj5NiGkwS/zjX9S96v0qK5SFil6y+c
EXv1GMN54aCmiHWBGq86tOKjV9M4hnlVpuRJPeHi52nAyHpJfmB7
-----END RSA PRIVATE KEY-----"""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_dataprint("类型")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.shoujihaoif 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)# -------------------------------------------------------------------------------------