Chaosthebot / Chaos

A social coding experiment that updates its own code democratically.
http://chaosthebot.com
MIT License
2.44k stars 210 forks source link

Problems with encryption #541

Closed eamanu closed 7 years ago

eamanu commented 7 years ago

I am working in #443 and I need encrypt the tokens.

I can encrypt correctly but when I decrypt it raise this message

  File "chaos.py", line 160, in <module>
    main()
  File "chaos.py", line 78, in main
    api_twitter = ta.API_TWITTER(settings.TWITTER_API_KEYS_FILE)
  File "/root/workspace/Chaos/twitter_api/__init__.py", line 10, in __init__
    self.__twitter_keys = misc.GetKeys(path)
  File "/root/workspace/Chaos/twitter_api/misc.py", line 17, in GetKeys
    consumer_key = decrypt(key)
  File "/root/workspace/Chaos/encryption.py", line 41, in decrypt
    label=None
  File "/root/.virtualenvs/chaos/lib/python3.6/site-packages/cryptography/hazmat/backends/openssl/rsa.py", line 388, in decrypt
    return _enc_dec_rsa(self._backend, self, ciphertext, padding)
  File "/root/.virtualenvs/chaos/lib/python3.6/site-packages/cryptography/hazmat/backends/openssl/rsa.py", line 70, in _enc_dec_rsa
    return _enc_dec_rsa_pkey_ctx(backend, key, data, padding_enum, padding)
  File "/root/.virtualenvs/chaos/lib/python3.6/site-packages/cryptography/hazmat/backends/openssl/rsa.py", line 112, in _enc_dec_rsa_pkey_ctx
    _handle_rsa_enc_dec_error(backend, key)
  File "/root/.virtualenvs/chaos/lib/python3.6/site-packages/cryptography/hazmat/backends/openssl/rsa.py", line 142, in _handle_rsa_enc_dec_error
    raise ValueError("Decryption failed.")
PlasmaPower commented 7 years ago

Are you sure you're doing the encryption correctly? How are you encrypting the tokens?

eamanu commented 7 years ago

I used this code to encrypt:

from encryption import decrypt
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives.serialization import load_pem_public_key
pkey = load_pem_public_key(open("server/pubkey.txt", "rb").read(), default_backend())

# defines the tokens and keys
consumer_key = b'1234'
consumer_secret = b'1234'
access_token = b'2134'
access_secret = b'12345'

def encryp(key):
    encrypted = pkey.encrypt(key, padding.OAEP(
        padding.MGF1(hashes.SHA1()),
        hashes.SHA1(),
        None
    ))
    return encrypted

f = open("twitter_keys.secret/consumer_key.secret", "wb")
f.write(encryp(consumer_key))
f.close()

f = open("twitter_keys.secret/consumer_secret.secret", "wb")
f.write(encryp(consumer_secret))
f.close()

f = open("twitter_keys.secret/access_token.secret", "wb")
f.write(encryp(access_token))
f.close()

f = open("twitter_keys.secret/access_secret.secret", "wb")
f.write(encryp(access_secret))
f.close()

And then to read the keys I used this:

PATH = 'twitter_keys.secret/'
    l_files = ['consumer_key', 'consumer_secret', 'access_token', 'access_secret']

    for k in l_files:
        f = open(PATH + k + ".secret", 'rb')
        key = f.read()
        if (k == 'consumer_key'):
            consumer_key = decrypt(key)
        if (k == 'consumer_secret'):
            consumer_secret = decrypt(key)
        if (k == 'access_token'):
            access_token = decrypt(key)
        if (k == 'access_secret'):
            access_secret = decrypt(key)
        f.close()

If I run in the console, work ok. But when I run with docker: ./run.sh I have errors

PlasmaPower commented 7 years ago

If I run in the console, work ok. But when I run with docker: ./run.sh I have errors

Are you sure the key is the same for both of them?

eamanu commented 7 years ago

Yes. Is the same code .

PlasmaPower commented 7 years ago

The same code, or the same key? The key is randomly generated, so it won't be the same in docker by default, even if the code is the same. Copy /etc/privkey from your machine into the docker container.

eamanu commented 7 years ago

I am using the same public key to encrypt my twitters keys. I encrypt the twtitter keys with encryp(key). And then decrypt with decrypt(key)

How do yo do to encrypt yours (for example, twitter) keys and then upload to the github?

@phil-r Do you know about the encrypt?