Spaces:
Sleeping
Sleeping
Improve RSA block size detection for variable base64 lengths
Browse files- encryption.py +43 -20
encryption.py
CHANGED
|
@@ -120,41 +120,64 @@ def decrypt_data(encrypted_base64: str) -> Optional[Any]:
|
|
| 120 |
return {"encrypted_data": encrypted_base64, "decryption_error": str(e)}
|
| 121 |
|
| 122 |
|
| 123 |
-
def decrypt_multiple_blocks(encrypted_data: str
|
| 124 |
"""
|
| 125 |
Decrypt multiple concatenated encrypted blocks.
|
| 126 |
|
| 127 |
-
RSA 2048-bit encrypted data is
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
Args:
|
| 130 |
encrypted_data: Concatenated base64-encoded encrypted blocks
|
| 131 |
-
block_size: Size of each encrypted block in base64 chars (default 344 for RSA-2048)
|
| 132 |
|
| 133 |
Returns:
|
| 134 |
List of decrypted data objects
|
| 135 |
"""
|
| 136 |
results = []
|
| 137 |
|
| 138 |
-
#
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
|
|
|
| 146 |
return results
|
| 147 |
|
| 148 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
for i in range(0, len(encrypted_data), block_size):
|
| 150 |
block = encrypted_data[i:i + block_size]
|
| 151 |
-
if block:
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
results.append(result)
|
| 156 |
-
except Exception as e:
|
| 157 |
-
logger.error(f"Failed to decrypt block {i // block_size}: {e}")
|
| 158 |
-
continue
|
| 159 |
|
| 160 |
return results
|
|
|
|
| 120 |
return {"encrypted_data": encrypted_base64, "decryption_error": str(e)}
|
| 121 |
|
| 122 |
|
| 123 |
+
def decrypt_multiple_blocks(encrypted_data: str) -> list[Any]:
|
| 124 |
"""
|
| 125 |
Decrypt multiple concatenated encrypted blocks.
|
| 126 |
|
| 127 |
+
RSA 2048-bit encrypted data is 256 bytes, which can be:
|
| 128 |
+
- 344 chars in base64 with padding
|
| 129 |
+
- 342 chars in base64 without padding (URL-safe)
|
| 130 |
+
|
| 131 |
+
This function tries multiple block sizes to find the right one.
|
| 132 |
|
| 133 |
Args:
|
| 134 |
encrypted_data: Concatenated base64-encoded encrypted blocks
|
|
|
|
| 135 |
|
| 136 |
Returns:
|
| 137 |
List of decrypted data objects
|
| 138 |
"""
|
| 139 |
results = []
|
| 140 |
|
| 141 |
+
# Common block sizes for RSA-2048 in base64
|
| 142 |
+
# 344 = with padding, 342 = without padding
|
| 143 |
+
POSSIBLE_BLOCK_SIZES = [344, 342, 343, 256]
|
| 144 |
+
|
| 145 |
+
# First, try to decrypt as a single block
|
| 146 |
+
if len(encrypted_data) <= 350:
|
| 147 |
+
result = decrypt_data(encrypted_data)
|
| 148 |
+
if result:
|
| 149 |
+
results.append(result)
|
| 150 |
return results
|
| 151 |
|
| 152 |
+
# Try each possible block size
|
| 153 |
+
for block_size in POSSIBLE_BLOCK_SIZES:
|
| 154 |
+
# Check if data length is divisible by block size
|
| 155 |
+
if len(encrypted_data) % block_size == 0:
|
| 156 |
+
blocks_results = []
|
| 157 |
+
success = True
|
| 158 |
+
|
| 159 |
+
for i in range(0, len(encrypted_data), block_size):
|
| 160 |
+
block = encrypted_data[i:i + block_size]
|
| 161 |
+
result = decrypt_data(block)
|
| 162 |
+
if result and "decryption_error" not in result:
|
| 163 |
+
blocks_results.append(result)
|
| 164 |
+
else:
|
| 165 |
+
success = False
|
| 166 |
+
break
|
| 167 |
+
|
| 168 |
+
if success and blocks_results:
|
| 169 |
+
logger.info(f"Successfully decrypted {len(blocks_results)} blocks with block_size={block_size}")
|
| 170 |
+
return blocks_results
|
| 171 |
+
|
| 172 |
+
# Fallback: try to decrypt with default block size 344, collecting all results
|
| 173 |
+
block_size = 344
|
| 174 |
+
logger.warning(f"Falling back to block_size={block_size}, data length={len(encrypted_data)}")
|
| 175 |
+
|
| 176 |
for i in range(0, len(encrypted_data), block_size):
|
| 177 |
block = encrypted_data[i:i + block_size]
|
| 178 |
+
if block:
|
| 179 |
+
result = decrypt_data(block)
|
| 180 |
+
if result:
|
| 181 |
+
results.append(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
return results
|