M2Crypto 使用 AES256 加密/解密

发布于 2024-07-21 07:11:38 字数 57 浏览 5 评论 0原文

有人可以向我提供使用 Python 使用 m2crypto aes256 CBC 加密/解密的代码吗

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小糖芽 2024-07-28 07:11:38

M2Crypto 的文档很糟糕。 有时 OpenSSL 文档(m2crypto 包装 OpenSSL)可以提供帮助。 最好的选择是查看 M2Crypto 单元测试 - https:/ /gitlab.com/m2crypto/m2crypto/blob/master/tests/test_evp.py - 查找 test_AES() 方法。

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.

却一份温柔 2024-07-28 07:11:38

看看 m2secret

小型实用程序和模块
使用加密和解密数据
对称密钥算法。 默认情况下
使用 CBC 的 256 位 AES (Rijndael),
但有些选项是可配置的。
用于导出密钥的PBKDF2算法
来自密码。

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.

挖个坑埋了你 2024-07-28 07:11:38

我在 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

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
温柔戏命师 2024-07-28 07:11:38
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()
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()
就此别过 2024-07-28 07:11:38

当谈到安全性时,没有什么比阅读文档更好的了。

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

即使我花时间理解并制作完美的代码供您复制和粘贴,您不会知道我做得好不好。 我知道不是很有帮助,但我祝你好运并保证数据安全。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文