コード例 #1
0
def encrypt_all(password, method, op, data):
    if method is not None and method.lower() == 'table':
        method = None
    if not method:
        [encrypt_table, decrypt_table] = init_table(password)
        if op:
            return string.translate(data, encrypt_table)
        else:
            return string.translate(data, decrypt_table)
    else:
        import M2Crypto.EVP
        result = []
        method = method.lower()
        (key_len, iv_len) = method_supported[method]
        (key, _) = EVP_BytesToKey(password, key_len, iv_len)
        if op:
            iv = random_string(iv_len)
            result.append(iv)
        else:
            iv = data[:iv_len]
            data = data[iv_len:]
        if method != 'salsa20-ctr':
            cipher = M2Crypto.EVP.Cipher(method.replace('-', '_'),
                                         key,
                                         iv,
                                         op,
                                         key_as_bytes=0,
                                         d='md5',
                                         salt=None,
                                         i=1,
                                         padding=1)
        else:
            cipher = encrypt_salsa20.Salsa20Cipher(method, key, iv, op)
        result.append(cipher.update(data))
        return ''.join(result)
コード例 #2
0
    def get_cipher(self, password, method, op, iv=None):
        password = password.encode('utf-8')
        method = method.lower()
        m = self.get_cipher_len(method)
        if m:
            key, iv_ = EVP_BytesToKey(password, m[0], m[1])
            if iv is None:
                iv = iv_
            iv = iv[:m[1]]
            if op == 1:
                self.cipher_iv = iv[:
                                    m[1]]  # this iv is for cipher not decipher
            if method != 'salsa20-ctr':
                import M2Crypto.EVP
                return M2Crypto.EVP.Cipher(method.replace('-', '_'),
                                           key,
                                           iv,
                                           op,
                                           key_as_bytes=0,
                                           d='md5',
                                           salt=None,
                                           i=1,
                                           padding=1)
            else:
                return encrypt_salsa20.Salsa20Cipher(method, key, iv, op)

        logging.error('method %s not supported' % method)
        sys.exit(1)