You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PyCryptodome解密二维码时出现错误

Troubleshooting RSA Decryption Failure in decrypt_qr.py

Hey there! It’s super common for code that works standalone to hit snags when integrated into another module—let’s break down the most likely reasons your RSA decryption is failing when used in decrypt_qr.py, even though rsa_module.py runs fine on its own.

Common Causes & Fixes

1. Mismatched Data Formats

When you test rsa_module.py directly, you’re probably passing raw encrypted bytes to your decrypt function. But QR code content is often returned as a string (maybe Base64-encoded) or a truncated byte stream, which your decrypt function doesn’t expect.

Fix:
Check the format of the data you’re pulling from the QR code, then convert it to match what your decrypt function expects:

  • If the QR content is Base64:
    import base64
    # Convert QR string back to bytes
    encrypted_bytes = base64.b64decode(qr_scanned_content)
    # Now pass to decrypt function
    decrypted_text = rsa_module.decrypt(encrypted_bytes, private_key)
    
  • If it’s a UTF-8 string of encrypted bytes:
    encrypted_bytes = qr_scanned_content.encode('utf-8')
    

2. Broken Key Loading

In rsa_module.py, you might be loading your private key from a file path that works when running that module directly—but when decrypt_qr.py runs, it’s using a different working directory, so the key path is invalid.

Fix:
Use absolute paths to load your key file instead of relative paths. Here’s how with pathlib:

from pathlib import Path

# Get the directory of the current script (decrypt_qr.py)
script_dir = Path(__file__).parent
# Assume your private key is in the same directory as the script
private_key_path = script_dir / "private_key.pem"

with open(private_key_path, 'rb') as f:
    private_key = rsa.PrivateKey.load_pkcs1(f.read())

3. Missing Error Handling

When testing standalone, you might not have hit edge cases (like truncated data or invalid encoding) that pop up with real QR codes. Without proper error catching, you’re missing key details about what’s breaking.

Fix:
Wrap your decrypt call in a try/except block to log detailed errors:

try:
    decrypted_text = rsa_module.decrypt(qr_content, private_key)
except Exception as e:
    print(f"Decryption error: {type(e).__name__} - {str(e)}")
    # Log useful context to debug
    print(f"Input data type: {type(qr_content)}, length: {len(qr_content)}")

4. Module Import Conflicts

It’s possible that decrypt_qr.py is importing a different decrypt function than the one you wrote in rsa_module.py (e.g., from another library or a duplicate module with the same name).

Fix:
Double-check your import statement to ensure you’re pulling the right function:

# Use explicit absolute import
from your_package_name.rsa_module import decrypt

Also, make sure your package is installed in your environment (or use a virtual environment) to avoid namespace clashes.

5. Truncated QR Content

QR codes have size limits—if your encrypted data is too long, it might get truncated when generating the QR code, leading to incomplete data being passed to your decrypt function.

Fix:

  • Verify the length of your encrypted data matches what’s stored in the QR code.
  • If the data is too large, split it into chunks before generating QR codes, then reassemble the chunks in decrypt_qr.py before decrypting.

内容的提问来源于stack exchange,提问作者Moody Cody

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:51:02