Python implementation of SQL Server's ENCRYPTBYPASSPHRASE and DECRYPTBYPASSPHRASE functions.
pip install pysqlcryptfrom pysqlcrypt import encrypt_by_passphrase, decrypt_by_passphrase, SQLCryptVersion
# Encrypt
ciphertext = encrypt_by_passphrase("passphrase", "plaintext", SQLCryptVersion.V2)
# Decrypt with auto-detection (recommended)
plaintext = decrypt_by_passphrase("passphrase", ciphertext, encoding="auto")Returns encrypted bytes. Use SQLCryptVersion.V1 for SQL Server 2008-2016, SQLCryptVersion.V2 for 2017+.
Returns decrypted bytes, or str if encoding is specified. Accepts bytes or hex string (with or without 0x prefix).
| Value | Behavior |
|---|---|
None |
Returns raw bytes |
"auto" |
Auto-detects UTF-16LE (NVARCHAR) vs UTF-8 |
"utf-8" |
Decodes as UTF-8 (VARCHAR) |
"utf-16-le" |
Decodes as UTF-16LE (NVARCHAR) |
| Version | SQL Server | Algorithm |
|---|---|---|
| V1 | 2008-2016 | 3DES-CBC, SHA1 |
| V2 | 2017+ | AES-256-CBC, SHA256 |
- Use
encoding="auto"for mixed VARCHAR/NVARCHAR data - The
authenticatorparameter embeds additional context data for verification - Decryption auto-detects the version from the ciphertext header
MIT
🍌