10

I've found this exactly same question. But PyCrypto doesn't install both on python 3.6.5 and 3.7.0.

So, I implement some kind of Gronsfeld-like cipher. I know, that's awful, but I can simply encrypt and derypt string with password

def encrypt(string, password):
    int_list = []
    password_len = len(password)
    for cnt, sym in enumerate(string):
        password_sym = password[cnt % password_len]
        int_list.append(ord(sym)-ord(password_sym))
    return int_list

# got some list which contain mine key to Todoist api, yes, this can be bruteforced, but same as any other API key
>>> [-20, -20, -50, -14, -61, -54, 2, 0, 32, 27, -51, -21, -54, -53, 4, 3, 29, -14, -51, 29, -10, -6, 1, 4, 28,
       29, -55, -17, -59, -42, 2, 50, -13, -14, -52, -15, -56, -59, -44, 4]

def decrypt(int_list, password):
    output_string = ""
    password_len = len(password)
    for cnt, numb in enumerate(int_list):
        password_sym = password[cnt % password_len]
        output_string += chr(numb+ord(password_sym))
    return output_string

So, how to do it properly?

Egor Egorov
  • 313
  • 1
  • 4
  • 19
  • I tested your code, it works. What exactly is your problem? – Sharku Aug 21 '18 at 09:32
  • Did you try installing PyCrypto? It works fine on 3.6. – Daniel Roseman Aug 21 '18 at 09:32
  • @Sharku I don't think it's secure :/ – Egor Egorov Aug 21 '18 at 10:32
  • @DanielRoseman `Command ""c:\program files (x86)\python36-32\python.exe" -u -c "import setuptools, tokenize;__file__='C:\\Users\\EEgorov.NWX\\AppData\\Local\\Temp\\pip-install-6hs9ukdo\\pycrypto\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\EEgorov.NWX\AppData\Local\Temp\pip-record-m0z81zyx\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:\Users\EEgorov.NWX\AppData\Local\Temp\pip-install-6hs9ukdo\pycrypto\` – Egor Egorov Aug 21 '18 at 10:33
  • https://stackoverflow.com/questions/11405549/how-do-i-install-pycrypto-on-windows – snakecharmerb Aug 22 '18 at 20:23

1 Answers1

21

Cryptography is an actively developed library that provides cryptographic recipes and primitives. It supports Python 2.6-2.7, Python 3.3+ and PyPy.

Installation

$ pip install cryptography

Example code using high level symmetric encryption recipe:

from cryptography.fernet import Fernet
key = Fernet.generate_key()
cipher_suite = Fernet(key)
cipher_text = cipher_suite.encrypt(b"A really secret message. Not for prying eyes.")
plain_text = cipher_suite.decrypt(cipher_text)
Sathiyakugan
  • 674
  • 7
  • 20
  • If key not proper it throws an exception, so it get bruteforcing to new speed, not needed to check even API key online. Don't think this will be improve security. – Egor Egorov Aug 21 '18 at 10:44
  • @EgorEgorov I couldn't get u clearly! could you give me an exampe code where It's generating error? You should give the key of length 44 otherwise it will result in " Incorrect padding". A random key is generated by the Fernet.generate_key(). If you want you can generate a key. make sure you maintain this format ---> key = b'Yzx79GUul49KKI11kQ-42pjySAMDCbKywCrDravrbkg=' – Sathiyakugan Aug 21 '18 at 11:16
  • code in question, it iterates over password symbols. So, if code get wrong password, it generate string with just wrong unicode symbols, not throw exception. – Egor Egorov Aug 22 '18 at 09:08
  • @EgorEgorov any Decrypt function won't say it 's correct or not but the function will take the key and the ciphertext and return the output! here the key length is 42. so in order to a brute-force attack, you need to use (there can be 96 characters) 96*96*............*96 =96^42 combination. so mathematically it needs a lot of time to brute force and your life will be over. – Sathiyakugan Aug 22 '18 at 16:33
  • How are we supposed to store the key generated by Fernet for future encryption/decryption? – default123 Sep 21 '20 at 18:12
  • @default123 You can write the generated key to a file via `file.write(key.decode())` and then read it back from the file via `key = file.read().encode()`. – Christopher Rice May 08 '22 at 03:34