【问题标题】:C# RijndaelManaged to Python equivalentC# RijndaelManaged 到 Python 等价物
【发布时间】:2015-09-07 12:44:18
【问题描述】:

我有以下 C# 代码(代码是继承的,无法编译)。这用于解密和解压缩保存的文件。

using System.Security.Cryptography;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;

//Not the real key but same amount of chars
private const string kEncyptionKey = "01234567";

public string DecryptAndDecompressText (string strFileName)
{
    // Decryption ///
    FileStream fin = null;
    try
    {
        fin = new FileStream(strFileName, FileMode.Open, FileAccess.Read);
    }
    catch (System.IO.FileNotFoundException)
    {
        return "";
    }

    MemoryStream    memoryToDecompress =  new MemoryStream();

    UnicodeEncoding UE       = new UnicodeEncoding();
    RijndaelManaged RMCrypto = new RijndaelManaged();

    // This is the encryption key for our file 
    byte[] key = UE.GetBytes(kEncyptionKey);

    // Decrypt the data to a stream
    CryptoStream cs = new CryptoStream( memoryToDecompress, 
                                        RMCrypto.CreateDecryptor(key, key),
                                        CryptoStreamMode.Write);
    byte [] fileBuffer = new byte[fin.Length];
    fin.Read(fileBuffer, 0, fileBuffer.Length);
    cs.Write(fileBuffer, 0, fileBuffer.Length);

    fin.Close();

    // Reset the index of the Memory Stream
    memoryToDecompress.Position = 0;

    // Let the GC clean this up, we still need the memory stream
    //cs.Close();   


    // Decompress the File
    ZipInputStream s;
    s = new ZipInputStream(memoryToDecompress);

    ZipEntry theEntry;
    try
    {
        theEntry = s.GetNextEntry();
    }
    catch (System.Exception)
    {
        // Could not open the file...
        return "";
    }
}

我正在尝试创建一个 python 程序来做同样的事情。这就是我所拥有的:

from Crypto.Cipher import AES

KEY = '01234567'.encode('utf-16be')

_f = open('<file>', 'r')

 _content = _f.read()

_cipher = AES.new(KEY, AES.MODE_CBC, KEY)

_dcontent = _cipher.decrypt(_content)

with open('extract.zip', 'w') as newfile:
    newfile.write(_dcontent)

_f.close()

我将结果写入磁盘,因为我希望它是一个 zip 文件(其中包含一个文件)。但是我无法使用存档管理器打开文件。

欢迎提出任何建议!

【问题讨论】:

    标签: c# python aes rijndael


    【解决方案1】:

    您必须使用相同的键System.Text.UnicodeEncoding 是 UTF-16le 编码,在 python 中也有类似的编码:

    KEY = '01234567'.encode('utf-16le')
    

    如果您使用的是 Windows,则必须以 二进制模式 读写文件:

    _f = open('<file>', 'rb')
    ...
    open('extract.zip', 'wb')
    

    【讨论】:

    • 你能说得更具体点吗?你有任何错误吗?如果没有,是否写入输出文件?如果是这样,输出文件的大小是否与预期的输出文件相同?如果是这样,除了前 16 个字节之外,预期文件是否与输出文件相同?
    • 我没有收到任何错误。一个文件被写入。输入和输出文件大小相同。每个的前 32 个字节是不同的。 0e50 74aa 793e 0f10 967d 679e df3e ba8e 97c3 6092 4aa4 d45e 366c 437e 8abe c13d vs 4b50 0403 0014 0000 0008 b67b 40be 4d05 bb83 ae92 0000 2331 0005 0008 0000 50531(使用 hexdump 获得)
    • 如果前 16 个字节与您预期的不同,那么问题将是一个错误的 IV,但由于这是 32 个字节,我真的不知道它可能是什么。另外,我不确定 "input and output files" 中的 "input" 应该是什么。
    【解决方案2】:

    您应该使用proper zip file library。我猜这是您的 write 语句失败的特定格式。使用这个库应该可以避免这些缺点。 open 函数可以将密码作为可选的,以防它受到保护。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-08
      • 2018-12-07
      • 2011-10-29
      • 2015-02-24
      • 1970-01-01
      • 1970-01-01
      • 2019-06-03
      • 2010-10-05
      相关资源
      最近更新 更多