【问题标题】:Convert C# decryption to Python PyDes将 C# 解密转换为 Python PyDes
【发布时间】:2011-05-05 19:33:09
【问题描述】:

我在将代码从 C# 转换为 Python 时遇到问题。 Martijn's C# Blog 是一个出色的加密/解密程序 [附在下面],但我无法将它直接转换为 python 版本pyDes [下面的示例]

    public static string DecryptString(string Message, string Passphrase)
    {
        byte[] Results;
        System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();

        // Step 1. We hash the passphrase using MD5
        // We use the MD5 hash generator as the result is a 128 bit byte array
        // which is a valid length for the TripleDES encoder we use below

        MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
        byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));

        // Step 2. Create a new TripleDESCryptoServiceProvider object
        TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();

        // Step 3. Setup the decoder
        TDESAlgorithm.Key = TDESKey;
        TDESAlgorithm.Mode = CipherMode.ECB;
        TDESAlgorithm.Padding = PaddingMode.PKCS7;

        // Step 4. Convert the input string to a byte[]
        byte[] DataToDecrypt = Convert.FromBase64String(Message);

        // Step 5. Attempt to decrypt the string
        try
        {
            ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
            Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
        }
        finally
        {
            // Clear the TripleDes and Hashprovider services of any sensitive information
            TDESAlgorithm.Clear();
            HashProvider.Clear();
        }

        // Step 6. Return the decrypted string in UTF8 format
        return UTF8.GetString( Results );
    }

PyDES:

from pyDes import *


data = "Please encrypt my data"
k = des("DESCRYPT", ECB, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5)
d = k.encrypt(data)
print "Encrypted: %r" % d
print "Decrypted: %r" % k.decrypt(d)
assert k.decrypt(d, padmode=PAD_PKCS5) == dat

我修改了原始的 pyDes 以使用 ECB 而不是 CBC。 没有一个字符串匹配,我已经走到了尽头。 请帮忙! 谢谢

【问题讨论】:

    标签: c# python encryption tripledes


    【解决方案1】:

    没关系 --- 想通了。

    如果你在 pyDes 中改变这一行

    print "Encrypted: %r" % d
    

    print "Encrypted: %r" % d.encode('base64')
    

    那么代码完全匹配。

    要进行比较,请运行 Martijn 网站上的原始代码:[输出]

    Message: This world is round, not flat, don't believe them!
    Password: secret
    Encrypted string: pafHnI124lxzCr+93COqxfgOTan8x9oPzX4R/PDYkBnrjufk0/7mesG5tmS2AU
    Pxna3z0jY+7II=
    Decrypted string: This world is round, not flat, don't believe them!
    

    这里是 pyDes 修改后的代码——你需要用 md5Hash 填充密码

    import md5
    from pyDes import *
    
    Msg = "This world is round, not flat, don't believe them!"
    Password = "secret"
    
    m=md5.new()
    m.update(Password)
    
    k = triple_des(m.digest() , ECB,padmode=PAD_PKCS5)
    d = k.encrypt(Msg)
    
    print "Encrypted: %r" % d.encode('base64')  
    print "Decrypted: %r" % k.decrypt(d)
    assert k.decrypt(d, padmode=PAD_PKCS5) == Msg
    

    输出:

    Encrypted: 'pafHnI124lxzCr+93COqxfgOTan8x9oPzX4R/PDYkBnrjufk0/7mesG5tmS2AUPxna3z
    0jY+7II=\n'
    Decrypted: "This world is round, not flat, don't believe them!"
    

    我希望这可以帮助下一个人!!!! 谢谢 -D

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-07
      • 2019-09-21
      • 1970-01-01
      • 2014-11-03
      • 1970-01-01
      • 2019-06-06
      • 2021-02-22
      相关资源
      最近更新 更多