2009-05-12 33 views
6

Ai đó có thể cung cấp cho tôi mã để mã hóa/giải mã sử dụng m2crypto AES256 CBC sử dụng PythonM2Crypto Encrypt/Decrypt sử dụng AES256

+0

Bạn có thể có một cái nhìn một [tại bài này] [1]. [1]: http://stackoverflow.com/questions/5003626/problem-with-m2cryptos-aes – ohe

Trả lời

-1

Khi nói đến an ninh không có gì nhịp đập đọc tài liệu.

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

Thậm chí nếu tôi dành thời gian để hiểu và làm cho đoạn code hoàn hảo để bạn có thể sao chép và dán, bạn sẽ không có ý tưởng nếu tôi đã làm một công việc tốt hay không. Không phải là rất hữu ích tôi biết, nhưng tôi muốn bạn may mắn và dữ liệu an toàn.

+0

Liên kết đó không còn giá trị. –

13

Tài liệu của M2Crypto thật khủng khiếp. Đôi khi tài liệu OpenSSL (m2crypto wraps OpenSSL) có thể giúp bạn. Đặt cược tốt nhất của bạn là xem xét các bài kiểm tra đơn vị M2Crypto - http://svn.osafoundation.org/m2crypto/trunk/tests/test_evp.py - tìm phương pháp test_AES().

+0

+1 ... joe, câu trả lời này được nhiều người đánh giá cao trong khi tôi đang làm việc cho một dự án thú cưng. Tôi đã bắt đầu nghĩ rằng tôi đã được hạt khi nhìn vào các tài liệu M2Crypto và, um, thưa thớt nhận xét epydoc API; chắc chắn, tôi đã thiếu một cái gì đó hiển nhiên! Cảm ơn vì đã phục hồi đức tin của tôi. –

2

Hãy xem m2secret:

tiện ích nhỏ và mô-đun cho mã hóa và giải mã dữ liệu sử dụng thuật toán đối xứng-key. Theo mặc định, sử dụng AES 256 bit (Rijndael) bằng CBC, nhưng một số tùy chọn có thể định cấu hình được. Thuật toán PBKDF2 được sử dụng để lấy được khóa từ mật khẩu.

1
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() 
1

tôi sử dụng wrapper sau khoảng M2Crypto (mượn từ 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 
Các vấn đề liên quan