小编Mic*_*ams的帖子

PowerShell和Python中的AES加密

我的目标是能够在PowerShell中对字符串进行AES加密,将其发送到可用python的UNIX系统,并将字符串解密回纯文本.我也希望能够做到这一点.我不是加密人或PowerShell/python程序员,但这是我迄今为止用代码做的事情:

function Create-AesManagedObject($key, $IV) {
    $aesManaged = New-Object "System.Security.Cryptography.AesManaged"
    $aesManaged.Mode = [System.Security.Cryptography.CipherMode]::CBC
    $aesManaged.Padding = [System.Security.Cryptography.PaddingMode]::Zeros
    $aesManaged.BlockSize = 128
    $aesManaged.KeySize = 256
    if ($IV) {
        if ($IV.getType().Name -eq "String") {
            $aesManaged.IV = [System.Convert]::FromBase64String($IV)
        }
        else {
            $aesManaged.IV = $IV
        }
    }
    if ($key) {
        if ($key.getType().Name -eq "String") {
            $aesManaged.Key = [System.Convert]::FromBase64String($key)
        }
        else {
            $aesManaged.Key = $key
        }
    }
    $aesManaged
}

function Encrypt-String($key, $unencryptedString) {
    $bytes = [System.Text.Encoding]::UTF8.GetBytes($unencryptedString)
    $aesManaged = Create-AesManagedObject $key $IV
    $encryptor = $aesManaged.CreateEncryptor()
    $encryptedData = $encryptor.TransformFinalBlock($bytes, 0, …
Run Code Online (Sandbox Code Playgroud)

python powershell cryptography aes pycrypto

6
推荐指数
1
解决办法
3733
查看次数

标签 统计

aes ×1

cryptography ×1

powershell ×1

pycrypto ×1

python ×1