Changeset 58
- Timestamp:
- 08/13/13 00:28:04 (11 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/INSTALL
r41 r58 1 1 Bletchley Installation Guide 2 2 ============================ 3 4 Bletchley is developed under Debian, but is likely to work on most 5 modern Linux distributions, *BSD and MacOS. Windows is not yet 6 supported, but may be in the future. 7 3 8 4 9 Dependencies … … 7 12 * scons (available as a package in most Linux distributions) 8 13 * gcc 9 * Recommended: python "requests" library (python3-requests under Debian)14 * Optional: python "requests" library (python3-requests under Debian) 10 15 11 16 -
trunk/lib/bletchley/CBC/__init__.py
r52 r58 197 197 198 198 199 def decrypt_next_byte(self, prior, block, known_bytes ):199 def decrypt_next_byte(self, prior, block, known_bytes, cache=True): 200 200 """Decrypts one byte of ciphertext by modifying the prior 201 201 ciphertext block at the same relative offset. … … 257 257 258 258 decrypted = struct.pack("B",self._thread_result^base^(numKnownBytes+1)) 259 self.decrypted = decrypted + self.decrypted 259 if cache: 260 self.decrypted = decrypted + self.decrypted 260 261 # Return previous bytes together with current byte 261 262 return decrypted+known_bytes 262 263 263 264 264 def decrypt_block(self, prior, block, last_bytes=b'' ):265 def decrypt_block(self, prior, block, last_bytes=b'', cache=True): 265 266 """Decrypts the block of ciphertext provided as a parameter. 266 267 … … 268 269 269 270 while(len(last_bytes)!=self.block_size): 270 last_bytes = self.decrypt_next_byte(prior, block, last_bytes )271 last_bytes = self.decrypt_next_byte(prior, block, last_bytes, cache) 271 272 272 273 self.log_message("Decrypted block: %s" % repr(last_bytes)) … … 336 337 raise InvalidBlockError(self.block_size,len(plaintext)) 337 338 338 ptext = self.decrypt_block(b'\x00'*self.block_size, ciphertext )339 ptext = self.decrypt_block(b'\x00'*self.block_size, ciphertext, cache=False) 339 340 prior = buffertools.xorBuffers(ptext, plaintext) 341 self.log_message("Encrypted block: %s to %s with prior %s" % (repr(plaintext), repr(ciphertext), repr(prior))) 340 342 return prior,ciphertext 341 343 342 344 343 def encrypt(self,plaintext ):345 def encrypt(self,plaintext, ciphertext=None): 344 346 """Encrypts a plaintext value through "CBC-R" style prior-block 345 347 propagation. … … 355 357 356 358 """ 357 359 358 360 blocks = buffertools.splitBuffer(buffertools.pkcs7PadBuffer(plaintext, self.block_size), 359 361 self.block_size) 360 361 if (len(self.decrypted) >= self.block_size 362 if ciphertext != None: 363 if len(ciphertext) % self.block_size != 0: 364 raise InvalidBlockError(self.block_size,len(ciphertext)) 365 num_cblocks = (len(ciphertext) // self.block_size) - 1 366 del blocks[0-num_cblocks:] # we've already encrypted these 367 prior = ciphertext[0:self.block_size] 368 369 elif (len(self.decrypted) >= self.block_size 362 370 and len(self._ciphertext) >= 2*self.block_size): 363 371 # If possible, reuse work from prior decryption efforts on original … … 368 376 buffertools.xorBuffers(final_plaintext, blocks[-1])) 369 377 ciphertext = self._ciphertext[0-self.block_size:] 378 del blocks[-1] 370 379 else: 371 380 # Otherwise, select a random last block and generate the prior block 372 ciphertext= struct.pack("B"*self.block_size,381 prior = struct.pack("B"*self.block_size, 373 382 *[random.getrandbits(8) for i in range(self.block_size)]) 374 prior,ciphertext = self.encrypt_block(blocks[-1], ciphertext) 375 376 # Continue generating all prior blocks 377 for i in range(len(blocks)-2, -1, -1): 378 prior,cblock = self.encrypt_block(blocks[i],prior) 379 ciphertext = cblock+ciphertext 380 383 ciphertext = b'' 384 385 try: 386 # Continue generating all prior blocks 387 for i in range(len(blocks)-1, -1, -1): 388 prior,cblock = self.encrypt_block(blocks[i],prior) 389 ciphertext = cblock+ciphertext 390 except Exception as e: 391 self.log_message("Encryption failure. prior+ciphertext: %s" % repr(prior+ciphertext)) 392 381 393 # prior as IV 382 394 return prior,ciphertext
Note: See TracChangeset
for help on using the changeset viewer.