3

コードをC#からPythonに変換するのに問題があります。MartijnのC#ブログは、暗号化/復号化のための優れたプログラムです[以下に同封]が、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を変更して、CBCの代わりにECBを使用しました。どの弦も一致せず、私はロープの端にいます。助けてください!ありがとう

4

1 に答える 1

10

気にしないでください---それを理解しました。

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!"

これが次の人に役立つことを願っています!!!! ありがとう

于 2011-05-05T20:13:22.717 に答える