/// <summary> /// Processes records from the input pipeline. /// For each input object, the command decrypts the data, /// then exports a new SecureString created from the object. /// </summary> protected override void ProcessRecord() { SecureString importedString = null; Utils.CheckArgForNullOrEmpty(_s, "String"); try { string encryptedContent = String; byte[] iv = null; // If this is a V2 package if (String.IndexOf(SecureStringHelper.SecureStringExportHeader, StringComparison.OrdinalIgnoreCase) == 0) { try { // Trim out the header, and retrieve the // rest of the string string remainingData = this.String.Substring( SecureStringHelper.SecureStringExportHeader.Length, String.Length - SecureStringHelper.SecureStringExportHeader.Length); // Unpack it from Base64, get the string // representation, then parse it into its components. byte[] inputBytes = Convert.FromBase64String(remainingData); string dataPackage = System.Text.Encoding.Unicode.GetString(inputBytes); string[] dataElements = dataPackage.Split(Utils.Separators.Pipe); if (dataElements.Length == 3) { encryptedContent = dataElements[2]; iv = Convert.FromBase64String(dataElements[1]); } } catch (FormatException) { // Will be raised if we can't convert the // input from a Base64 string. This means // it's not really a V2 package. encryptedContent = String; iv = null; } } if (SecureKey != null) { Dbg.Diagnostics.Assert(Key == null, "Only one encryption key should be specified"); importedString = SecureStringHelper.Decrypt(encryptedContent, SecureKey, iv); } else if (Key != null) { importedString = SecureStringHelper.Decrypt(encryptedContent, Key, iv); } else if (!AsPlainText) { importedString = SecureStringHelper.Unprotect(String); } else { importedString = SecureStringHelper.FromPlainTextString(String); } } catch (ArgumentException e) { ErrorRecord er = SecurityUtils.CreateInvalidArgumentErrorRecord( e, "ImportSecureString_InvalidArgument" ); WriteError(er); } catch (CryptographicException e) { ErrorRecord er = SecurityUtils.CreateInvalidArgumentErrorRecord( e, "ImportSecureString_InvalidArgument_CryptographicError" ); WriteError(er); } if (importedString != null) { WriteObject(importedString); } }