私はPowerShellのAesで簡単な暗号化/復号化をしようとしていますが、出力を復号化しようとしても例外は発生し続けます。引数 "0"を指定して "ReadToEnd"を呼び出す例外: "埋め込みが無効であり、削除できません"
私は例外を取得: "0" 引数(複数可)と "ReadToEnd" を呼び出す
例外: "パディングは 無効であり、削除することはできません"
Function Aes-Encrypt($plainTextBytes){
$key = "vqMcLYelBxefzIAMpO9Q/Q=="
$plainText = [System.Text.Encoding]::UTF8.GetBytes($plainTextBytes)
#Use the AES cipher and represent it as an object.
$AES = New-Object "System.Security.Cryptography.AesManaged"
$AES.Mode = [System.Security.Cryptography.CipherMode]::CBC
$AES.Padding = [System.Security.Cryptography.PaddingMode]::PKCS7
$AES.BlockSize = 128
$AES.KeySize = 128
$IV = $AES.IV
#$AES.Key = $key
# Creates a MemoryStream to do the encryption in
$ms = new-Object IO.MemoryStream
# Creates the new Cryptology Stream --> Outputs to $MS or Memory Stream
$cs = new-Object Security.Cryptography.CryptoStream $ms,$AES.CreateEncryptor(),"Write"
#Writes the string in the Cryptology Stream
$cs.Write($IV, 0, 16)
$cs.Write($plainText, 0, $plainText.Length);
$cs.FlushFinalBlock();
# Stops the Cryptology Stream
$cs.Close()
# Stops writing to Memory
$ms.Close()
# Clears the IV and HASH from memory to prevent memory read attacks
$cs.Clear()
# Takes the MemoryStream and puts it to an array
[byte[]]$rmesult = $ms.ToArray()
# return $ms.ToArray()
# Converts the array from Base 64 to a string and returns
return [Convert]::ToBase64String($rmesult)
}
Function Aes-Decrypt($DecryptData){
$key = "vqMcLYelBxefzIAMpO9Q/Q=="
#$plainText = [System.Text.Encoding]::UTF8.GetBytes($plainTextBytes)
# Create a COM Object for RijndaelManaged Cryptography
#$r = new-Object System.Security.Cryptography.RijndaelManaged
#Use the AES cipher and represent it as an object.
$AES = New-Object "System.Security.Cryptography.AesManaged"
$AES.Mode = [System.Security.Cryptography.CipherMode]::CBC
$AES.Padding = [System.Security.Cryptography.PaddingMode]::PKCS7
$AES.BlockSize = 128
$AES.KeySize = 128
$IV = $AES.IV
#$AES.Key = $key
$cipherTextBytes = [Convert]::FromBase64String($DecryptData)
# Creates a MemoryStream to do the encryption in
$ms = new-Object IO.MemoryStream @(,$cipherTextBytes)
# Creates the new Cryptology Stream --> Outputs to $MS or Memory Stream
$cs = new-Object Security.Cryptography.CryptoStream $ms,$AES.CreateDecryptor(),"Read"
# Read the new decrypted stream
$sr = new-Object IO.StreamReader $cs
# Return from the function the stream
Write-Output $sr.ReadToEnd()
# Stops the stream
$sr.Close()
# Stops the crypology stream
$cs.Close()
# Stops writing to Memory
$ms.Close()
$cs.Clear()
# Takes the MemoryStream and puts it to an array
return $ms.ToArray()
}