Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell“;填充无效,无法删除“;错误_Powershell_Encryption - Fatal编程技术网

Powershell“;填充无效,无法删除“;错误

Powershell“;填充无效,无法删除“;错误,powershell,encryption,Powershell,Encryption,我正在尝试解密Powershell中的字符串,但收到此错误。可能有什么问题?此异常可能代表许多不同的情况,并且并非所有情况都与填充有关,因此我尝试对可能发生这种情况的所有不同情况进行分类 如果您知道引发此填充异常的另一种情况,请添加它 首先,这是一个按预期工作的加密/解密示例 $testData = "Hi there! This is a test of a string during encryption" $enc = [system.Text.Encoding]::UTF8 $data

我正在尝试解密Powershell中的字符串,但收到此错误。可能有什么问题?

此异常可能代表许多不同的情况,并且并非所有情况都与填充有关,因此我尝试对可能发生这种情况的所有不同情况进行分类

如果您知道引发此填充异常的另一种情况,请添加它

首先,这是一个按预期工作的加密/解密示例

$testData = "Hi there! This is a test of a string during encryption"
$enc = [system.Text.Encoding]::UTF8
$data = $enc.getBytes($testData)

# Encrypt some data

$encryptAlgorithm = [System.Security.Cryptography.SymmetricAlgorithm] (New-Object System.Security.Cryptography.AesCryptoServiceProvider)

$encryptAlgorithm.Mode = [System.Security.Cryptography.CipherMode]::CBC
$encryptAlgorithm.Padding = [System.Security.Cryptography.PaddingMode]::PKCS7
$encryptAlgorithm.KeySize = 128
$encryptAlgorithm.BlockSize = 128
$encryptAlgorithm.Key = @(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
$encryptAlgorithm.IV = @(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)


$encryptor = [System.Security.Cryptography.ICryptoTransform]$encryptAlgorithm.CreateEncryptor()
$encryptorMemoryStream = new-Object IO.MemoryStream
$encryptorCryptoStream = new-Object Security.Cryptography.CryptoStream $encryptorMemoryStream,$encryptor,"Write"
$encryptorCryptoStream.Write($data, 0, $data.Length)
$encryptorCryptoStream.FlushFinalBlock();

$encryptedData = $encryptorMemoryStream.ToArray()

Write-Host $enc.GetString($encryptedData)
Write-Host $encryptedData.Length

# Decrypt some data
$descryptAlgorithm = [System.Security.Cryptography.SymmetricAlgorithm] (New-Object System.Security.Cryptography.AesCryptoServiceProvider)

$descryptAlgorithm.Mode = [System.Security.Cryptography.CipherMode]::CBC
$descryptAlgorithm.Padding = [System.Security.Cryptography.PaddingMode]::PKCS7
$descryptAlgorithm.KeySize = 128
$descryptAlgorithm.BlockSize = 128
$descryptAlgorithm.Key = @(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
$descryptAlgorithm.IV = @(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)

$decryptor = [System.Security.Cryptography.ICryptoTransform]$descryptAlgorithm.CreateDecryptor()

$dataToDecrypt = $encryptedData

$decryptorMemoryStream = new-Object IO.MemoryStream @(,$dataToDecrypt)
$decryptorCryptoStream = new-Object Security.Cryptography.CryptoStream $decryptorMemoryStream,$decryptor,"Read"
$streamReader = new-Object IO.StreamReader $decryptorCryptoStream
try
{
    Write-Output $streamReader.ReadToEnd()
}
catch
{
    $e = $_.Exception
    $msg = $e.Message
    while ($e.InnerException) {
      $e = $e.InnerException
      $msg += "`n" + $e.Message
    }
    $msg
}
让我们看一些触发填充异常的示例

未能刷新最终块

这篇文章很好地描述了这个场景

#$encryptorCryptoStream.FlushFinalBlock();
无效密钥

$descryptAlgorithm.BlockSize = 64
我在这里稍微更改了用于解密密钥的字节数组,以模拟不匹配的密钥

$descryptAlgorithm.Key = @(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17)
使用空输入管理RijndaelManager

$descryptAlgorithm = [System.Security.Cryptography.SymmetricAlgorithm] (New-Object System.Security.Cryptography.RijndaelManaged)
// ...
$dataToDecrypt = @()
填充无效

我已经手动在要解密的数据末尾添加了一些无效的填充

$dataToDecrypt = $encryptedData + @(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
不同的填充方案

$descryptAlgorithm.BlockSize = 64
有趣的是,只有一些填充方案组合会导致填充错误。很多人会解密而不会出错,即使结果是错误的

$descryptAlgorithm.Padding = [System.Security.Cryptography.PaddingMode]::ANSIX923
不同的块大小

$descryptAlgorithm.BlockSize = 64

这些是直接的错误,但还有更多错误:错误的密钥、错误的IV(使用单块消息)、错误的密文/IV/密钥编码。需要注意的是,不应将填充检查用作错误的指示器,因为在没有正确输入的情况下,大约256分之一的密文是可解密的。这就是为什么使用诸如GCM或EAX之类的认证模式,或者使用带有强MAC的加密然后MAC方案(如HMAC-SHA256)很重要的原因。也没有提到:1。对于在没有填充的情况下调试decrypt,这样您可以看到填充并告诉它实际上是填充错误(通常不是)或其他错误。2.如果IV不正确,只有第一个模块错误,因此可以验证。3.如果加密是AES,一般情况下,块大小将为128位。虽然Rijndael可能具有非AES块大小或密钥大小,但这种情况很少见。4.如果未进行最终确定调用,则输出大小将不正确。总之,这不是一个特别通透或有用的问题-答案对。代码也应该在问题中吗?这个问题应该被删除,因为它太宽泛,不清楚要问什么,为什么这个代码不起作用(如果有代码的话)。IV应该是随机字节,通常在加密数据的前面加上前缀,用于解密,而不是密钥。不应将填充错误指示返回给调用方,否则可能导致填充oracle安全漏洞。