2017-05-31 3 views
-2

Ich bin neu in Python/Django. Ich möchte die AES-Verschlüsselung verwenden. nach der Suche fand ich einige Bibliotheken wie folgt: https://gist.github.com/jeetsukumaran/1291836 aber ich konnte sie nicht verwenden, weil ich IV habe, aber das nicht IV-Code haben. Ich habe Beispielcode in C#. Kann mir jemand helfen, diesen Code in Python-Code umzuwandeln? HierPython AES verschlüsseln

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; 
    } 
    } 
} 
} 
+3

Ich wähle diese Frage als Wegthema zu schließen, weil Stack-Überlauf kein Schreiben von Code-Service ist. Wenn Sie ein bestimmtes Problem beim Konvertieren des Codes haben, aktualisieren Sie die Frage bitte. – Alasdair

+0

dieser C# -Code ist nur ein Beispiel von dem, was ich will. Ich brauche es nicht, um es auf Python zu übertragen. –

+0

können Sie http://pythonhosted.org/pycrypto/ verwenden, es hat Unterstützung für das Einrichten eigener IV. Zum Beispiel: http://pythonhosted.org/pycrypto/Crypto.Cipher.AES-module.html – valentin

Antwort

0

ist ein Beispiel für AES-Verschlüsselung bekannte pycrypto Bibliothek:

>>> 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' 
Verwandte Themen