public static string DecodePassword(string encodedPassword, Process process) { byte[] encodedBytes; using (TextAsStream _stream = new TextAsStream(encodedPassword)) using (TextCodedStream stream = new TextCodedStream(_stream, false, false)) { List<byte> _encodedBytes = new List<byte>(); int bufferSize = 1; byte[] buffer = new byte[bufferSize]; while (true) { int countRead = stream.Read(buffer, 0, bufferSize); if (countRead != bufferSize) { break; } _encodedBytes.Add(buffer[0]); } encodedBytes = _encodedBytes.ToArray(); } byte[] passwordBytes = MangleBytes(encodedBytes, process); string password = ASCIIEncoding.ASCII.GetString(passwordBytes); return password; }
public static string EncodePassword(string password, Process process) { byte[] passwordBytes = ASCIIEncoding.ASCII.GetBytes(password); byte[] encodedBytes = MangleBytes(passwordBytes, process); string encodedPassword; using (MemoryStream _stream = new MemoryStream()) { using (TextCodedStream stream = new TextCodedStream(_stream, true, true)) { stream.Write(encodedBytes, 0, encodedBytes.Length); stream.Flush(); } _stream.Position = 0; encodedPassword = StreamFunctions.ToString(_stream); } return encodedPassword; }
internal static string GeneratePasswordFileName(string prompt, Process process) { int encodeLength = 8; byte[] encodedBytes = new byte[encodeLength]; byte[] promptBytes = ASCIIEncoding.ASCII.GetBytes(prompt); byte[] idBytes = BitConverter.GetBytes((short)process.Id); for (int byteIndex = 0; byteIndex < promptBytes.Length; byteIndex++) { byte value = (byte)(promptBytes[byteIndex] ^ idBytes[byteIndex % idBytes.Length] ^ encodedBytes[byteIndex % encodeLength]); encodedBytes[byteIndex % encodeLength] = value; } string encodedFileName; using (MemoryStream _stream = new MemoryStream()) { using (TextCodedStream stream = new TextCodedStream(_stream, true, true)) { stream.Write(encodedBytes, 0, encodeLength); stream.Flush(); } _stream.Position = 0; encodedFileName = StreamFunctions.ToString(_stream); } return encodedFileName; }