GithubHelp home page GithubHelp logo

py-cidecrypt's People

Contributors

hussaintamboli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

screenmagicmm

py-cidecrypt's Issues

Fixed version for Python 2.7 + non ascii input characters

The code didn't work out of the box for me..

So I'll leave this here for the next poor soul who has to dig into an old codeigniter database with Python:

# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import hashlib
import base64
import mcrypt # Downloaded from https://pypi.python.org/pypi/python-mcrypt
# or http://labix.org/python-mcrypt.
# If the installation gives error - "mycrypt.h file not found" then
# Install libmcrypt or  libmcrypt-dev . See the document in python help(mcrypt)

class Decrypt:

    def __init__(self, key=''):
        self.key = key
        self.SUCCESS, self.FAILED = True, False
        self.KEY_SIZE = 32
        self.BLOCK_SIZE = 32

    def getKey(self, key):
        md5Key = hashlib.md5()
        md5Key.update(key)
        return md5Key.hexdigest()

    def getBase64Decode(self, encString):
        b64DecString = base64.b64decode(encString)
        return b64DecString

    def hashMethod(self, key):
        hashStr = ''
        try:
            hashStr = hashlib.sha1(key).hexdigest()
        except Exception as e:
            return self.FAILED
        return hashStr

    def removeCipherNoise(self, string, key):
        keyHash = self.hashMethod(key)
        keylen = len(keyHash)
        stri = ''.encode('iso-8859-1')
        i, j = 0, 0
        length = len(string)
        while i < length:
            if j >= keylen:
                j = 0
            temp = ord(string[i]) - ord(keyHash[j])
            if temp < 0:
                temp = temp + 256
            stri += chr(temp)
            i += 1
            j += 1
        return stri

    def mcryptDecode(self, string, key):
        string = self.removeCipherNoise(string, key)
        initSize = 32
        initVect = string[0:initSize]
        string = string[initSize:]
        decoded = self.mcryptDecryption(key, initVect, string)
        if decoded == self.FAILED:
            return self.FAILED
        return decoded.rstrip('\x00')

    def decode(self, string):
        if not self.key:
            return self.FAILED
        dec = self.getBase64Decode(string)
        decoded = self.mcryptDecode(dec, self.key)
        if decoded == self.FAILED:
            return self.FAILED
        return decoded

    def mcryptDecryption(self, key, iv, data):
        try:
            m = mcrypt.MCRYPT('rijndael-256', 'cbc')
            m.init(key, iv)
            decrypted = m.decrypt(data)
        except Exception as e:
            return self.FAILED
        return decrypted

text = 'ENCRYPTED_TEXT'
dec = Decrypt(key='YOUR_KEY')

print dec.decode(text)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.