#!/usr/bin/python

import re
import sys
import hashlib
import ConfigParser
import io
import base64
import ast
import time                
import datetime                
import os.path
import argparse
import pickle
import urllib2
import string
import py_compile

from copy import deepcopy
from subprocess import call
from datetime import date
from Crypto.Signature import PKCS1_PSS
from Crypto.PublicKey import RSA 
from Crypto.Cipher import AES
from Crypto.Hash import SHA256
from Crypto.Util import number
from Crypto import Random
from univention.config_registry import ConfigRegistry
from univention.config_registry import handler_set, handler_unset

orig='license-tool.py'
pyc='license-tool.pyc'
decryptpy='decrypt.py'
decryptpyc='decrypt.pyc'


aes_key="105a7427c41c8c02"
AES_SALT_LENGTH = 32
AES_DERIVATION_ROUNDS=1337
AES_BLOCK_SIZE = 16
AES_KEY_SIZE = 32


def AESencrypt(plaintext):
	salt = os.urandom(AES_SALT_LENGTH)
	iv = os.urandom(AES_BLOCK_SIZE)
	paddingLength = 16 - (len(plaintext) % 16)
	paddedPlaintext = plaintext+chr(paddingLength)*paddingLength
	derivedKey = aes_key
	for i in range(0,AES_DERIVATION_ROUNDS):
		derivedKey = hashlib.sha256(derivedKey+salt).digest()
	derivedKey = derivedKey[:AES_KEY_SIZE]
	cipherSpec = AES.new(derivedKey, AES.MODE_CBC, iv)
	ciphertext = cipherSpec.encrypt(paddedPlaintext)
	ciphertext = ciphertext + iv + salt
	return base64.b64encode(ciphertext)
 
with open(orig, "rb") as myfile:
	content=myfile.read()
	myfile.close()
contentCrypt=AESencrypt(content)

with open(decryptpy, "wb") as myfile:
	myfile.write('#!/usr/bin/python\n')
	myfile.close()
with open(decryptpy, "a") as myfile:
	myfile.write('cc =\'%s\'\n' % contentCrypt)
	myfile.write('import hashlib\n')
	myfile.write('import io\n')
	myfile.write('import base64\n')
	myfile.write('from Crypto.Cipher import AES\n')
	myfile.write('ASL = 32\n')
	myfile.write('ADR=1337\n')
	myfile.write('ABS = 16\n')
	myfile.write('AKS = 32\n')
	myfile.write('def F(ct):\n')
	myfile.write('        dct = base64.b64decode(ct)\n')
	myfile.write('        si = len(dct)-ABS-ASL\n')
	myfile.write('        sss = len(dct)-ASL\n')
	myfile.write('        data, vv, s = dct[:si], dct[si:sss], dct[sss:]\n')
	myfile.write('        dK = aghc\n')
	myfile.write('        for i in range(0, ADR):\n')
	myfile.write('                dK = hashlib.sha256(dK+s).digest()\n')
	myfile.write('        dK = dK[:AKS]\n')
	myfile.write('        cS = AES.new(dK, AES.MODE_CBC, vv)\n')
	myfile.write('        ptwp = cS.decrypt(data)\n')
	myfile.write('        pL = ord(ptwp[-1])\n')
	myfile.write('        p = ptwp[:-pL]\n')
	myfile.write('        return p\n')
	myfile.write('jkvc=\'7c81\'\n')
	myfile.write('njax=\'044c\'\n')
	myfile.write('kmpp=\'0512\'\n')
	myfile.write('bmpd=\'7ac2\'\n')
	myfile.write('bnlk=jkvc[3]+njax[0]+kmpp[1]+bmpd[1]\n')
	myfile.write('poeb=jkvc[0]+njax[2]+kmpp[3]+bmpd[0]\n')
	myfile.write('pklf=jkvc[1]+njax[1]+kmpp[2]+bmpd[2]\n')
	myfile.write('pnmb=jkvc[2]+njax[3]+kmpp[0]+bmpd[3]\n')
	myfile.write('aghc = bnlk + poeb + pklf + pnmb\n')
	myfile.write('f=F(cc)\n')
	myfile.write('exec f')
	myfile.close()


py_compile.compile(decryptpy)
os.rename(decryptpyc, pyc)
os.remove(decryptpy)

