如何解密通过.net

时间:2017-09-01 21:28:14

标签: powershell encryption

我不是加密专家,但我能够使用以下代码加密.net应用程序中的字符串,所以我使用“mystring”这是来自.net的明文并使用CipherUtility加密它

string encrypted = CipherUtility.Encrypt<AesManaged>(mystring, "password", "salt");

我从以下链接使用的CipherUtility代码

https://bitlush.com/blog/symmetric-encryption-in-c-sharp

我能够将加密的字符串传递给Python并使用AES lib /模块从Python解密它。现在我想弄清楚如何将我的加密字符串传递给Powershell并使用一些AES方法通过Powershell解密?

我正在看这段代码

https://gist.github.com/ctigeek/2a56648b923d198a6e60

但我无法弄清楚如何让它工作只提供设置为“密码”的密码和从上面的例子设置为“盐”的盐?

我尝试了在此站点找到的代码中的解密函数,但仍然遇到填充错误 https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Script-410ef9df

更新:

这是我的.net代码加密来自cipher_utility.cs

public static string Encrypt<T>(string value, string password, string salt)
             where T : SymmetricAlgorithm, new()
        {
            DeriveBytes rgb = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt));

            SymmetricAlgorithm algorithm = new T();

            byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3);
            byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3);
            string test = Encoding.UTF8.GetString(rgbKey);

            ICryptoTransform transform = algorithm.CreateEncryptor(rgbKey, rgbIV);

            using (MemoryStream buffer = new MemoryStream())
            {
                using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Write))
                {
                    using (StreamWriter writer = new StreamWriter(stream, Encoding.Unicode))
                    {
                        writer.Write(value);
                    }
                }

                return Convert.ToBase64String(buffer.ToArray());
            }
        }

然后我从上面的gallery.technet.microsoft帖子中获取代码并根据我的.net代码将其更改为我认为应该是:)

function Decrypt-String($Encrypted, $salt="salt", $init="password")
{
    # If the value in the Encrypted is a string, convert it to Base64
    if($Encrypted -is [string]){
        $Encrypted = [Convert]::FromBase64String($Encrypted)
       }


    # Create a COM Object for RijndaelManaged Cryptography
    $r = new-Object System.Security.Cryptography.RijndaelManaged

    # Convert the Passphrase to UTF8 Bytes
    #$pass = [Text.Encoding]::UTF8.GetBytes($Passphrase)

    # Convert the Salt to UTF Bytes
    $salt = [Text.Encoding]::UTF8.GetBytes($salt)

    # Create the Encryption Key using the passphrase, salt and SHA1 algorithm at 256 bits
    $r.Key = (new-Object Security.Cryptography.PasswordDeriveBytes $init, $salt, "SHA1", 5).GetBytes(32) #256/8

    # Create the Intersecting Vector Cryptology Hash with the init
    $r.IV = (new-Object Security.Cryptography.SHA1Managed).ComputeHash( [Text.Encoding]::UTF8.GetBytes($init) )[0..15]

    # Create a new Decryptor
    $d = $r.CreateDecryptor()

    # Create a New memory stream with the encrypted value.
    $ms = new-Object IO.MemoryStream @(,$Encrypted)

    # Read the new memory stream and read it in the cryptology stream
    $cs = new-Object Security.Cryptography.CryptoStream $ms,$d,"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 the memory stream
    $ms.Close()

    # Clears the RijndaelManaged Cryptology IV and Key
    $r.Clear()

}

我收到以下填充错误

Exception calling "Close" with "0" argument(s): "Padding is invalid and cannot be removed."
At C:\Users\user1\AppData\Local\Temp\67499731-a8a3-4871-a807-f11ba86a26bf.ps1:33 char:5
+     $sr.Close()
+     ~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : CryptographicException

所以我的最终目标是在powershell中使用AES解密我在.net中加密的内容。

0 个答案:

没有答案