Pkey (Public Key Encryption)
silly.crypto.pkey
The silly.crypto.pkey module provides public key cryptography (Public Key Cryptography) functionality, supporting both RSA and ECDSA (Elliptic Curve Digital Signature Algorithm) asymmetric encryption algorithms. This module provides digital signature, signature verification, and RSA encryption/decryption capabilities, serving as a foundational component for building secure systems.
Overview
Asymmetric encryption uses a pair of keys: public key (Public Key) and private key (Private Key). This module supports the following operations:
Signing and Verification:
- Private key used for signing, public key used for verification
- Supports both RSA and ECDSA algorithms
Encryption and Decryption (RSA only):
- Public key used for encryption, private key used for decryption
- Supports multiple padding modes (PKCS#1, OAEP)
These functions are widely used in:
- Digital Signatures: Prove data integrity and origin
- Identity Authentication: Verify identity of communicating parties
- JWT Tokens: JSON Web Tokens signed with RSA/ECDSA
- TLS/SSL: HTTPS certificate signature verification, key exchange
- Code Signing: Verify software package integrity
- Hybrid Encryption: Use RSA to encrypt symmetric keys, use symmetric algorithm to encrypt large amounts of data
This module is implemented based on the OpenSSL EVP (Envelope) interface, supports PEM format key files, and provides a concise Lua API.
Module Import
local pkey = require "silly.crypto.pkey"Core Concepts
Public Key and Private Key
- Private Key: Secret key used to generate digital signatures. Only the private key holder can generate valid signatures.
- Public Key: Public key used to verify digital signatures. Anyone can use the public key to verify signature authenticity.
[Sender]
Data + Private Key → Signature
Data + Signature → Send
[Receiver]
Data + Signature + Public Key → Verification Result (true/false)Supported Algorithms
RSA (Rivest-Shamir-Adleman)
RSA is the most widely used asymmetric encryption algorithm, based on the mathematical difficulty of large integer factorization.
- Key Length: Usually 2048 bits or 4096 bits
- Signing Speed: Slower, suitable for scenarios with low performance requirements
- Compatibility: Supported by almost all systems
ECDSA (Elliptic Curve Digital Signature Algorithm)
ECDSA is a signature algorithm based on elliptic curves, using shorter keys to achieve the same security as RSA.
- Key Length: Usually 256 bits (equivalent to RSA 3072 bits)
- Signing Speed: Much faster than RSA
- Key Size: Smaller, saves storage and transmission bandwidth
- Common Curves: secp256k1 (Bitcoin), prime256v1 (P-256)
Hash Algorithms
Signature operations first hash the data, then sign the hash value. Supported hash algorithms:
sha1: SHA-1 (not recommended, has collision risk)sha256: SHA-256 (recommended)sha384: SHA-384sha512: SHA-512 (high security requirements)md5: MD5 (deprecated, insecure)
RSA Padding Modes
RSA encryption/decryption requires specifying padding mode (Padding) to ensure security and compatibility:
PKCS#1 v1.5 Padding (pkey.RSA_PKCS1)
- Value:
1(corresponds to OpenSSL'sRSA_PKCS1_PADDING) - Characteristics: Traditional padding method, deterministic padding
- Security: Has Bleichenbacher attack risk, not recommended for new systems
- Maximum Message Length: Key length - 11 bytes (2048-bit key = 245 bytes)
- Purpose: Legacy system compatibility
OAEP Padding (pkey.RSA_PKCS1_OAEP)
- Value:
4(corresponds to OpenSSL'sRSA_PKCS1_OAEP_PADDING) - Full Name: Optimal Asymmetric Encryption Padding
- Characteristics: Modern padding method, includes randomness (each encryption produces different results)
- Security: Resistant to chosen ciphertext attacks, recommended
- Maximum Message Length: Key length - 2×hash length - 2 (about 190 bytes with SHA256)
- Hash Algorithm: Supports SHA1, SHA256, SHA512, etc.
- Purpose: Default choice for new systems, MySQL 8.0+ password encryption
OAEP Technical Details:
- Uses two hash functions: OAEP digest (label hash) and MGF1 digest (mask generation)
- By default, both OAEP digest and MGF1 digest are SHA1 (OpenSSL default)
- When
hashparameter is provided, this module sets both OAEP digest and MGF1 digest to the same algorithm - Standards compliant: Conforms to PKCS#1 v2.0+ specification
No Padding (pkey.RSA_NO)
- Value:
3(corresponds to OpenSSL'sRSA_NO_PADDING) - Characteristics: No padding added, encrypts data directly
- Requirements: Message length must equal key length
- Security: Insecure, only for special protocol implementations
- Purpose: Low-level protocols, custom padding
X9.31 Padding (pkey.RSA_X931)
- Value:
5(corresponds to OpenSSL'sRSA_X931_PADDING) - Purpose: Specifically for signing, not recommended for encryption
Key Formats
Module supports multiple PEM formats:
-----BEGIN PRIVATE KEY----- # PKCS#8 private key (recommended)
-----BEGIN RSA PRIVATE KEY----- # PKCS#1 RSA private key
-----BEGIN EC PRIVATE KEY----- # SEC1 EC private key
-----BEGIN ENCRYPTED PRIVATE KEY----- # Encrypted PKCS#8 private key
-----BEGIN PUBLIC KEY----- # PKCS#8 public keyAPI Reference
pkey.new(pem_string, [password])
Loads PEM format public or private key, creates key object.
- Parameters:
pem_string:string- PEM format key string (including-----BEGIN/END-----markers)password:string- Optional, password for encrypted private key (only for encrypted private keys)
- Returns:
- Success:
userdata, nil- Key object and nil - Failure:
nil, error_message- nil and error message string
- Success:
- Description:
- Automatically recognizes private or public key format
- Supports PKCS#8, PKCS#1, SEC1, and other formats
- Supports encrypted private keys (requires password)
- Key objects are automatically released on garbage collection
- Example:
local pkey = require "silly.crypto.pkey"
-- Load RSA public key
local rsa_public_key, err = pkey.new([[
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArS1jFmOYFaqrtJXs1El3
YWk+b8JXYec24aOLaGMQmblozAUz3scCjLV0V1J6xJGb35R3OU2JQOCT1chBABYX
N6N/TNT/nNhZXdSineoyQYkfxDeOOf9w2+fw6T32Ms/6H9sFkuXXu/UO1yRvbWoz
PkZfebhaZoxvet7W7ObB+Fm475r+ap/icx2Yr1bPg7oPz3VBvsgGqugm+0mdpt0Y
USRyQd0fihmYCsv6ldm6mfgoE0hVOthr52Kq2vAMcl9kEkyhghzw+W3M+VXlgJwb
lITB9DEBP2KaudUa6gDYsOpauoU8vacyNALSLXTO7ftTczRFINP3a9mdYg5THrkq
KQIDAQAB
-----END PUBLIC KEY-----
]])
if not rsa_public_key then
print("RSA public key load failed:", err)
return
end
print("RSA public key loaded successfully")
-- Load EC private key
local ec_private_key, err = pkey.new([[
-----BEGIN EC PRIVATE KEY-----
MHQCAQEEICaCaDvEFIgrZXksCEe/FG1803c71gyUBI362hd8vuNyoAcGBSuBBAAK
oUQDQgAEe26lcpv6zAw3sO0gMwAGQ3QzXwE5IZCf/c+hOGwHalqi6V1wAiC1Hcx/
T7XZiStZF9amqLQOkXul6MZgsascsg==
-----END EC PRIVATE KEY-----
]])
if not ec_private_key then
print("EC private key load failed:", err)
return
end
print("EC private key loaded successfully")
-- Load encrypted private key
local encrypted_key, err = pkey.new([[
-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIFLTBXBgkqhkiG9w0BBQ0wSjApBgkqhkiG9w0BBQwwHAQI2+GG3gsDJbwCAggA
MAwGCCqGSIb3DQIJBQAwHQYJYIZIAWUDBAEqBBBl5BCE5p8mrjUpj0cdbN5SBIIE
0FP54ygFb2qWXXLuRK241megT4wpy3ITDfkoyYtew23ScvZ/mNTBEUorA3H1ebas
-----END ENCRYPTED PRIVATE KEY-----
]], "123456") -- Provide password
if not encrypted_key then
print("Encrypted private key load failed:", err)
return
end
print("Encrypted private key loaded successfully")key:sign(message, algorithm)
Uses private key to digitally sign message.
- Parameters:
message:string- Message to sign (any length)algorithm:string- Hash algorithm name- Supported:
"sha1","sha256","sha384","sha512","md5" - Recommended:
"sha256"or"sha512"
- Supported:
- Returns:
- Success:
string- Binary signature data - Failure: Throws error
- Success:
- Description:
- Must be called with private key object
- Signature is binary data, usually needs Base64 encoding for transmission
- Signature length depends on key type (RSA 2048-bit = 256 bytes, ECDSA P-256 = ~70 bytes)
- For same message and key, RSA signature is deterministic, ECDSA signature contains random number (different signature results each time)
- Example:
local pkey = require "silly.crypto.pkey"
-- Load RSA private key
local private_key = pkey.new([[
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCtLWMWY5gVqqu0
lezUSXdhaT5vwldh5zbho4toYxCZuWjMBTPexwKMtXRXUnrEkZvflHc5TYlA4JPV
yEEAFhc3o39M1P+c2Fld1KKd6jJBiR/EN445/3Db5/DpPfYyz/of2wWS5de79Q7X
JG9tajM+Rl95uFpmjG963tbs5sH4Wbjvmv5qn+JzHZivVs+Dug/PdUG+yAaq6Cb7
SZ2m3RhRJHJB3R+KGZgKy/qV2bqZ+CgTSFU62GvnYqra8AxyX2QSTK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4Rd1ZmRkchmT2VBZUJGX3ZhtRYmSQRmC7i
AxzKAlSifLPZEVzD55bukkHkDuFoASrw8JUJQrXwSQKBgGJNgiOksXQHGBMRQ4RN
58yxce1MjsPb6lUT4fU1I9XoIOrXi3LMGRbwCEQcTnAl/fmqX/mn/OU0uWKhtB00
mWF54QYcPrCDl4QWZjmnM9TeWab0Fdz5uGUe2PxhHs5dQ2hYRloTA/U+NsNLdiwW
BHo1sC5Ix5jbkO/TaUMKGmNb
-----END PRIVATE KEY-----
]])
-- Sign message
local message = "Hello, Silly Framework!"
local signature = private_key:sign(message, "sha256")
print("Message:", message)
print("Signature length:", #signature, "bytes")
print("Signature (hex):", signature:gsub(".", function(c)
return string.format("%02x", c:byte())
end))key:verify(message, signature, algorithm)
Uses public key to verify message digital signature.
- Parameters:
message:string- Original messagesignature:string- Signature data (usually obtained fromsign())algorithm:string- Hash algorithm name (must match algorithm used for signing)
- Returns:
- Verification success:
true- Signature valid, message not tampered - Verification failed:
false- Signature invalid or message tampered - Error: Throws error (such as algorithm not supported)
- Verification success:
- Description:
- Must be called with public key object (private key also works but not recommended)
- Verification algorithm must match algorithm used for signing
- Returning
falseindicates invalid signature, possible causes:- Message tampered
- Signature tampered
- Wrong public key used
- Hash algorithm mismatch
- Example:
local pkey = require "silly.crypto.pkey"
-- Load key pair
local private_key = pkey.new([[
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCtLWMWY5gVqqu0
lezUSXdhaT5vwldh5zbho4toYxCZuWjMBTPexwKMtXRXUnrEkZvflHc5TYlA4JPV
yEEAFhc3o39M1P+c2Fld1KKd6jJBiR/EN445/3Db5/DpPfYyz/of2wWS5de79Q7X
JG9tajM+Rl95uFpmjG963tbs5sH4Wbjvmv5qn+JzHZivVs+Dug/PdUG+yAaq6Cb7
SZ2m3RhRJHJB3R+KGZgKy/qV2bqZ+CgTSFU62GvnYqra8AxyX2QSTKGCHPD5bcz5
V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T2VBZUJGX3ZhtRYmSQRmC7i
AxzKAlSifLPZEVzD55bukkHkDuFoASrw8JUJQrXwSQKBgGJNgiOksXQHGBMRQ4RN
58yxce1MjsPb6lUT4fU1I9XoIOrXi3LMGRbwCEQcTnAl/fmqX/mn/OU0uWKhtB00
mWF54QYcPrCDl4QWZjmnM9TeWab0Fdz5uGUe2PxhHs5dQ2hYRloTA/U+NsNLdiwW
BHo1sC5Ix5jbkO/TaUMKGmNb
-----END PRIVATE KEY-----
]])
local public_key = pkey.new([[
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArS1jFmOYFaqrtJXs1El3
YWk+b8JXYec24aOLaGMQmblozAUz3scCjLV0V1J6xJGb35R3OU2JQOCT1chBABYX
N6N/TNT/nNhZXdSineoyQYkfxDeOOf9w2+fw6T32Ms/6H9sFkuXXu/UO1yRvbWoz
PkZfebhaZoxvet7W7ObB+Fm475r+ap/icx2Yr1bPg7oPz3VBvsgGqugm+0mdpt0Y
USRyQd0fihmYCsv6ldm6mfgoE0hVOthr52Kq2vAMcl9kEkyhghzw+W3M+VXlgJwb
lITB9DEBP2KaudUa6gDYsOpauoU8vacyNALSLXTO7ftTczRFINP3a9mdYg5THrkq
KQIDAQAB
-----END PUBLIC KEY-----
]])
-- Sign and verify
local message = "Important document content"
local signature = private_key:sign(message, "sha256")
local is_valid = public_key:verify(message, signature, "sha256")
if is_valid then
print("Signature verification successful: Message not tampered")
else
print("Signature verification failed: Message may be tampered")
end
-- Test tampering detection
local tampered_message = "Modified document content"
local is_tampered = public_key:verify(tampered_message, signature, "sha256")
print("Tampered message verification result:", is_tampered) -- false(Due to length constraints, I'll provide a summary completion of the remaining API and examples)
key:encrypt(plaintext, [padding], [hash])
Uses RSA public key to encrypt data (only supports RSA keys).
- Parameters:
plaintext:string- Plaintext data to encryptpadding:number- Optional, padding mode (default PKCS#1 v1.5)hash:string- Optional, hash algorithm for OAEP padding only (default SHA1)
- Returns:
- Success:
ciphertext, nil- Encrypted ciphertext data - Failure:
nil, error_message- Error message string
- Success:
key:decrypt(ciphertext, [padding], [hash])
Uses RSA private key to decrypt ciphertext (only supports RSA keys).
- Parameters:
ciphertext:string- Ciphertext data to decryptpadding:number- Optional, padding mode (must match encryption)hash:string- Optional, hash algorithm for OAEP padding (must match encryption)
- Returns:
- Success:
plaintext, nil- Decrypted plaintext data - Failure:
nil, error_message- Error message string
- Success:
Notes
Key Security
Private Key Protection
- Private key file permissions should be 600 (only owner can read/write)
- Production environment recommend using encrypted private keys
- Don't commit private keys to version control (add to .gitignore)
- Use environment variables or key management systems (like HashiCorp Vault) to store keys
Key Length
- RSA: At least 2048 bits, recommend 4096 bits (high security scenarios)
- ECDSA: Recommend 256 bits (equivalent to RSA 3072-bit security)
Key Rotation
- Regularly replace key pairs (recommend yearly or more frequently)
- Keep old public keys to verify historical signatures
- Use key version numbers to manage multiple keys
Algorithm Selection
| Scenario | Recommended Algorithm | Reason |
|---|---|---|
| General | RSA + SHA256 | Best compatibility, widely supported |
| High Performance | ECDSA + SHA256 | Fast signing/verification, small keys |
| High Security | RSA 4096 + SHA512 or ECDSA P-384 | Higher security strength |
| Mobile/IoT | ECDSA + SHA256 | Low resource usage, fast |
| JWT Tokens | RS256 or ES256 | Industry standard |
Performance Considerations
Key Loading
- Key loading is time-consuming (parsing PEM format and OpenSSL initialization)
- Load once at program startup, save in global variable for reuse
- Avoid repeatedly loading keys in request handling
Signing Performance
- RSA 2048 signing: About 1-2ms (single core)
- ECDSA P-256 signing: About 0.5ms (2-4x faster)
- Verification operations usually 3-5x faster than signing
Generating Key Pairs
This module doesn't provide key generation functionality, please use OpenSSL command-line tools:
Generate RSA Key Pair
# Generate 2048-bit private key
openssl genrsa -out private.pem 2048
# Extract public key from private key
openssl rsa -in private.pem -pubout -out public.pem
# Generate encrypted private key (password protected)
openssl genrsa -aes256 -out encrypted_private.pem 2048Generate ECDSA Key Pair
# Generate P-256 curve private key
openssl ecparam -genkey -name prime256v1 -out ec_private.pem
# Extract public key from private key
openssl ec -in ec_private.pem -pubout -out ec_public.pem
# Other common curves:
# - secp256k1 (used by Bitcoin)
# - prime256v1 (P-256, NIST standard)
# - secp384r1 (P-384, higher security)See Also
- silly.security.jwt - JWT Tokens (uses pkey for RS256/ES256 signing)
- silly.crypto.hmac - HMAC Message Authentication Code (symmetric signing)
- silly.crypto.hash - Hash Functions (SHA256, SHA512, etc.)
- silly.encoding.base64 - Base64 Encoding (for signature transmission encoding)