AES cipher
AES is a symmetric encryption algorithm that is widely used for securing data. It operates on fixed-size blocks of data and uses a secret key for both encryption and decryption. AES is known for its speed and security, making it a popular choice for various applications.
PKCS7AESCipher
¶
Bases: CipherProtocol
Source code in topsecret/infra/cipher/aes.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
|
decrypt(ciphertext, metadata, passphrase=None)
¶
Decrypts the provided ciphertext using the method specified in metadata.
This method supports two modes of decryption, corresponding to the encryption
methods:
1. Passphrase-based: If metadata["method"]
is "passphrase", this
method expects a passphrase
to be provided. It extracts the salt
from the beginning of the ciphertext
, derives the decryption key
using the passphrase and salt (via _get_key
), and then decrypts
the remaining part of the ciphertext
.
2. Auto-generated key: If metadata["method"]
is "auto" (or if no
method is specified, "auto" is assumed), this method expects the
decryption key to be prepended to the ciphertext
, separated by
AUTO_KEY_SEP
. It splits the ciphertext
to retrieve the key and
the actual encrypted data, then uses this key for decryption.
The Fernet symmetric decryption algorithm is used for the actual decryption.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ciphertext
|
Ciphertext
|
The encrypted data.
If passphrase-based, this is |
required |
metadata
|
Metadata
|
A dictionary containing information about the
encryption process, crucially the |
required |
passphrase
|
str | None
|
The secret phrase used for encryption if the method was "passphrase". Required for passphrase-based decryption. |
None
|
Returns:
Type | Description |
---|---|
str
|
The decrypted data as a UTF-8 string. |
Raises:
Type | Description |
---|---|
DecryptionError
|
If decryption fails due to various reasons, such as a missing passphrase for passphrase-based decryption, an invalid passphrase, an invalid key, corrupted ciphertext, or an unknown encryption method. |
Source code in topsecret/infra/cipher/aes.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
|
encrypt(data, passphrase=None)
¶
Encrypts the provided data using either a passphrase or an auto-generated key.
This method supports two modes of encryption:
-
Passphrase-based: If a
passphrase
is provided, a key is derived from it using PBKDF2HMAC. A salt is generated (or used if provided internally, though this implementation always generates a new one for passphrase encryption via_get_key
) and prepended to the ciphertext. The metadata will indicatemethod: "passphrase"
. -
Auto-generated key: If no
passphrase
is provided, a new Fernet key is generated. This key is then prepended to the ciphertext, separated by ab"|"
delimiter. The metadata will indicatemethod: "auto"
.
The Fernet symmetric encryption algorithm is used for the actual encryption of the data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
bytes
|
The bytes to be encrypted. |
required |
passphrase
|
str | None
|
An optional secret phrase. If provided, it's used to derive the encryption key. If None, a new key is generated and stored with the ciphertext. |
None
|
Returns:
Type | Description |
---|---|
tuple[Ciphertext, Metadata]
|
A tuple containing:
- Ciphertext: The encrypted data.
If passphrase-based, this is |
Source code in topsecret/infra/cipher/aes.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
|
get_hash(data)
¶
Generate a SHA-256 hash of the provided data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
bytes
|
The bytes to hash |
required |
Returns:
Type | Description |
---|---|
str
|
Hexadecimal string representation of the hash |
Source code in topsecret/infra/cipher/aes.py
170 171 172 173 174 175 176 177 178 179 180 |
|