dify/api/tests/unit_tests/libs/test_rsa.py
Bowen Liang b035c02f78
Some checks are pending
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/amd64, build-api-amd64) (push) Waiting to run
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/arm64, build-api-arm64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/amd64, build-web-amd64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/arm64, build-web-arm64) (push) Waiting to run
Build and Push API & Web / create-manifest (api, DIFY_API_IMAGE_NAME, merge-api-images) (push) Blocked by required conditions
Build and Push API & Web / create-manifest (web, DIFY_WEB_IMAGE_NAME, merge-web-images) (push) Blocked by required conditions
chore(api/tests): apply ruff reformat #7590 (#7591)
Co-authored-by: -LAN- <laipz8200@outlook.com>
2024-08-23 23:52:25 +08:00

30 lines
1.1 KiB
Python

import rsa as pyrsa
from Crypto.PublicKey import RSA
from libs import gmpy2_pkcs10aep_cipher
def test_gmpy2_pkcs10aep_cipher() -> None:
rsa_key_pair = pyrsa.newkeys(2048)
public_key = rsa_key_pair[0].save_pkcs1()
private_key = rsa_key_pair[1].save_pkcs1()
public_rsa_key = RSA.import_key(public_key)
public_cipher_rsa2 = gmpy2_pkcs10aep_cipher.new(public_rsa_key)
private_rsa_key = RSA.import_key(private_key)
private_cipher_rsa = gmpy2_pkcs10aep_cipher.new(private_rsa_key)
raw_text = "raw_text"
raw_text_bytes = raw_text.encode()
# RSA encryption by public key and decryption by private key
encrypted_by_pub_key = public_cipher_rsa2.encrypt(message=raw_text_bytes)
decrypted_by_pub_key = private_cipher_rsa.decrypt(encrypted_by_pub_key)
assert decrypted_by_pub_key == raw_text_bytes
# RSA encryption and decryption by private key
encrypted_by_private_key = private_cipher_rsa.encrypt(message=raw_text_bytes)
decrypted_by_private_key = private_cipher_rsa.decrypt(encrypted_by_private_key)
assert decrypted_by_private_key == raw_text_bytes