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: