M2Crypto Encrypt/Decrypt using AES256
Asked Answered
S

5

7

Can someone provide me code to encrypt / decrypt using m2crypto aes256 CBC using Python

Siccative answered 12/5, 2009 at 11:47 Comment(1)
You can have a look a [at this post][1]. [1]: https://mcmap.net/q/1476015/-problem-with-m2crypto-39-s-aesCryan
D
14

M2Crypto's documentation is terrible. Sometimes the OpenSSL documentation (m2crypto wraps OpenSSL) can help. Your best bet is to look at the M2Crypto unit tests -- https://gitlab.com/m2crypto/m2crypto/blob/master/tests/test_evp.py -- look for the test_AES() method.

Dogeatdog answered 12/5, 2009 at 19:24 Comment(1)
+1... joe, this answer was much appreciated today while I was working on a pet project. I was beginning to think I was nuts when looking at the M2Crypto docs and, um, sparsely commented epydoc API; surely, I was missing something obvious! Thanks for restoring my faith.Treadwell
G
3

Take a look at m2secret:

Small utility and module for encrypting and decrypting data using symmetric-key algorithms. By default uses 256-bit AES (Rijndael) using CBC, but some options are configurable. PBKDF2 algorithm used to derive key from password.

Godderd answered 12/5, 2009 at 21:48 Comment(0)
P
3

I use following wrapper around M2Crypto (borrowed from cryptography.io):

import os
import base64
import M2Crypto


class SymmetricEncryption(object):

    @staticmethod
    def generate_key():
        return base64.b64encode(os.urandom(48))

    def __init__(self, key):
        key = base64.b64decode(key)
        self.iv = key[:16]
        self.key = key[16:]

    def encrypt(self, plaintext):
        ENCRYPT = 1
        cipher = M2Crypto.EVP.Cipher(alg='aes_256_cbc', key=self.key, iv=self.iv, op=ENCRYPT)
        ciphertext = cipher.update(plaintext) + cipher.final()
        return base64.b64encode(ciphertext)

    def decrypt(self, cyphertext):
        DECRYPT = 0
        cipher = M2Crypto.EVP.Cipher(alg='aes_256_cbc', key=self.key, iv=self.iv, op=DECRYPT)
        plaintext = cipher.update(base64.b64decode(cyphertext)) + cipher.final()
        return plaintext
Porky answered 21/6, 2016 at 12:58 Comment(0)
B
2
def encrypt_file(key, in_filename, out_filename,iv):
    cipher=M2Crypto.EVP.Cipher('aes_256_cfb',key,iv, op=1)
    with open(in_filename, 'rb') as infile:
        with open(out_filename, 'wb') as outfile:
          outfile.write(b)
          while True:
            buf = infile.read(1024)
            if not buf:
                break
            outfile.write(cipher.update(buf))

          outfile.write( cipher.final() )  
          outfile.close()
        infile.close()

def decrypt_file(key, in_filename, out_filename,iv):
    cipher = M2Crypto.EVP.Cipher("aes_256_cfb",key , iv, op = 0)
    with open(in_filename, 'rb') as infile: 
        with open(out_filename, 'wb') as outfile:
          while True:
            buf = infile.read(1024)
            if not buf:
                break
            try:
                outfile.write(cipher.update(buf))
            except:
                print "here"
          outfile.write( cipher.final() )  
          outfile.close()
        infile.close()
Bydgoszcz answered 25/9, 2013 at 10:26 Comment(0)
I
-1

When it comes to security nothing beats reading the documentation.

http://chandlerproject.org/bin/view/Projects/MeTooCrypto

Even if I took the time to understand and make the perfect code for you to copy and paste, you would have no idea if I did a good job or not. Not very helpful I know, but I wish you luck and secure data.

Islaen answered 12/5, 2009 at 15:56 Comment(1)
That link is no longer valid.Put

© 2022 - 2024 — McMap. All rights reserved.