Help wanted: stickynotes encryption problems

49 views
Skip to first unread message

Edward K. Ream

unread,
Jul 31, 2019, 9:45:15 AM7/31/19
to leo-editor
I fixed the original cause of #1265 only to run into a more tricky problem: `binascii.Error: Incorrect padding` in sn_decode function.

The fixes suggested here do not work as is. Perhaps the input string must be converted to a b64_string first. As you can see, this is a tricky topic.  I am stuck, and would like some help.

I'll entertain fixes to this issue only before Leo 6.0 goes out the door this coming Friday.  Otherwise, this issue will wait until Leo 6.1.

Edward

vitalije

unread,
Jul 31, 2019, 11:23:58 AM7/31/19
to leo-e...@googlegroups.com
I have found two mistakes in stickynotes.py, but I have installed old Crypto module which has slightly different way of encrypting/decrypting.

The first one is that it refuses to create encrypted stickynote from empty node which is, according to the documentation, proper way to create encrypted stycky note.
@g.command('stickynoteenc')
def stickynoteenc_f(event, rekey=False):
    """ Launch editable 'sticky note' for the encrypted node """
    if not encOK:
        g.es('no en/decryption - need python-crypto module')
        return
    if not __ENCKEY[0] or rekey:
        sn_getenckey()
    c = event['c']
    p = c.p
    v = p.v

    def focusin():
        if v is c.p.v:
            decoded = sn_decode(v.b)
            if decoded is None:
                return
            if decoded != nf.toPlainText():
                # only when needed to avoid scroll jumping
                nf.setPlainText(decoded)
            nf.setWindowTitle(p.h)
            nf.dirty = False

    def focusout():
        if not nf.dirty:
            return
        enc = sn_encode(str(nf.toPlainText()))
        if v.b != enc:
            v.b = enc
            v.setDirty()
            # Fix #249: Leo and Stickynote plugin do not request to save
            c.setChanged(True)
        nf.dirty = False
        p = c.p
        if p.v is v:
            c.selectPosition(c.p)
        c.redraw()

    if rekey:
        unsecret = sn_decode(v.b)
        if unsecret is None:
            return
        sn_getenckey()
        secret = sn_encode(unsecret)
        v.b = secret
    // this is new - check if it is old note already encrypted
    if v.b:
        decoded = sn_decode(v.b)
        if decoded is None:
            return
    else:
        // creating new encrypted note
        decoded = v.b
    nf = mknote(c,p, focusin=focusin, focusout=focusout)
    nf.setPlainText(decoded)
    if rekey:
        g.es("Key updated, data decoded with new key shown in window")

See the red colored code.

The other mistake is in padding string. It is necessary to pad bytes not unicode strings.

def sn_encode(s):

    s1 = s.encode('utf8')

    pad = b' '*(16-len(s1)%16)

    txta = get_encoder().encrypt(s1 + pad)

    txt = base64.b64encode(txta)

    txt = str(txt, 'utf-8')

    wrapped = textwrap.wrap(txt, break_long_words=True)

    return '\n'.join(wrapped)



With this two changes it works for me although I get crashes if calling AES.new as in pycryptodome.

The encryption / decryption itself must be handled separately to check if we have new or old Crypto module.
The old one doesn't have AES.MODE_EAX.

def get_encoder():
    key = __ENCKEY[0]
    if hasattr(AES, 'MODE_EAX'):
        return AES.new(key, AES.MODE_EAX)
    else:
        return AES.new(key)
# txt = get_encoder().encrypt(s1 + pad)

# or

# decoded = get_encoder().decrypt(s1)



HTH Vitalije

vitalije

unread,
Jul 31, 2019, 11:33:04 AM7/31/19
to leo-editor
I really don't know what is wrong with code blocks in google groups. Whenever I copy code from Leo and paste it in a message, each line of code is separated by an empty line. If I try to type in code directly it prevents me to append new line, or to add space at the end of line, quite annoying really. Now in the previous message I had pasted code to Atom editor, then copied it again and pasted here. The first code block was ok, the second was turned into a single line. So I had to edit previous message and when I manually added line breaks and saved message, now it is again turned each line break into two line breaks.

Does anybody else notices similar behavior? I am using  Google Chrome, regularly updated.

Vitalije 

Matt Wilkie

unread,
Aug 1, 2019, 7:30:54 PM8/1/19
to leo-editor
Does anybody else notices similar behavior? I am using  Google Chrome, regularly updated.

I'm not seeing this with up to date Firefox on Windows right now, but I have run into formatting wierdness in g-groups before. One thing that helped was turning off the browser automatic spell checking.

-matt

Matt Wilkie

unread,
Aug 2, 2019, 5:44:04 PM8/2/19
to leo-editor

Does anybody else notices similar behavior? I am using  Google Chrome, regularly updated.

I'm not seeing this with up to date Firefox on Windows right now, but I have run into formatting wierdness in g-groups before. One thing that helped was turning off the browser automatic spell checking.

Aha! Ran into this problem today, with Firefox. The fix seems to be 1) initiate code block using toolbar button, then 2) paste the code into the block. When I typed/pasted the code first and then turned into a code block it added line breaks.

-matt

Reply all
Reply to author
Forward
0 new messages