def decrypt(key, encoded): key = key.encode('ascii') padded_key = key.ljust(KEY_SIZE, b'\0') ciphertext = base64.b64decode(encoded.encode('ascii')) sg = pprp.data_source_gen(ciphertext, block_size=BLOCK_SIZE) dg = pprp.rjindael_decrypt_gen(padded_key, sg, block_size=BLOCK_SIZE) return pprp.decrypt_sink(dg).decode('utf-8')
def decryptRjindael(self, key, data): key_size = 16 block_size = 32 padded_key = key.ljust(key_size, b'\0') ciphertext = base64.b64decode(data) sg = pprp.data_source_gen(ciphertext, block_size=block_size) dg = pprp.rjindael_decrypt_gen(padded_key, sg, block_size=block_size) return pprp.decrypt_sink(dg).decode('utf-8')
def test_full(self): passphrase = 'password'.encode('ASCII') salt = 'salt'.encode('ASCII') key_size = 32 data = "this is a test" * 100 data_bytes = data.encode('ASCII') key = pprp.pbkdf2(passphrase, salt, key_size) # Create a source from available data. sg = pprp.data_source_gen(data_bytes) # Feed the source into the encryptor. eg = pprp.rjindael_encrypt_gen(key, sg) # Feed the encryptor into the decryptor. dg = pprp.rjindael_decrypt_gen(key, eg) # Sink the output into an IO-stream. decrypted = pprp.decrypt_sink(dg) self.assertEquals(data_bytes, decrypted)
logger.addHandler(ch) _configure_logging() def trans(text): return text.encode('ASCII') if sys.version_info[0] >= 3 else text passphrase = trans('password') salt = trans('salt') key_size = 32 data = ("this is a test" * 100).encode('ASCII') key = pprp.pbkdf2(passphrase, salt, key_size) # Create a source from available data. sg = pprp.data_source_gen(data) # Feed the source into the encryptor. eg = pprp.rjindael_encrypt_gen(key, sg) # Feed the encryptor into the decryptor. dg = pprp.rjindael_decrypt_gen(key, eg) # Sink the output into an IO-stream. decrypted = pprp.decrypt_sink(dg) assert data == decrypted
def source_gen(): for i in range(0, len(data), block_size): block = data[i:i + block_size] len_ = len(block) if len_ > 0: yield block.encode('ASCII') if len_ < block_size: break # Pump the encrypter output into the decrypter. encrypted_gen = pprp.rjindael_encrypt_gen(key, source_gen(), block_size) # Run, and sink the output into an IO stream. Trim the padding off the last # block. s = io.BytesIO() ends_at = 0 for block in pprp.rjindael_decrypt_gen(key, encrypted_gen, block_size): ends_at += block_size if ends_at >= len(data): block = pprp.trim_pkcs7_padding(block) s.write(block) decrypted = s.getvalue() assert data == decrypted.decode('ASCII')