mirror of
https://github.com/langgenius/dify.git
synced 2026-03-17 04:47:50 +08:00
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
29 lines
1.1 KiB
Python
29 lines
1.1 KiB
Python
from Crypto.PublicKey import RSA
|
|
|
|
from libs import gmpy2_pkcs10aep_cipher
|
|
|
|
|
|
def test_gmpy2_pkcs10aep_cipher():
|
|
rsa_key = RSA.generate(2048)
|
|
public_key = rsa_key.publickey().export_key(format="PEM")
|
|
private_key = rsa_key.export_key(format="PEM")
|
|
|
|
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
|