Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trouble decrypting messages (Example script included) #81

Open
bitnom opened this issue May 16, 2023 · 0 comments
Open

Trouble decrypting messages (Example script included) #81

bitnom opened this issue May 16, 2023 · 0 comments

Comments

@bitnom
Copy link

bitnom commented May 16, 2023

I made a fork/branch to get the library working with Python 3.10. It works but as there is no documentation, I think I'm doing something wrong in my usage.

Installing my fork/branch:

pip install "git+https://github.com/bitnom/pure-python-otr.git@python3"

My example:

import potr
from potr.compatcrypto import DSAKey

# Custom Context class
class MyContext(potr.context.Context):
    def __init__(self, account, peername):
        super(MyContext, self).__init__(account, peername)

    def getPolicy(self, key):
        return self.user.getPolicy(key)

    def inject(self, msg, appdata=None):
        print(f"{self.user.name} -> {self.peer}: {msg}")
        if appdata:
            try:
                decrypted_msg, tlvs = appdata.receiveMessage(msg)
                if decrypted_msg:
                    print(f"{appdata.user.name} received decrypted message: {decrypted_msg}")
            except potr.context.NotEncryptedError:
                pass
            except potr.context.NotOTRMessage:
                pass

    def handleQuery(self, message, appdata=None):
        if 2 in message.versions and self.getPolicy('ALLOW_V2'):
            self.authStartV2(appdata=appdata)
        elif 1 in message.versions and self.getPolicy('ALLOW_V1'):
            self.authStartV1(appdata=appdata)
        else:
            print(f"{self.user.name} received a non-OTR message: {message.msg.decode('utf-8')}")

# Custom Account class with required methods
class MyAccount(potr.context.Account):
    contextclass = MyContext

    def __init__(self, *args, **kwargs):
        super(MyAccount, self).__init__(*args, **kwargs)
        self.policy = {
            'ALLOW_V2': True,
            'ALLOW_V1': False,
            'REQUIRE_ENCRYPTION': True,
            'SEND_TAG': True,
        }

    def loadPrivkey(self):
        return DSAKey.generate()

    def savePrivkey(self):
        pass

    def saveTrusts(self):
        pass

    def getPolicy(self, key):
        return self.policy.get(key, False)

# Initialize accounts for Alice and Bob
alice_account = MyAccount('alice@example.com', 'XMPP', maxMessageSize=1024)
bob_account = MyAccount('bob@example.com', 'XMPP', maxMessageSize=1024)

# Initialize contexts for Alice and Bob
alice_context = alice_account.getContext('bob@example.com')
bob_context = bob_account.getContext('alice@example.com')

# Alice sends an OTR query message to Bob
alice_query = alice_account.getDefaultQueryMessage(alice_context.getPolicy)
alice_context.inject(alice_query, appdata=bob_context)

# Wait for the AKE to complete
import time
time.sleep(5)

# Alice and Bob can now send encrypted messages to each other
encrypted_msg = alice_context.sendMessage(potr.context.FRAGMENT_SEND_ALL, b"Hello, Bob!", appdata=bob_context)
decrypted_msg, tlvs = bob_context.receiveMessage(encrypted_msg)
if decrypted_msg:
    print(f"Bob received decrypted message: {decrypted_msg}")

encrypted_msg = bob_context.sendMessage(potr.context.FRAGMENT_SEND_ALL, b"Hello, Alice!", appdata=alice_context)
decrypted_msg, tlvs = alice_context.receiveMessage(encrypted_msg)
if decrypted_msg:
    print(f"Alice received decrypted message: {decrypted_msg}")


# Alice and Bob can disconnect the OTR session
alice_context.disconnect()
bob_context.disconnect()

which prints:

alice@example.com -> bob@example.com: b'?OTRv2?\nI would like to start an Off-the-Record private conversation. However, you do not have a plugin to support that.\nSee https://otr.cypherpunks.ca/ for more information.'
bob@example.com -> alice@example.com: b'?OTR:AAICAAAAxKgjXJiCdN3p76/REwybuA3kWoZa5QOxuYsaZbLqO+pIfEDSkD1gUWGerdyj6ikELK0QArEcrhfQhvuw7OsfrCFujruX9AoNCglTIG5LLcU5skElWOk+DZQ1dunSMwn0E9BVGFCFfCtvXkjyESMwS2rU0LIR4topbdp9HLRNnJizNKWlpwO4Q1AxNqlF8OY9i265tqHj29EetAtcR2zx3P3dEdM3+aiJcZxoJ8h4YyKASmtD1QDHWR3D6JmPK3JQSN90DlkAAAAg1E8Re3oNsmtK6WRQtl3AMd6nPmi24q/F84ktgmIP90M=.'
bob@example.com -> alice@example.com: b'?OTR:AAICAAAAxIr+WZ5U1YFXf5KZ/IF+Exw5S0ENvJYEEq22rh2iQNldbB3QFWHmwr2PiymJ5kWeuj4XXDFwRM9Y08dzrGqq238aDeCMkGP1dIZ3O9s7Q/k3Pt9+pN9Csb3wbu9xUVFt3blSI5VoR4nVcAJrwlFcX4QpH6OWhmqqm6DDt/SoqXBeVGoddbhTGVQLEMcsjelXwmYeKjqxb3kR3ihgCli8b4/69rnK1/UiX8V5Wqp2FKd80uYH+XSH2vtFC9/4qWbdcjYBkC8AAAAgEagK0yJzkHwg7DQKF3nSL8IPGzD7YkZ5NwcIn+qk2oI=.'
alice@example.com -> bob@example.com: b'?OTR:AAICAAAAxGvDVCQvO9BDPzt916H400KtSjrFvic8qhkMyykSkCMHMNJL9yVUEsuqQqxSqu4CsqKmrwQekfGYMxmQF+u+ssuJJnJ4omXUGk1tjFvOgrG2677/rYSY4t7Cco96Ram9j3/DtKWrpc9V+jn7f0R8Gnf+LIrQtxnSOXAvTCD4BEGX25f72d5RGlGJZkb1ZsmV8j7Ov662P+G9PpEg+4VZhGXN1Y/YFmDAwuk4Sw4CK74Ge9Q9KEy+KWRKu3lowSEi96iBYrkAAAAgFnqzNfHwNx5UmHSXx7Fm0H6cNEUTKoZ9ioLLjKUIask=.'

Why is decrypted_msg always None? I must be missing something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant