/// <summary> /// GetObjectData /// </summary> /// <param name="info"></param> /// <param name="context"></param> public void GetObjectData(SerializationInfo info, StreamingContext context) { if (info == null) { return; } // serialize the secure string string safePassword = string.Empty; if (_password != null && _password.Length > 0) { byte[] key; byte[] iv; if (s_delegate != null && s_delegate(context, out key, out iv)) { safePassword = SecureStringHelper.Encrypt(_password, key, iv).EncryptedData; } else { try { safePassword = SecureStringHelper.Protect(_password); } catch (CryptographicException cryptographicException) { throw PSTraceSource.NewInvalidOperationException(cryptographicException, Credential.CredentialDisallowed); } } } info.AddValue("UserName", _userName); info.AddValue("Password", safePassword); }
/// <summary> /// Processes records from the input pipeline. /// For each input object, the command encrypts /// and exports the object. /// </summary> protected override void ProcessRecord() { string exportedString = null; EncryptionResult encryptionResult = null; const string argumentName = "SecureString"; Utils.CheckSecureStringArg(SecureStringData, argumentName); if (SecureStringData.Length == 0) { throw PSTraceSource.NewArgumentException(argumentName); } if (SecureKey != null) { Dbg.Diagnostics.Assert(Key == null, "Only one encryption key should be specified"); encryptionResult = SecureStringHelper.Encrypt(SecureString, SecureKey); } else if (Key != null) { encryptionResult = SecureStringHelper.Encrypt(SecureString, Key); } else { exportedString = SecureStringHelper.Protect(SecureString); } if (encryptionResult != null) { // The formatted string is Algorithm Version, // Initialization Vector, Encrypted Data string dataPackage = string.Format( System.Globalization.CultureInfo.InvariantCulture, "{0}|{1}|{2}", 2, encryptionResult.IV, encryptionResult.EncryptedData); // encode the package, and output it. // We also include a recognizable prefix so that // we can use the old decryption mechanism if we // don't see it. While the old decryption // generated invalid data for the first bit of the // SecureString, it at least didn't generate an // exception. byte[] outputBytes = System.Text.Encoding.Unicode.GetBytes(dataPackage); string encodedString = Convert.ToBase64String(outputBytes); WriteObject(SecureStringHelper.SecureStringExportHeader + encodedString); } else if (exportedString != null) { WriteObject(exportedString); } }
protected override void ProcessRecord() { string str = null; EncryptionResult encryptionResult = null; Utils.CheckSecureStringArg(base.SecureStringData, "SecureString"); if (base.SecureKey == null) { if (base.Key == null) { str = SecureStringHelper.Protect(this.SecureString); } else { encryptionResult = SecureStringHelper.Encrypt(this.SecureString, base.Key); } } else { encryptionResult = SecureStringHelper.Encrypt(this.SecureString, base.SecureKey); } if (encryptionResult == null) { if (str != null) { base.WriteObject(str); } return; } else { object[] v = new object[3]; v[0] = 2; v[1] = encryptionResult.IV; v[2] = encryptionResult.EncryptedData; string str1 = string.Format(CultureInfo.InvariantCulture, "{0}|{1}|{2}", v); byte[] bytes = Encoding.Unicode.GetBytes(str1); string base64String = Convert.ToBase64String(bytes); base.WriteObject(string.Concat(SecureStringHelper.SecureStringExportHeader, base64String)); return; } }