Skip to content Skip to sidebar Skip to footer

Using PowerShell To Decrypt A Python Encrypted String

I am using a python program to take a large string (256 bytes or more) and encrypt it using AES-CBC. This would happen on a Linux system then the encrypted data would be transferre

Solution 1:

I modified your Encrypt. Your encrypt was missing the $IV reference.

The decrypt appends the IV array and also passes it to the object.

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, $bytes.Length);
    [byte[]] $fullData = $aesManaged.IV + $encryptedData
    $aesManaged.Dispose()
    [System.Convert]::ToBase64String($fullData)
}

function Decrypt-String($key, $encryptedStringWithIV) {

    $bytes = [System.Convert]::FromBase64String($encryptedStringWithIV)
    $IV = $bytes[0..15]
    $aesManaged = Create-AesManagedObject $key $IV
    $decryptor = $aesManaged.CreateDecryptor();
    $unencryptedData = $decryptor.TransformFinalBlock($bytes, 16, $bytes.Length - 16);
    $aesManaged.Dispose()
    [System.Text.Encoding]::UTF8.GetString($unencryptedData).Trim([char]0)
}


$unencryptedString = "TextMustBe16BytesUsually"
$encryptedString = Encrypt-String $key $unencryptedString
$backToPlainText = Decrypt-String $key $encryptedString
$backToPlainText

Post a Comment for "Using PowerShell To Decrypt A Python Encrypted String"