Esempio n. 1
0
def keyFinder(s):
    engLetFreq = (0.0749, 0.0129, 0.0354, 0.0362, 0.1400, 0.0218, 0.0174, 0.0422, 0.0665, 0.0027, 0.0047, 0.0357,\
               0.0339, 0.0674, 0.0737, 0.0243, 0.0026, 0.0614, 0.0695, 0.0985, 0.0300, 0.0116, 0.0169, 0.0028,\
               0.0164, 0.0004)
    deltamin = 1000
    bestrot = 0
    freq = Message.frequency(s)
    for key in range(26):
        d = min([delta(freq[key:]+freq[:key], engLetFreq)])
        if d<deltamin:
            deltamin = d
            bestrot = key
    return bestrot
Esempio n. 2
0
def new_message():
    message = Message.from_json(request.json)
    if message.body == "" or message.body is None:
        abort(422)
    elif message.mode == "" or message.mode is None:
        abort(422)
    db.session.add(message)
    db.session.commit()
    listFrequency = Message.frequency(message.body)
    RoundedList = [ round(i*100, 2) for i in listFrequency ]
    frequency = [{'x': alphabet[i], 'y': [RoundedList[i]]} for i in range(26)]
    decipherKey = keyFinder(message.body)
    answerKey = None
    if decipherKey > 0 :
        answerKey = decipherKey
    message = Message.caesar_cipher(message)
    return jsonify({"message":message.to_json(),"freequency": frequency, "answerKey": answerKey}), 201