Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Decrypt a SecureString on Remote Computer

$
0
0

Hi,

I have two systems namely comp1 and comp2, both in same domain. I am using powershell encryption method on comp1, to encrypt a text ad save it to a file.

 

$secure= Read-Host "Enter Password"-asSecureString $bytes= ConvertFrom-SecureString $secure$bytes | Out-File c:\securePassword.txt

Now I am copying the exact file to comp2 and trying to decrypt it by using this code:

 

function Decrypt([string]$exportfile)
{$securepassword= ConvertTo-SecureString $exportfile$marshal=[System.Runtime.InteropServices.Marshal]$ptr=$marshal::SecureStringToBSTR( $securepassword )$str=$marshal::PtrToStringBSTR( $ptr )$marshal::ZeroFreeBSTR( $ptr )return$str
}$exportfile= get-content c:\securepassword.txt
Decrypt $exportfile

but I am getting this error:

------------------------------------------------------------------------------------------------------------------------

ConvertTo-SecureString : Key not valid for use in specified state.

At line:3 char:42
+  $securepassword = ConvertTo-SecureString <<<<  $exportfile
    + CategoryInfo          : InvalidArgument: (:) [ConvertTo-SecureString], C
   ryptographicException
    + FullyQualifiedErrorId : ImportSecureString_InvalidArgument_Cryptographic
   Error,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand
 
Exception calling "SecureStringToBSTR" with "1" argument(s): "Value cannot be n
ull.
Parameter name: s"
At line:5 char:37
+  $ptr = $marshal::SecureStringToBSTR <<<< ( $securepassword )
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
 
Cannot convert argument "0", with value: "", for "PtrToStringBSTR" to type "Sys
tem.IntPtr": "Cannot convert null to type "System.IntPtr"."
At line:6 char:34
+  $str = $marshal::PtrToStringBSTR <<<< ( $ptr )
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
 
Cannot convert argument "0", with value: "", for "ZeroFreeBSTR" to type "System
.IntPtr": "Cannot convert null to type "System.IntPtr"."
At line:7 char:24
+  $marshal::ZeroFreeBSTR <<<< ( $ptr )
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

------------------------------------------------------------------------------------------------------------------------

How can I perform this operation. I also don't wanna use any encryption which involves any key. Is there is another way of doing this?


Regards, Ravi



Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>