python AES加密

时间:2017-05-31 12:39:41

标签: python django aes

我是python / django的新手。我想使用AES加密。 搜索后我发现了一些像这样的库:https://gist.github.com/jeetsukumaran/1291836 但我不能使用它们,因为我有IV但这没有IV代码。 我在c#中有示例代码。任何人都可以帮助我将此代码转换为python代码?

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace AESUtility {
 public class AES {
  string AES_Key = string.Empty;
  string AES_IV = string.Empty;
  public AES(string AES_Key, string AES_IV) {
   this.AES_Key = AES_Key;
   this.AES_IV = AES_IV;
  }
  public bool Encrypt(String Input, out string encryptedString) {
   try {
    var aes = new RijndaelManaged();
    aes.KeySize = 256;
    aes.BlockSize = 256;
    aes.Padding = PaddingMode.PKCS7;
    aes.Key = Convert.FromBase64String(this.AES_Key);
    aes.IV = Convert.FromBase64String(this.AES_IV);
    var encrypt = aes.CreateEncryptor(aes.Key, aes.IV);
    byte[] xBuff = null;
    using(var ms = new MemoryStream()) {
     using(var cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write)) {
      byte[] xXml = Encoding.UTF8.GetBytes(Input);
      cs.Write(xXml, 0, xXml.Length);
     }
     xBuff = ms.ToArray();
    }
    encryptedString = Convert.ToBase64String(xBuff);
    return true;
   } catch (Exception ex) {
    encryptedString = string.Empty;
    return false;
   }
  }
  public bool Decrypt(String Input, out string decodedString) {
   try {
    RijndaelManaged aes = new RijndaelManaged();
    aes.KeySize = 256;
    aes.BlockSize = 256;
    aes.Mode = CipherMode.CBC;
    aes.Padding = PaddingMode.PKCS7;
    aes.Key = Convert.FromBase64String(this.AES_Key);
    aes.IV = Convert.FromBase64String(this.AES_IV);
    var decrypt = aes.CreateDecryptor();
    byte[] xBuff = null;
    using(var ms = new MemoryStream()) {
     using(var cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write)) {
      byte[] xXml = Convert.FromBase64String(Input);
      cs.Write(xXml, 0, xXml.Length);
     }
     xBuff = ms.ToArray();
    }
    decodedString = Encoding.UTF8.GetString(xBuff);
    return true;
   } catch (Exception ex) {
    decodedString = string.Empty;
    return false;
   }
  }
 }
}

2 个答案:

答案 0 :(得分:2)

OFB模式下的AES 256:

from Crypto.Cipher import AES
from Crypto.Random import new as Random
from hashlib import sha256
from base64 import b64encode,b64decode

class AESCipher:
  def __init__(self,data,key):
    self.block_size = 16
    self.data = data
    self.key = sha256(key.encode()).digest()[:32]
    self.pad = lambda s: s + (self.block_size - len(s) % self.block_size) * chr (self.block_size - len(s) % self.block_size)
    self.unpad = lambda s: s[:-ord(s[len(s) - 1:])]

  def encrypt(self):
    plain_text = self.pad(self.data)
    iv = Random().read(AES.block_size)
    cipher = AES.new(self.key,AES.MODE_OFB,iv)
    return b64encode(iv + cipher.encrypt(plain_text.encode())).decode()

  def decrypt(self):
    cipher_text = b64decode(self.data.encode())
    iv = cipher_text[:self.block_size]
    cipher = AES.new(self.key,AES.MODE_OFB,iv)
    return self.unpad(cipher.decrypt(cipher_text[self.block_size:])).decode()

用法:

AESCipher("data to encrypt","key to use").encrypt()

AESCipher("encrypted data","key to use").decrypt()

答案 1 :(得分:1)

以下是使用众所周知的pycrypto库进行AES加密的示例:

>>> from Crypto.Cipher import AES
>>> obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
>>> message = "The answer is no"
>>> ciphertext = obj.encrypt(message)
>>> ciphertext
'\xd6\x83\x8dd!VT\x92\xaa`A\x05\xe0\x9b\x8b\xf1'
>>> obj2 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
>>> obj2.decrypt(ciphertext)
'The answer is no'