Example #1
0
def encrypt_hex(key, data):
    """
    @note: 加密内容变为hex
    @return: bytes
    """
    try: return xxtea.encrypt_hex(data, key)
    except: return b""
Example #2
0
 def test_encrypt_hex_nopadding_zero(self):
     key = os.urandom(16)
     for i in (8, 12, 16, 20):
         data = b'\0' * i
         enc = xxtea.encrypt_hex(data, key, padding=False)
         dec = xxtea.decrypt_hex(enc, key, padding=False)
         self.assertEqual(data, dec)
Example #3
0
    def test_hex_encode(self):
        for i in range(2048):
            key = os.urandom(16)
            data = os.urandom(i)

            enc = xxtea.encrypt(data, key)
            hexenc = xxtea.encrypt_hex(data, key)
            self.assertEqual(binascii.b2a_hex(enc), hexenc)
Example #4
0
    def post(self):
        email = self.get_body_argument("email", default=None)
        auth = self.get_body_argument("auth", default=None)
        # after users submit their email
        if email:
            # check captcha
            captcha = self.get_body_argument("captcha", default="")
            if not Captcha.check(captcha, self):
                self.custom_error("验证码错误")

            user = yield self.db.member.find_one({
                "email": email
            })
            if not user:
                self.custom_error("不存在这个Email")
            sign = "%s|%s|%s" % (user["username"], user["password"], time.time())
            svalue = xxtea.encrypt_hex(utf8(sign), self.get_byte_16(self.settings.get("cookie_secret")))
            url = self.settings.get("base_url") + "/forgetpwd?auth=%s" % url_escape(svalue, False)
            Sendemail(self.settings.get("email")).send(
                to=user["email"],
                orgin="*****@*****.**",
                title=u"找回密码 - %s" % self.settings["site"]["webname"],
                content=u"点击链接找回你的密码:<br /><a href=\"%s\">%s</a><br />如果你没有找回密码,请忽视这封邮件" % (url, url)
            )
            self.render("forgetpwd.htm", success=True)
        # after users click url in their email, and submit a new password
        elif auth:
            newpwd = self.get_body_argument("password")
            try:
                svalue = xxtea.decrypt_hex(utf8(auth), self.get_byte_16(self.settings.get("cookie_secret")))
                (username, password, t) = svalue.split("|")
            except:
                self.custom_error("参数错误,请重新找回密码", jump="/forgetpwd")
            if time.time() - float(t) > 30 * 60:
                self.custom_error("链接已过期,请在30分钟内点击链接找回密码", jump="/forgetpwd")
            newpwd = yield self.backend.submit(hash.get, newpwd)
            user = yield self.db.member.find_and_modify({
                "username": username, "password": password
            }, {
                "$set": {"password": newpwd}
            })
            if not user:
                self.custom_error("参数错误,请重新找回密码", jump="/forgetpwd")
            else:
                self.custom_error("密码修改成功", jump="/login", status="success")
        else:
            self.custom_error("不存在这个Email")
Example #5
0
def set_environ(varname='', data='', key=None):
    """
    设置环境变量值(加密数据)

    :param varname: str, 环境变量名称
    :param data: 值
    :param key: 加密的 key
    :return: str
    """
    if key:
        try:
            data = xxtea.encrypt_hex(data, key).decode('utf-8')
        except Exception:
            data = ''

    os.environ[varname] = data

    return data
Example #6
0
 def test_encrypt_hex(self):
     hexenc = xxtea.encrypt_hex(self.data, self.key)
     self.assertEqual(hexenc, self.hexenc)
Example #7
0
if var == '0':

    df = open("text.txt", "r")
    data = df.read().encode("cp1251").decode("utf-8")
    df.close()
    print("Входящие данные")
    print(data)

    key = key_gen(16)
    kf = open("key.txt", "w", encoding='utf-8')
    kf.write(key)
    kf.close()
    print("ключ")
    print(key)

    shifr = xxtea.encrypt_hex(data, key.encode('utf-8'))
    print("шифр")
    print(shifr.decode('utf-8'))
    sh = open("shifr.txt", "w", encoding='utf-8')
    sh.write(shifr.decode('utf-8'))
    sh.close()
else:
    df = open("shifr.txt", "r")
    data = df.read().encode('utf-8')
    df.close()
    print("Входящие данные")
    print(data)

    kf = open("key.txt", "r")
    key = kf.read().encode('utf-8')
    kf.close()
Example #8
0
 def test_encrypt_hex(self):
     hexenc = xxtea.encrypt_hex(self.data, self.key)
     self.assertEqual(hexenc, self.hexenc)
Example #9
0
    try:
        length = int(sys.argv[1])
        times = int(sys.argv[2])
    except:
        print('Usage: {} datalength times'.format(sys.argv[0]),
              file=sys.stderr)
        sys.exit(-1)

    testkey = os.urandom(16)
    testdata = os.urandom(length)

    t = timeit.Timer('encrypt({}, {})'.format(repr(testdata), repr(testkey)),
                     'from xxtea import encrypt')
    print('    encrypt:', t.timeit(times))

    testdata = xxtea.encrypt(testdata, testkey)
    t = timeit.Timer('decrypt({}, {})'.format(repr(testdata), repr(testkey)),
                     'from xxtea import decrypt')
    print('    decrypt:', t.timeit(times))

    t = timeit.Timer(
        'encrypt_hex({}, {})'.format(repr(testdata), repr(testkey)),
        'from xxtea import encrypt_hex')
    print('encrypt_hex:', t.timeit(times))

    testdata = xxtea.encrypt_hex(testdata, testkey)
    t = timeit.Timer(
        'decrypt_hex({}, {})'.format(repr(testdata), repr(testkey)),
        'from xxtea import decrypt_hex')
    print('decrypt_hex:', t.timeit(times))
def index(request):
    a = xxtea.encrypt_hex(data, key)
    b = xxtea.decrypt_hex(enchex, key)
    return HttpResponse(a + " " + b)
import os

from django.http import HttpResponse

import xxtea

key = os.urandom(16)
data = os.urandom(19)
enchex = xxtea.encrypt_hex(data, key)

def index(request):
    a = xxtea.encrypt_hex(data, key)
    b = xxtea.decrypt_hex(enchex, key)
    return HttpResponse(a + " " + b)
Example #12
0
@author = super_fazai
@File    : demo.py
@connect : [email protected]
'''

# xxtea加解密算法
# pip3 install xxtea
# https://pypi.org/project/xxtea/

import os
import xxtea
import binascii

# Key must be a 16-byte string.
key = os.urandom(16)
print('key: {}'.format(key))
s = b"xxtea is good"

enc = xxtea.encrypt(s, key)
dec = xxtea.decrypt(enc, key)
print(s == dec)

hexenc = xxtea.encrypt_hex(s, key)
print(hexenc)
print(s == xxtea.decrypt_hex(hexenc, key))

from base64 import b64decode

key = os.urandom(16)
s = 'QUvZd5mSBl+PxFo8LFfHOdVRBxbdL6CC0JrFtY/DlOHZQuLhrnRzdUvdq4ZHTCWuNTWCm5/K/pQ8jw8FaX7tPW1QR7ik7FqvDrKIPPEg4n7PjQogcabkyA4cBKk='
xxtea.decrypt(s, key=key)
Example #13
0
import sys
import timeit
import xxtea


if __name__ == '__main__':
    try:
        length = int(sys.argv[1])
        times = int(sys.argv[2])
    except:
        print('Usage: {} datalength times'.format(sys.argv[0]), file=sys.stderr)
        sys.exit(-1)

    testkey = os.urandom(16)
    testdata = os.urandom(length)

    t = timeit.Timer('encrypt({}, {})'.format(repr(testdata), repr(testkey)), 'from xxtea import encrypt')
    print('    encrypt:', t.timeit(times))

    testdata = xxtea.encrypt(testdata, testkey)
    t = timeit.Timer('decrypt({}, {})'.format(repr(testdata), repr(testkey)), 'from xxtea import decrypt')
    print('    decrypt:', t.timeit(times))

    t = timeit.Timer('encrypt_hex({}, {})'.format(repr(testdata), repr(testkey)), 'from xxtea import encrypt_hex')
    print('encrypt_hex:', t.timeit(times))

    testdata = xxtea.encrypt_hex(testdata, testkey)
    t = timeit.Timer('decrypt_hex({}, {})'.format(repr(testdata), repr(testkey)), 'from xxtea import decrypt_hex')
    print('decrypt_hex:', t.timeit(times))

Example #14
0
 def TEstA_Send(self, text):
     enckey = self.secret_key.encode()
     encrypted = xxtea.encrypt_hex(text, enckey, padding=False)
     encrypted = encrypted.decode()
     encrypted = encrypted.upper()
     self.serialPort.write(bytes(str(encrypted), "utf-8"))