jebin2 commited on
Commit
2bccb50
·
1 Parent(s): 651af0e

Improve RSA block size detection for variable base64 lengths

Browse files
Files changed (1) hide show
  1. 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, block_size: int = 344) -> list[Any]:
124
  """
125
  Decrypt multiple concatenated encrypted blocks.
126
 
127
- RSA 2048-bit encrypted data is typically 256 bytes, which is ~344 chars in base64.
 
 
 
 
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
- # If the data is smaller than block_size, treat as single block
139
- if len(encrypted_data) <= block_size:
140
- try:
141
- result = decrypt_data(encrypted_data)
142
- if result:
143
- results.append(result)
144
- except Exception as e:
145
- logger.error(f"Failed to decrypt single block: {e}")
 
146
  return results
147
 
148
- # Split into blocks and decrypt each
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  for i in range(0, len(encrypted_data), block_size):
150
  block = encrypted_data[i:i + block_size]
151
- if block: # Skip empty blocks
152
- try:
153
- result = decrypt_data(block)
154
- if result:
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