예제 #1
0
def encrypt(data, msgtype, servername, args):
  global encrypted
  pre, message = string.split(args, ":", 1)
  prestr=pre.split(" ")
  username=prestr[-2]
  buf = weechat.current_buffer()
  nick = weechat.buffer_get_string(buf, 'localvar_nick')
  if os.path.exists(weechat_dir + '/' + username + '.db'):

    a = Axolotl(nick, dbname=weechat_dir+'/'+username+'.db', dbpassphrase=getPasswd(username))
    a.loadState(nick, username)
    encrypted = a.encrypt(message)
    if encrypted == '':
        return args
    encrypted = b2a_base64(encrypted)
    a.saveState()
    del a
    encrypted = encrypted.replace("\n","")
    final_msg = pre + ":" +encrypted
    if len(encrypted) > 400:
      # I arrived at this next equation heuristically. If it doesn't work, let me know
      # and I will work on it some more. -DRA
      numsplits = 2*int(len(encrypted)/400) + 1
      splitmsg=string.split(message," ")
      cutpoint=int(len(splitmsg)/numsplits)
      encrypted_list = []
      for i in range(numsplits+1):
        if min((i+1)*cutpoint, len(splitmsg)) == (i+1)*cutpoint:
          segment = string.join(splitmsg[i*cutpoint:(i+1)*cutpoint]," ") + "\n"
          a = Axolotl(nick, dbname=weechat_dir+'/'+username+'.db', dbpassphrase=getPasswd(username))
          a.loadState(nick, username)
          encrypted = b2a_base64(a.encrypt(segment))
          a.saveState()
          del a
          valid_segment = True
        else:
          segment = string.join(splitmsg[i*cutpoint:]," ")
          if segment.strip() is None or len(segment) == 0:
            valid_segment = False
          else:
            a = Axolotl(nick, dbname=weechat_dir+'/'+username+'.db', dbpassphrase=getPasswd(username))
            a.loadState(nick, username)
            encrypted = b2a_base64(a.encrypt(segment))
            a.saveState()
            del a
            valid_segment = True
        encrypted = encrypted.replace("\n","")
        if valid_segment:
          encrypted_list += [encrypted]
      final_msg = ''
      for item in encrypted_list:
        final_msg = final_msg + pre + ":" + item + '\n'
    return final_msg
    return encrypted
  else:
    return args
예제 #2
0
def encrypt(data, msgtype, servername, args):
  global encrypted
  pre, message = string.split(args, ":", 1)
  prestr=pre.split(" ")
  username=prestr[-2]
  buf = weechat.current_buffer()
  nick = weechat.buffer_get_string(buf, 'localvar_nick')
  if os.path.exists(weechat_dir + '/' + username + '.db'):

    a = Axolotl(nick, dbname=weechat_dir+'/'+username+'.db', dbpassphrase=getPasswd(username))
    a.loadState(nick, username)
    encrypted = a.encrypt(message)
    if encrypted == '':
        return args
    encrypted = b2a_base64(encrypted)
    a.saveState()
    del a
    encrypted = encrypted.replace("\n","")
    final_msg = pre + ":" +encrypted
    if len(encrypted) > 400:
      # I arrived at this next equation heuristically. If it doesn't work, let me know
      # and I will work on it some more. -DRA
      numsplits = 2*int(len(encrypted)/400) + 1
      splitmsg=string.split(message," ")
      cutpoint=int(len(splitmsg)/numsplits)
      encrypted_list = []
      for i in range(numsplits+1):
        if min((i+1)*cutpoint, len(splitmsg)) == (i+1)*cutpoint:
          segment = string.join(splitmsg[i*cutpoint:(i+1)*cutpoint]," ") + "\n"
          a = Axolotl(nick, dbname=weechat_dir+'/'+username+'.db', dbpassphrase=getPasswd(username))
          a.loadState(nick, username)
          encrypted = b2a_base64(a.encrypt(segment))
          a.saveState()
          del a
          valid_segment = True
        else:
          segment = string.join(splitmsg[i*cutpoint:]," ")
          if segment.strip() is None or len(segment) == 0:
            valid_segment = False
          else:
            a = Axolotl(nick, dbname=weechat_dir+'/'+username+'.db', dbpassphrase=getPasswd(username))
            a.loadState(nick, username)
            encrypted = b2a_base64(a.encrypt(segment))
            a.saveState()
            del a
            valid_segment = True
        encrypted = encrypted.replace("\n","")
        if valid_segment:
          encrypted_list += [encrypted]
      final_msg = ''
      for item in encrypted_list:
        final_msg = final_msg + pre + ":" + item + '\n'
    return final_msg
    return encrypted
  else:
    return args
예제 #3
0
    def test_shared_db(self):
        # create two instance classes - one which will share its database
        # (note that Dick and Harry's passphrases must match or Harry won't
        # be able to load Dick's saved database)
        shared_pass = '******'
        tom = Axolotl('Tom', dbpassphrase="tom's passphrase")
        dick = Axolotl('Dick', dbpassphrase=shared_pass)

        # initialize Tom and Dick's states
        tom.initState(other_name=dick.name,
                      other_identityKey=dick.state['DHIs'],
                      other_handshakeKey=dick.handshakePKey,
                      other_ratchetKey=dick.state['DHRs'],
                      verify=False)
        dick.initState(other_name=tom.name,
                       other_identityKey=tom.state['DHIs'],
                       other_handshakeKey=tom.handshakePKey,
                       other_ratchetKey=tom.state['DHRs'],
                       verify=False)

        # get the plaintext
        msg = 'plaintext'

        # Tom encrypts it to Dick
        ciphertext = tom.encrypt(msg)

        # save Dick's state prior to decrypting the message
        dick.saveState()

        # Dick decrypts the ciphertext
        assert dick.decrypt(ciphertext) == msg

        # now load Dick's state to Harry
        harry = Axolotl('Harry', dbpassphrase=shared_pass)
        harry.loadState(dick.name, tom.name)

        # Harry decrypts the ciphertext
        assert harry.decrypt(ciphertext) == msg
예제 #4
0
파일: test_db.py 프로젝트: felipedau/pyaxo
    def test_shared_db(self):
        # create two instance classes - one which will share its database
        # (note that Dick and Harry's passphrases must match or Harry won't
        # be able to load Dick's saved database)
        shared_pass = '******'
        tom = Axolotl('Tom', dbpassphrase="tom's passphrase")
        dick = Axolotl('Dick', dbpassphrase=shared_pass)

        # initialize Tom and Dick's states
        tom.initState(other_name=dick.name,
                      other_identityKey=dick.state['DHIs'],
                      other_handshakeKey=dick.handshakePKey,
                      other_ratchetKey=dick.state['DHRs'],
                      verify=False)
        dick.initState(other_name=tom.name,
                       other_identityKey=tom.state['DHIs'],
                       other_handshakeKey=tom.handshakePKey,
                       other_ratchetKey=tom.state['DHRs'],
                       verify=False)

        # get the plaintext
        msg = 'plaintext'

        # Tom encrypts it to Dick
        ciphertext = tom.encrypt(msg)

        # save Dick's state prior to decrypting the message
        dick.saveState()

        # Dick decrypts the ciphertext
        assert dick.decrypt(ciphertext) == msg

        # now load Dick's state to Harry
        harry = Axolotl('Harry', dbpassphrase=shared_pass)
        harry.loadState(dick.name, tom.name)

        # Harry decrypts the ciphertext
        assert harry.decrypt(ciphertext) == msg
예제 #5
0
파일: test4.py 프로젝트: ghtdak/pyaxo
# initialize their states
a.initState('Barb', b.state['DHIs'], b.handshakePKey, b.state['DHRs'],
            verify=False)
b.initState('Angie', a.state['DHIs'], a.handshakePKey, a.state['DHRs'],
            verify=False)

# tell who is who
if a.mode:
    print 'Angie is Alice-like'
    print 'Barb is Bob-like'
else:
    print 'Angie is Bob-like'
    print 'Barb is Alice-like'

# send some messages back and forth
msg0 = a.encrypt('message 0')
print 'b decrypt: ', b.decrypt(msg0)
msg1 = b.encrypt('message 1')
print 'a decrypt: ', a.decrypt(msg1)
msg2 = a.encrypt('message 2')
msg3 = a.encrypt('message 3')
print 'b decrypt: ', b.decrypt(msg2)
msg4 = b.encrypt('message 4')
print 'a decrypt: ', a.decrypt(msg4)
msg5 = a.encrypt('message 5')
print 'b decrypt: ', b.decrypt(msg5)
print 'b decrypt: ', b.decrypt(msg3)

# save the state
a.saveState()
b.saveState()
예제 #6
0
파일: threetoe.py 프로젝트: ghtdak/pyaxo
b = Axolotl("Barb")

# initialize their states
a.initState("Barb", b.state["DHIs"], b.handshakePKey, b.state["DHRs"], verify=False)
b.initState("Angie", a.state["DHIs"], a.handshakePKey, a.state["DHRs"], verify=False)

# tell who is who
if a.mode:
    print "Angie is Alice-like"
    print "Barb is Bob-like"
else:
    print "Angie is Bob-like"
    print "Barb is Alice-like"

# Walk the three-toed Axolotl walk..
msg0 = a.encrypt("Step 1, Toe 1")
msg1 = a.encrypt("Step 1, Toe 2")
msg2 = a.encrypt("Step 1, Toe 3")
print "b decrypt: ", b.decrypt(msg0)
print "b decrypt: ", b.decrypt(msg1)
print "b decrypt: ", b.decrypt(msg2)
msg0 = b.encrypt("Step 2, Toe 1")
msg1 = b.encrypt("Step 2, Toe 2")
msg2 = b.encrypt("Step 2, Toe 3")
print "a decrypt: ", a.decrypt(msg0)
print "a decrypt: ", a.decrypt(msg1)
print "a decrypt: ", a.decrypt(msg2)
msg0 = a.encrypt("Step 3, Toe 1")
msg1 = a.encrypt("Step 3, Toe 2")
msg2 = a.encrypt("Step 3, Toe 3")
print "b decrypt: ", b.decrypt(msg0)
예제 #7
0
파일: test3.py 프로젝트: eglanz/pyaxo
# tell who is who
if tom.mode:
    print 'Tom is Alice-like'
    print 'Dick is Bob-like'
else:
    print 'Tom is Bob-like'
    print 'Dick is Alice-like'

print

# get the plaintext
with open('file.txt', 'r') as f:
    msg = f.read()

# Tom encrypts it to Dick
ciphertext = tom.encrypt(msg)

# save Dick's state prior to decrypting the message
dick.saveState()

# Dick decrypts the ciphertext
print "Dick's decryption..."
print dick.decrypt(ciphertext)

# now load Dick's state to Harry
print
print "Harry is loading Dick's state..."
harry.loadState('Dick', 'Tom')

# Harry decrypts the ciphertext
print
예제 #8
0
파일: test2.py 프로젝트: ghtdak/pyaxo
# initialize their states
a.initState('Barb', b.state['DHIs'], b.handshakePKey, b.state['DHRs'],
            verify=False)
b.initState('Angie', a.state['DHIs'], a.handshakePKey, a.state['DHRs'],
            verify=False)

# tell who is who
if a.mode:
    print 'Angie is Alice-like'
    print 'Barb is Bob-like'
else:
    print 'Angie is Bob-like'
    print 'Barb is Alice-like'

# send some messages back and forth
msg0 = a.encrypt('message 0')
msg1 = a.encrypt('message 1')
msg2 = a.encrypt('message 2')
msg3 = a.encrypt('message 3')
msg4 = a.encrypt('message 4')
msg5 = a.encrypt('message 5')
msg6 = a.encrypt('message 6')
msg7 = a.encrypt('message 7')
msg8 = a.encrypt('message 8')
msg9 = a.encrypt('message 9')
msg10 = a.encrypt('message 10')
msg11 = a.encrypt('message 11')
print 'b decrypt: ', b.decrypt(msg11)
print 'b decrypt: ', b.decrypt(msg10)
print 'b decrypt: ', b.decrypt(msg9)
print 'b decrypt: ', b.decrypt(msg8)
예제 #9
0
파일: test.py 프로젝트: eglanz/pyaxo
b = Axolotl('Barb', dbpassphrase=None)

# initialize their states
a.initState('Barb', b.state['DHIs'], b.handshakePKey, b.state['DHRs'], verify=False)
b.initState('Angie', a.state['DHIs'], a.handshakePKey, a.state['DHRs'], verify=False)

# tell who is who
if a.mode:
    print 'Angie is Alice-like'
    print 'Barb is Bob-like'
else:
    print 'Angie is Bob-like'
    print 'Barb is Alice-like'

# send some messages back and forth
msg0 = a.encrypt('message 0')
msg1 = a.encrypt('message 1')
msg2 = b.encrypt('message 2')
print 'b decrypt: ', b.decrypt(msg0)
print 'b decrypt: ', b.decrypt(msg1)
print 'a decrypt: ', a.decrypt(msg2)
msg3 = a.encrypt('message 3')
msg4 = a.encrypt('message 4')
msg5 = b.encrypt('message 5')
msg6 = a.encrypt('message 6')
msg7 = b.encrypt('message 7')
msg8 = a.encrypt('message 8')
msg9 = a.encrypt('message 9')
msg10 = a.encrypt('message 10')
msg11 = a.encrypt('message 11')
print 'b decrypt: ', b.decrypt(msg11)
예제 #10
0
파일: threetoe.py 프로젝트: eglanz/pyaxo
b = Axolotl('Barb')

# initialize their states
a.initState('Barb', b.state['DHIs'], b.handshakePKey, b.state['DHRs'], verify=False)
b.initState('Angie', a.state['DHIs'], a.handshakePKey, a.state['DHRs'], verify=False)

# tell who is who
if a.mode:
    print 'Angie is Alice-like'
    print 'Barb is Bob-like'
else:
    print 'Angie is Bob-like'
    print 'Barb is Alice-like'

# Walk the three-toed Axolotl walk..
msg0 = a.encrypt('Step 1, Toe 1')
msg1 = a.encrypt('Step 1, Toe 2')
msg2 = a.encrypt('Step 1, Toe 3')
print 'b decrypt: ', b.decrypt(msg0)
print 'b decrypt: ', b.decrypt(msg1)
print 'b decrypt: ', b.decrypt(msg2)
msg0 = b.encrypt('Step 2, Toe 1')
msg1 = b.encrypt('Step 2, Toe 2')
msg2 = b.encrypt('Step 2, Toe 3')
print 'a decrypt: ', a.decrypt(msg0)
print 'a decrypt: ', a.decrypt(msg1)
print 'a decrypt: ', a.decrypt(msg2)
msg0 = a.encrypt('Step 3, Toe 1')
msg1 = a.encrypt('Step 3, Toe 2')
msg2 = a.encrypt('Step 3, Toe 3')
print 'b decrypt: ', b.decrypt(msg0)