加密&使用PyCrypto AES 256解密 [英] Encrypt & Decrypt using PyCrypto AES 256

查看:171
本文介绍了加密&使用PyCrypto AES 256解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用接受两个参数的PyCrypto构建两个函数:消息和密钥,然后加密/解密消息。



我发现几个网络上的链接帮助我,但每个人都有缺陷:



这个在codekoala 使用os.urandom,这是PyCrypto不鼓励的。



此外,我给予功能的键不能保证具有预期的确切长度。我可以做些什么来实现这一点?



另外,有几种模式,哪一种被推荐?我不知道该用什么:/



最后,IV是什么?我可以提供不同的IV加密和解密,还是会以不同的结果返回?



以下是我迄今为止所做的:

$ b来自Crypto import的
$ b

 来自Crypto.Cipher导入的
AES
import base64

BLOCK_SIZE = 32

def加密(message,passphrase):
#passphrase必须是16,24或32字节长,我该怎么办?
IV = Random.new()。read(BLOCK_SIZE)
aes = AES.new(passphrase,AES.MODE_CFB,IV)
返回base64.b64encode(aes.encrypt(message))

def decrypt(encrypted,passphrase):
IV = Random.new()。read(BLOCK_SIZE)
aes = AES.new(passphrase,AES.MODE_CFB,IV)
return aes.decrypt(base64.b64decode(encrypted))


解决方案

这是我的实现,对我有一些修复,并增强了密钥和秘密短语与32字节和iv到16个字节的对齐:

  import base64 
import hashlib
从Crypto import从$ Cr $ C $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $

def __init __(self,key):
self.bs = 32
self.key = hashlib.sha256(key.encode())。digest()

def encrypt(self,raw):
raw = self._pad(raw)
iv = Random.new()。read(AES.block_size)
cipher = AES.new(self.key,AES.MODE_CBC,iv)
返回base64.b64encode(iv + cipher.encrypt(raw))

def decrypt(self,enc):
enc = base64.b64decode(enc)
iv = enc [:AES.block_size]
cipher = AES.new(self.key,AES.MODE_CBC,iv)
return self._unpad(cipher.decrypt(enc [AES.block_size:]))。decode('utf-8')

def _pad(self,s):
return s + self.bs - len(s)%self.bs)* chr(self.bs - len(s)%self.bs)

@staticmethod
def _unpad(s):
return s [: - ord(s [len(s)-1:])]


I'm trying to build two functions using PyCrypto that accept two parameters: the message and the key, and then encrypt/decrypt the message.

I found several links on the web to help me out, but each one of them has flaws:

This one at codekoala uses os.urandom, which is discouraged by PyCrypto.

Moreover, the key I give to the function is not guaranteed to have the exact length expected. What can I do to make that happen ?

Also, there are several modes, which one is recommended? I don't know what to use :/

Finally, what exactly is the IV? Can I provide a different IV for encrypting and decrypting, or will this return in a different result?

Here's what I've done so far:

from Crypto import Random
from Crypto.Cipher import AES
import base64

BLOCK_SIZE=32

def encrypt(message, passphrase):
    # passphrase MUST be 16, 24 or 32 bytes long, how can I do that ?
    IV = Random.new().read(BLOCK_SIZE)
    aes = AES.new(passphrase, AES.MODE_CFB, IV)
    return base64.b64encode(aes.encrypt(message))

def decrypt(encrypted, passphrase):
    IV = Random.new().read(BLOCK_SIZE)
    aes = AES.new(passphrase, AES.MODE_CFB, IV)
    return aes.decrypt(base64.b64decode(encrypted))

解决方案

Here is my implementation and works for me with some fixes and enhances the alignment of the key and secret phrase with 32 bytes and iv to 16 bytes:

import base64
import hashlib
from Crypto import Random
from Crypto.Cipher import AES

class AESCipher(object):

    def __init__(self, key): 
        self.bs = 32
        self.key = hashlib.sha256(key.encode()).digest()

    def encrypt(self, raw):
        raw = self._pad(raw)
        iv = Random.new().read(AES.block_size)
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        return base64.b64encode(iv + cipher.encrypt(raw))

    def decrypt(self, enc):
        enc = base64.b64decode(enc)
        iv = enc[:AES.block_size]
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        return self._unpad(cipher.decrypt(enc[AES.block_size:])).decode('utf-8')

    def _pad(self, s):
        return s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs)

    @staticmethod
    def _unpad(s):
        return s[:-ord(s[len(s)-1:])]

这篇关于加密&使用PyCrypto AES 256解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆