Let's say I need to do this in Powershell:
$SecurePass = Get-Content $CredPath | ConvertTo-SecureString -Key (1..16) [String]$CleartextPass = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($CredPass));
The contents of $ CredPath is a file containing the output of ConvertFrom-SecureString -Key (1..16).
How to execute the ConvertTo-SecureString -key (1..16) part in C # /. NET?
I know how to create a SecureString , but I'm not sure how encryption should be handled.
Do I encrypt each character with AES or decrypt a string and then create a secure string for the character?
I know almost nothing about cryptography, but from what I put together, I could just call the Powershell command using C #.
For reference, I found a similar AES encryption / decryption entry here: Using AES encryption in C #
UPDATE
I looked at the link posted by Keith , but I come across additional unknowns. DecryptStringFromBytes_Aes takes three arguments:
static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
The first argument is a byte array representing the ciphertext. The question is, how should a string be displayed in an array of bytes? Should it be submitted with or without encoding?
byte[] ciphertext = Encoding.ASCII.GetBytes(encrypted_text); byte[] ciphertext = Encoding.UTF8.GetBytes(encrypted_text); byte[] ciphertext = Encoding.Unicode.GetBytes(encrypted_text); byte[] ciphertext = new byte[encrypted_password.Length * sizeof(char)]; System.Buffer.BlockCopy(encrypted_password.ToCharArray(), 0, text, 0, text.Length);
The second byte array is the key, which should be just an integer:
byte[] key = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
The third byte array is the “Initialization Vector” - it seems that calling Aes.Create () will generate byte [] for IV randomly. Looking through, I found that I might need to use the same IV. Since ConvertFrom-SecureString and ConvertTo-SecureString can encrypt / decrypt using only the key, I remain with the assumption that IV [] can be random or has a static definition.
I have not found a winning combination, but I will continue to try.