How to decrypt a string in C #, which is encrypted through PowerShell

Is it possible to decrypt a string in C #, which is encrypted through PowerShell and how?

The string is encrypted through PowerShell, as shown below:

$pw = read-host "Enter Password" –AsSecureString ConvertFrom-SecureString $pw | out-file "C:\file.txt" 

To convert it using PowerShell, I can use these commands that call the C # class System.Runtime.InteropServices.Marshal class.

 $pwdSec = Get-Content "C:\file.txt" | ConvertTo-SecureString $bPswd = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwdSec) $pswd = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bPswd) 

The file contains a string that has been converted to the encrypted standard string("hello") .

So, if you open file.txt , it looks like this:

 01000000d08c9ddf0115d1118c7a00c04fc297eb0100000052ded6c2db80e748933432e19b9de8b10000 000002000000000003660000c00000001000000016dc35885d76d07bab289eb9927cfc1e000000000480 0000a0000000100000003106cde553f45b08d13d89d11336170b280000005cc865c1ee1b57e84ed3d1a2 d3f2d0ec0f189b532e61c18d1f31444d6f119a1e8368477fd2d81f54140000000cb0262e58b08ae14f37 22c14c69684841b6b21c 
+7
source share
1 answer

The output file from ConvertFrom-SecureString you ConvertFrom-SecureString is a UTF-16 string (password) protected by ProtectedData.Protect stored as a hex dump.

To restore the encoding, use:

 // Read file to string string exportedData = File.ReadAllText(@"file.txt"); // Remove all new-lines exportedData = exportedData.Replace(Environment.NewLine, ""); // Convert the hex dump to byte array int length = exportedData.Length / 2; byte[] encryptedData = new byte[length]; for (int index = 0; index < length; ++index) { encryptedData[index] = byte.Parse( exportedData.Substring(2 * index, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture); } // Decrypt the byte array to Unicode byte array byte[] data = ProtectedData.Unprotect(encryptedData, (byte[])null, DataProtectionScope.CurrentUser); // Convert Unicode byte array to string string password = Encoding.Unicode.GetString(data); 

The above code works if you did not specify -Key using ConvertFrom-SecureString . The secured string is then secured using the Windows Data Protection API (DPAPI) . As such, the string must be decoded on the same machine and the account in which it was encoded.

+6
source

All Articles