7

誰かがPythonを使用してm2crypto aes256 CBCを使用して暗号化/復号化するコードを提供できますか

4

5 に答える 5

14

M2Crypto のドキュメントはひどいものです。OpenSSL のドキュメント (m2crypto は OpenSSL をラップしています) が役立つ場合があります。あなたの最善の策は、M2Crypto単体テストを見ることです - https://gitlab.com/m2crypto/m2crypto/blob/master/tests/test_evp.py - メソッドを探しますtest_AES()

于 2009-05-12T19:24:30.113 に答える
3

M2Crypto の次のラッパーを使用します ( 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
于 2016-06-21T12:58:51.133 に答える
3

m2secretを見てください:

対称鍵アルゴリズムを使用してデータを暗号化および復号化するための小さなユーティリティおよびモジュール。デフォルトでは、CBC を使用する 256 ビット AES (Rijndael) を使用しますが、一部のオプションは構成可能です。パスワードからキーを導出するために使用される PBKDF2 アルゴリズム。

于 2009-05-12T21:48:36.367 に答える
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()
于 2013-09-25T10:26:01.170 に答える
-1

セキュリティに関して言えば、ドキュメントを読むことに勝るものはありません。

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

時間をかけて理解し、コピーして貼り付けるのに最適なコードを作成したとしても、私が良い仕事をしたかどうかはわかりません. あまり役に立ちませんが、幸運と安全なデータをお祈りします。

于 2009-05-12T15:56:19.440 に答える