0

pycryptodome の KDF モジュールを使用して、Python で PowerShell の PasswordDeriveBytes メソッドを再作成しようとしています。

私が現在渡せない主なことは、PowerShell から取得した正しい DerivedKey の結果を Python で取得することです。

パラメーター:

password = '1234567890'
salt = '0987654321'
length = 16
iterations = 2

サンプル PowerShell コード:

$AsciiEncoder = New-Object System.Text.ASCIIEncoding;
$DerivedPass = New-Object System.Security.Cryptography.PasswordDeriveBytes('1234567890', $AsciiEncoder.GetBytes('0987654321'), "SHA1", 2);
[Byte[]] $KeyBytes = $DerivedPass.GetBytes(16);
-join($KeyBytes | ForEach { $_.ToString("X2")});

結果:

C937511EBDBE12C7A0FCC8D6CB42BEDC 

*注: 上記の PowerShell コードは、https://github.com/PowerShellMafia/PowerSploit/blob/master/ScriptModification/Out-EncryptedScript.ps1から修正されています。以下の Python コードの機能を再実装しようとしています。

パイソンコード:

from Crypto.Protocol import KDF
from Crypto.Hash import SHA1

def password_derive_key(password, salt, length, count):
    return KDF.PBKDF2(password, salt, length, count)

def main():
    derivedKey = password_derive_key('1234567890', '0987654321', 16, 2)
    print derivedKey.encode('hex')

if __name__ == "__main__":
    main()

結果:

5a3f103f5dc4558f8dca5d5b145ed0b4

私が考えることができる唯一のことは、PasswordDeriveBytes の「.GetBytes」メソッドが pycryptodome の KDF モジュールとは異なる結果を生成していることです。

どんな助けでも大歓迎です。

4

1 に答える 1