Eigentlich entschlüssle ich Strings, die ich mit Powershell in C# erstellt habe.Decrypt SecureString mit benutzerdefinierten Schlüssel
erstelle ich die Secure mit folgenden Powershell-Befehl:
ConvertTo-SecureString -String "TopSecret" -AsPlainText -Force | ConvertFrom-SecureString
ich die Secure mit folgenden C# -Code zu entschlüsseln:
string exportedData = string.Empty;
bool SecureStringOK = true;
try
{
// Read args[0] to string
exportedData = args[0];
}
catch (System.IndexOutOfRangeException)
{
Console.WriteLine("NO_SECURESTRING");
Debug.WriteLine("NO_SECURESTRING");
SecureStringOK = false;
}
if (SecureStringOK)
{
// Decrypt the byte array to Unicode byte array
try
{
// 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);
}
byte[] data = ProtectedData.Unprotect(encryptedData, (byte[])null, DataProtectionScope.CurrentUser);
// Convert Unicode byte array to string
string password = Encoding.Unicode.GetString(data);
// Write Output
Console.WriteLine(password);
Debug.WriteLine(password);
}
catch (System.Security.Cryptography.CryptographicException)
{
Console.WriteLine("WRONG_SECURESTRING: " + args[0]);
Debug.WriteLine("WRONG_SECURESTRING: " + args[0]);
}
catch (System.FormatException)
{
Console.WriteLine("WRONG_SECURESTRING_FORMAT: " + args[0]);
Debug.WriteLine("WRONG_SECURESTRING_FORMAT: " + args[0]);
}
}
Dies funktioniert in beide Richtungen gut, aber jetzt erstelle ich die Secure in Powershell mit meiner eigenen Schlüsseldatei:
ConvertTo-SecureString -String "TopSecret" -AsPlainText -Force | ConvertFrom-SecureString -Key $KeyPath
Irgendwelche Ideen was Ich muss den C# -Code ändern, um eine bestimmte Schlüsseldatei zu verwenden?