From b46e772d0961e2a94ea2a5f429caeb3d697d7764 Mon Sep 17 00:00:00 2001 From: Bai Date: Tue, 30 Jun 2020 18:35:01 +0800 Subject: [PATCH] =?UTF-8?q?feat(login=20password=20ecrypt):=20=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E5=AF=86=E7=A0=81=E5=8A=A0=E5=AF=86=E4=BC=A0=E8=BE=93?= =?UTF-8?q?=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/authentication/tests.py | 14 ++++++++++++++ apps/authentication/utils.py | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/apps/authentication/tests.py b/apps/authentication/tests.py index 8b1378917..890a1fb4f 100644 --- a/apps/authentication/tests.py +++ b/apps/authentication/tests.py @@ -1 +1,15 @@ +from .utils import gen_key_pair, rsa_decrypt, rsa_encrypt + + +def test_rsa_encrypt_decrypt(message='test-password-$%^&*'): + """ 测试加密/解密 """ + print('Need to encrypt message: {}'.format(message)) + rsa_private_key, rsa_public_key = gen_key_pair() + print('RSA public key: \n{}'.format(rsa_public_key)) + print('RSA private key: \n{}'.format(rsa_private_key)) + message_encrypted = rsa_encrypt(message, rsa_public_key) + print('Encrypted message: {}'.format(message_encrypted)) + message_decrypted = rsa_decrypt(message_encrypted, rsa_private_key) + print('Decrypted message: {}'.format(message_decrypted)) + diff --git a/apps/authentication/utils.py b/apps/authentication/utils.py index 9d1c45d98..359778cb6 100644 --- a/apps/authentication/utils.py +++ b/apps/authentication/utils.py @@ -24,6 +24,14 @@ def gen_key_pair(): return rsa_private_key, rsa_public_key +def rsa_encrypt(message, rsa_public_key): + """ 加密登录密码 """ + key = RSA.importKey(rsa_public_key) + cipher = PKCS1_v1_5.new(key) + cipher_text = base64.b64encode(cipher.encrypt(message.encode())).decode() + return cipher_text + + def rsa_decrypt(cipher_text, rsa_private_key=None): """ 解密登录密码 """ if rsa_private_key is None: