예제 #1
0
파일: aserver.py 프로젝트: pylogic/bootcamp
def api():
    """only for fd api"""
    
    try:
        incoming = request.get_json()
        key = incoming.get('key')
        text = incoming.get('text')
        if 0 < len(text) <= 200 and 0 < len(key)<=8:
            chars = ts.filterchars(text)
            keychar = ts.filterchars(key)
            if len(chars) == 0 or len(keychar) == 0:
                return jsonify({'error': 'invalid chars'})
        else:
            return jsonify({'error': 'too long chas'})
        
    except:
        return jsonify({'error': 'no json data get'})
    if incoming.get('mode') == 'enc':
        ecr = puer.encrypt(key, text)
        return jsonify({'encrypted': True,
                              'res':ecr})
    elif incoming.get('mode') == 'dec':
        dcr = puer.decrypt(key, text)
        return jsonify({'decrypted': True,
                         'res': dcr
                         })
    else:
        return jsonify({'error': 'mission mode'})
    
    return jsonify({'res': 'ok'})
예제 #2
0
def index():
    if request.method == 'POST':
        text = request.form['text']
        key = request.form['key']
        _ = ts.update(text)
        _ = ts.update(key)
        chars = ts.filterchars(text)
        keychar = ts.filterchars(key)
        ecr = ts.encryptext(chars, keychar)
        r = {'key': keychar, 'text': ecr}
        return ecr

    else:
        return render_template('index.html', user=False)
예제 #3
0
def decrypt():
    if request.method == 'POST':
        text = request.form['text']
        key = request.form['key']
        # _ = ts.update(text)
        # _ = ts.update(key)
        chars = ts.filterchars(text)
        keychar = ts.filterchars(key)
        dcr = ts.decryptext(chars, keychar)
        r = {'key': keychar, 'text': dcr}
        return dcr

    else:
        return '''
예제 #4
0
def encrypt():
    """only for api call"""
    try:
        text = request.form['text']
        key = request.form['key']
    except:
        return jsonify({'error': 'no data'})

    if 0 < len(text) < 200 and 0 < len(key) < 8:
        chars = ts.filterchars(text)
        keychar = ts.filterchars(key)

        if len(chars) > 0 and len(keychar) > 0:
            ecr = puer.encrypt(key, text)
            return jsonify({'key': keychar, 'text': ecr})
        else:
            return jsonify({'error': 'no valid chars'})

    else:
        return jsonify({'error': 'too long text or key'})
예제 #5
0
파일: server.py 프로젝트: pylogic/bootcamp
def decrypt():
    """decrypt weibo"""
    if request.method == 'GET':
        return '''
                   <h2>解密文字</h2>
                   <form method="post">
                   <p><textarea cols=40 rows=10 name=text value='' style="background-color:BFCEDC"></textarea>
                   <p><input type=text name=key value='密码'>
                   <p><input type=submit value=DECRYPT>
               </form>
               '''
    #elif session['access_token']:
    else:
        text = request.form['text']
        key = request.form['key']
        # _ = ts.update(text)
        # _ = ts.update(key)
        chars = ts.filterchars(text)
        keychar = ts.filterchars(key)
        dcr = ts.decryptext(chars, keychar)
        r = {'key': keychar,
             'text': dcr}
        return dcr
예제 #6
0
파일: server.py 프로젝트: pylogic/bootcamp
def postweibo():
    # send a test weibo. the posted text will truncated and add a tail

    time = '%s automatically update. ' % datetime.datetime.now()
    # status = request.args.get('t', time)

    if 'access_token' in session and request.method == 'POST':
        text = request.form['text']
        text = text[:200]
        key = request.form['key']
        _ = ts.update(text)
        _ = ts.update(key)
        chars = ts.filterchars(text)
        keychar = ts.filterchars(key)
        ecr = ts.encryptext(chars, keychar)

        if len(ecr) > 122:
            ecr = ecr[:122]
        tail = 'https://aishe.org.cn 密码:%s' % keychar # must has this end
        sentpost = '%s %s' % (ecr, tail)
        client = WeiboClient(session['access_token'])
        result = client.post("statuses/share.json", data={"status":sentpost, "access_token":session['access_token']})
        # result = client.session.post('https://api.weibo.com/2/statuses/update.json', data={"status":"test article test article"})
        return jsonify(result)
    elif 'access_token' in session and request.method == 'GET':
        return '''
            <h2>加密并分享至微博</h2>
            <a>密码1-8常用汉字,正文不超过128字偶数汉字,非汉字会被丢弃</a>
            <form method="post">
            <p><textarea cols=40 rows=10 name=text value='Limit 128 Char' style="background-color:BFCEDC"></textarea>
            <p><input type=text name=key value='密码'>
            <p><input type=submit value=ENCRYPTWB>
        </form>
        '''
    else:
        return redirect(url_for('login'))