Exemple #1
0
    def generate(self,key,question_answers,title,content):
        # The encrypt key is generated as follows:
        #   1. Sort all Q&A answers.
        #   2. Join all Q&A answers with ";".
        #   3. HMAC the result with given key.
        # Text being encrypted is combined with content and time,
        # in a way that is human-friendly.
        #   The question, the encrypt key's hash are stored together
        # with ciphertext, so that it's easy to verify user's input
        # without attempted decrytion.

        enckey = self._calculate_key(key,question_answers)
        timestamp = time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())
        content = "%s\n\n%s" % (timestamp,content)
        title = formatTitle(title)
        ciphertext = x(enckey).encrypt(content)

        integrity_check = self._calculate_integrity(enckey,
                                                    ciphertext,
                                                    title)

        constructed = {
            'key': Hash('md5',enckey).hexdigest(),
            'questions': question_answers.keys(),
            'title': title,
            'content': ciphertext.encode('base64').replace('\n',''),
            'integrity': integrity_check
        }

        return json.dumps(constructed,indent=4)
Exemple #2
0
def listkeys(dpath,gkey):
    l = os.listdir(dpath)
    ret = {}
    for each in l:
        fpath = os.path.join(dpath,each)
        try:
            fkey = x(gkey).decrypt(open(fpath,'r').read())
        except:
            continue
        ret[each] = fkey
    return ret
Exemple #3
0
    def read(self,key_callback,qa_callback):
        key = str(key_callback(u'请输入解密密钥/Enter decrypting key:'))
        qa = dict(qa_callback(u'请回答这些问题/Answer these questions:',self.questions))
        enckey = self._calculate_key(key,qa)

        if Hash('md5',enckey).hexdigest() != self.keyhash:
            print '密码错误或问题回答错误 / Incorrect key or answers'
            return False
        if self.integrity_check != self._calculate_integrity(enckey,
                                                             self.ciphertext,
                                                             self.title):
            print '内容完整性校验失败 可能文件已经被篡改 / Integrity check failed This file may have been altered'
            return False

        try:
            return x(enckey).decrypt(self.ciphertext)
        except:
            return False
Exemple #4
0
def handler(gkey,dpath,epath):
    title, content, qas = '','',{}
    while True:
        s = e(title,content,qas)
        s.showDialog()
        result = s.result
        if result != None:
            # Do a confirm
            title = formatTitle(result['title'])
            content = str(result['content'])
            qas = dict(result['qas'])

            clearScreen()
            if (not title) or (not content):
                print '标题或内容有误'
                continue
            print '即将记录如下信息:'
            print '标题 %s' % title.encode('utf-8')
            print '问答'
            for q in qas:
                print " * [%s] %s" % (q,qas[q])
            print '内容\n%s' % content
            print '--------'
            cmd = raw_input('<Enter>:重新修改 s+<Enter>:保存 其他:取消').strip().lower()
            if cmd == 's':
                break
            elif cmd == '':
                continue
            else:
                return
        else:
            return

    # Save
    savekey = randkey(128)
    
    open(os.path.join(dpath,title),'w+').write(x(gkey).encrypt(savekey))

    doc = docformat.EncryptedFile()
    plaintext = doc.generate(savekey,qas,title,content)

    open(os.path.join(epath,title),'w+').write(plaintext)

    raw_input('保存完毕,按任意键返回')