Laitosto
Laitosto

Reputation: 55

Using CreateDecryotor() in PowerShell

I was wondering if anyone could help me out with an error I am getting in PowerShell. I am having no issue with creating the encryptor shown below:

 $Crypto = New-Object System.Security.Cryptography.RNGCryptoServiceProvider 
 $IV = New-Object System.Byte[] 16 
 $Crypto.GetNonZeroBytes($iv) 

 $RIJSym = new-Object System.Security.Cryptography.RijndaelManaged   

 [byte[]] $Key = ('mysecret$%@').ToCharArray() 
 $Encryptor = $RIJSym.CreateEncryptor($Key,$IV) 

But for what ever reason I am having an issue when I want to decrypt my key, here is what I am using and the error I get when the Program runs:

$Decrypted = $RIJSym.CreateDecryptor($Encryptor)

Error Message

 Cannot find an overload for "CreateDecryptor" and the argument count: "1".  
 At line:15 char:1
 + $DeCryp = $rijSym.CreateDecryptor($encryptor)
 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo          : NotSpecified: (:) [], MethodException
     + FullyQualifiedErrorId : MethodCountCouldNotFindBest

Upvotes: 0

Views: 1149

Answers (1)

Frode F.
Frode F.

Reputation: 54911

The error says it all... CreateDecryptor() doesn't have an overload the uses onl a single argument. The valid overloads are:

PS > $RIJSym.CreateDecryptor

OverloadDefinitions
-------------------
System.Security.Cryptography.ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV)
System.Security.Cryptography.ICryptoTransform CreateDecryptor()

You need to create the decryptor the same way you created the encrypter: by specifying the key and IV. Ex.

$Decrypted = $RIJSym.CreateDecryptor($Key, $IV)

Upvotes: 3

Related Questions