示例#1
0
 def get_cipher(self):
     """Return an M2Crypto.SSL.Cipher object for this connection; if the
     connection has not been initialised with a cipher suite, return None."""
     c = m2.ssl_get_current_cipher(self.ssl)
     if c is None:
         return None
     return Cipher(c)
示例#2
0
 def decrypt(self, password):
     c = Cipher(password)
     decrypted = c.decode(self.decoded)
     if not decrypted:
         logging.error('Did not decode properly.')
         return False
     to_write = base64.b64decode(decrypted)
     with open('decrypted.jpg', 'wb') as fh:
         fh.write(to_write)
     return True
示例#3
0
    def decrypt_music(self):
        key = self.read_key()
        x = Cipher(key)
        path = path_to_file
        with open(path, 'rb') as musicfile:
            decsound = musicfile.read()

            assert decsound is not None, 'CANNOT READ SOUND FILE!!!'
            decsound = x.decrypt(decsound)
        return decsound
示例#4
0
    def main(self, *args, **kwargs):
        secret = self.read_key()
        x = Cipher(secret)
        content = None


        with open(path_to_file, 'rb') as fd:
            content = fd.read()
        assert content is not None, 'CANNOT READ FILE TO ENCRYPTION!!!'

        a = x.encrypt(content)
        with open('output1', 'wb') as fd:
            fd.write(a)
示例#5
0
    def __init__(self, window_name):
        self.backend = BackendJson("data")
        self.cipher = Cipher()

        self.root = tkinter.Tk()
        self.root.title(window_name)
        # self.root.iconbitmap("./ico/icon.ico")
        # размер и положение окна (середира экрана)
        wx = self.root.winfo_screenwidth()
        wy = self.root.winfo_screenheight()
        x = int(wx / 2 - 600 / 2)
        y = int(wy / 2 - 400 / 2)
        self.root.geometry(f"600x400+{x}+{y}")
        self.root.resizable(False, False)
        # вызов начального фрейма
        self.frame_home()
示例#6
0
    def gen_cipher_file(self, cipherdata, modedata):
        headers = set([h for m in cipherdata + modedata for h in m['headers']])
        ciphers = [Cipher(defaultdict(none_factory(), c)) for c in cipherdata]
        modes = [CipherMode(defaultdict(none_factory(), m),
                            [c for c in cipherdata
                             if c.get('family') in ('aes', 'camellia')])
                 for m in modedata]
        classes = ciphers + modes
        self.objects.extend(classes)
        self.ciphers = ciphers

        self.write_class_file(self.cipher_file, classes, headers)
        self.write_doc_file(self.cipher_doc_file, "Ciphers",
                            docstrings.cipher_example, ciphers)
        self.write_doc_file(self.ciphermode_doc_file, "Cipher Modes",
                            docstrings.ciphermode_example, modes)
示例#7
0
 def run(self, data, _key):
     state = State(data)
     key = Key(_key)
     cipher = Cipher(state, key)
     cipher.encrypt()
     cipher.decrypt()
示例#8
0
    def encrypt(self, password):
        # Base64 of the encrypted image data.
        c = Cipher(password)
        self.b64encrypted = c.encode(base64.b64encode(self.bin_image))
        logging.debug('Encrypted b64: ' + self.b64encrypted)
        logging.info('Length of b64encoded: %d.' % len(self.b64encrypted))
        to_hexify = self.b64encrypted

        # ECC encode to hex_string.
        if FLAGS.ecc:
            logging.info('ECCoder encoding input length:  %d.' % \
                           len(self.b64encrypted))
            coder = ECCoder(FLAGS.ecc_n, FLAGS.ecc_k, FLAGS.threads)
            encoded = coder.encode(self.b64encrypted)
            logging.info('ECCoder encoding output length: %d.' % len(encoded))
            to_hexify = encoded

        # Hexified data for encoding in the uploaded image.
        hex_data = binascii.hexlify(to_hexify)
        self.enc_orig_hex_data = hex_data
        num_data = len(hex_data)
        logging.debug('Original encrypted hex Data: ' + hex_data)

        if FLAGS.extra_logs:
            with open('hex_data.log', 'w') as _:
                for i in range(0, len(hex_data), 80):
                    _.write(hex_data[i:min(i + 80, len(hex_data))] + '\n')

        logging.info('Length of hex_data: %d' % num_data)
        width, length = self.image.size
        width_power_2 = int(math.ceil(math.log(width, 2)))
        TARGET_WIDTH = 2**(width_power_2 + 1)
        logging.info('Width: %d.' % TARGET_WIDTH)

        width = int(TARGET_WIDTH / (self.block_size * 2.))
        height = int(math.ceil(num_data / (1. * width)))
        logging.info('Encrypted image (w x h): (%d x %d).' % (width, height))
        logging.info('Expected image (w x h): (%d x %d).' % \
                        (TARGET_WIDTH, height*self.block_size))

        # Create the base for the encrypted image.
        rgb_image_width = width * self.block_size * 2
        rgb_image_height = height * self.block_size

        self.rgb_image = Image.new('RGB', (rgb_image_width, rgb_image_height))
        colors = [(255, 255, 255), (255, 0, 0), (0, 255, 0), (0, 0, 255)]

        draw = ImageDraw.Draw(self.rgb_image)
        for i, hex_datum in enumerate(hex_data):
            #logging.info('hex_datum (%d): %s.' % (i, hex_datum))
            hex_val = int(hex_datum, 16)
            base4_1 = int(hex_val / 4.0)  # Implicit floor.
            base4_0 = int(hex_val - (base4_1 * 4))
            y_coord = int(i / (1. * width))
            x_coord = int(i - (y_coord * width))

            # base4_0
            base4_0_x = int(x_coord * self.block_size * 2)
            base4_0_y = int(y_coord * self.block_size)
            base4_0_rectangle = \
                [(base4_0_x, base4_0_y),
                 (base4_0_x + self.block_size, base4_0_y + self.block_size)]
            draw.rectangle(base4_0_rectangle, fill=colors[base4_0])

            # base4_1
            base4_1_x = int((x_coord * self.block_size * 2) + self.block_size)
            base4_1_y = int(y_coord * self.block_size)
            base4_1_rectangle = \
              [(base4_1_x, base4_1_y),
               (base4_1_x + self.block_size, base4_1_y + self.block_size)]
            draw.rectangle(base4_1_rectangle, fill=colors[base4_1])

        filename = 'rgb.jpg'
        logging.info('Saving %s (quality: %d).' % \
                       (filename, FLAGS.encrypted_image_quality))
        self.rgb_image.save(filename, quality=FLAGS.encrypted_image_quality)
        return filename
示例#9
0
def do_encryption(message, offset):
    alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
    process_method = CaesarCharProc(alphabet, offset)
    cipher = Cipher(process_method)
    encrypted_message = cipher.encrypt(message)
    print(f'Encrypted: {encrypted_message}')
示例#10
0
文件: app.py 项目: Elvira27/homework
from Cipher import Cipher

cip = Cipher()

key = input('Введи ключ: ')
text = input('Введи слово для шифрования: ')
key_text = input('Введи слово для расшифрования: ')

d = cip.cipher(key, text, key_text)

print(d)