|
-
Feb 18th, 2005, 08:17 PM
#1
Thread Starter
Sleep mode
Password mask in a console app![Resolved]
I want to do in a console window like the PasswordChar method of a textbox . Something like password mask . I can do it in a dirty way but I thought there might be something ready out there .
Thanks
Last edited by Pirate; Feb 19th, 2005 at 09:45 AM.
-
Feb 18th, 2005, 10:06 PM
#2
Re: Password mask in a console app!
 Originally Posted by Pirate
I want to do in a console window like the PasswordChar method of a textbox . Something like password mask . I can do it in a dirty way but I thought there might be something ready out there .
Thanks
hehe in case anyone else needs it here's my answer
use the library I posted in this post
and here's the code for it, assuming you've referenced that file already, and you have a C# Console application
Code:
using System;
using ExtendedConsole; // add ExtendedConsole.dll from my post to your project's references for this to work
namespace ConsoleApplication27
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
ConsoleEx c = new ConsoleEx();
c.SetMode(ConsoleEx.InputMode.EchoInput);
int chr=0;
string pass="";
const int ENTER = 13;
do
{
chr = Console.Read();
Console.Write ('*');
pass += (char)chr;
}while (chr!=ENTER);
Console.WriteLine();
Console.WriteLine ("your password: " + pass);
Console.ReadLine();
}
}
}
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Feb 19th, 2005, 09:39 AM
#3
Thread Starter
Sleep mode
It works perfect . Do you have the source code in C# because I'd rather include it in my proj and not using a dll ?
Thanks a lot
-
Feb 19th, 2005, 09:44 AM
#4
Thread Starter
Sleep mode
You deserve the points dude !
-
Feb 19th, 2005, 02:13 PM
#5
Re: Password mask in a console app![Resolved]
lol thanks
no dont have it in C#....
if anyone here is bored and decides to rewrite it, it would be really cool thouhg. Cuz I personally dont use VB anymore and yeah I hate the idea of referencing DLLs too.
edit:One thing I can do is to decompile my compiled dll for ya....
umm I had to change some stuff for it to work, I compiled the example above and it worked fine. The code may be messed up cuz it's right from the decompiler
ExtendedConsole.ConsoleEx
Code:
// Decompiled by Salamander version 1.0.6
// Copyright 2002 Remotesoft Inc. All rights reserved.
// http://www.remotesoft.com/salamander
//using Microsoft.VisualBasic.CompilerServices;
using System;
using System.Text;
using ExtendedConsole;
namespace ExtendedConsole
{
public sealed class ConsoleEx
{
public enum InputMode
{
LineInput = 0,
EchoInput = 1,
}
public enum ConsoleColor
{
Black = 0,
Blue = Win32Native.FOREGROUND_BLUE,
Green = Win32Native.FOREGROUND_GREEN,
SkyBlue = Win32Native.FOREGROUND_BLUE + Win32Native.FOREGROUND_GREEN,
Red = Win32Native.FOREGROUND_RED,
Purple = Win32Native.FOREGROUND_BLUE + Win32Native.FOREGROUND_RED,
Brown = Win32Native.FOREGROUND_GREEN + Win32Native.FOREGROUND_RED,
White = Win32Native.FOREGROUND_BLUE + Win32Native.FOREGROUND_GREEN +
Win32Native.FOREGROUND_RED,
Gray = Win32Native.FOREGROUND_INTENSIFY,
BlueForte = Win32Native.FOREGROUND_BLUE + Win32Native.FOREGROUND_INTENSIFY,
GreenForte = Win32Native.FOREGROUND_GREEN + Win32Native.FOREGROUND_INTENSIFY,
SkyBlueForte = Win32Native.FOREGROUND_BLUE + Win32Native.FOREGROUND_GREEN +
Win32Native.FOREGROUND_INTENSIFY,
RedForte = Win32Native.FOREGROUND_RED + Win32Native.FOREGROUND_INTENSIFY,
PurpleForte = Win32Native.FOREGROUND_BLUE + Win32Native.FOREGROUND_RED +
Win32Native.FOREGROUND_INTENSIFY,
Yellow = Win32Native.FOREGROUND_GREEN + Win32Native.FOREGROUND_RED +
Win32Native.FOREGROUND_INTENSIFY,
WhiteForte = Win32Native.FOREGROUND_BLUE + Win32Native.FOREGROUND_GREEN +
Win32Native.FOREGROUND_RED + Win32Native.FOREGROUND_INTENSIFY
}
public enum CursorType
{
Off = 0,
SingleLine = 1,
Block = 2,
}
private IntPtr hConsoleIn;
private IntPtr hConsoleOut;
private Win32Native.CONSOLE_INFO conInfo;
private Win32Native.CURSOR_INFO cursorInfo;
private int backColor;
private short backgroundAttrib;
public string Title
{
get
{
StringBuilder stringBuilder = new StringBuilder(128);
Win32Native.GetConsoleTitle(stringBuilder, 128);
return stringBuilder.ToString();
}
set
{
Win32Native.SetConsoleTitle(value);
}
}
public int Columns
{
get
{
return conInfo.MaxSize.x;
}
}
public int Rows
{
get
{
return conInfo.MaxSize.y;
}
}
public int CursorX
{
get
{
updateConsoleInfo();
return conInfo.CursorPosition.x;
}
}
public int CursorY
{
get
{
updateConsoleInfo();
return conInfo.CursorPosition.y;
}
}
public ConsoleEx()
{
Win32Native.AllocConsole();
hConsoleIn = Win32Native.GetStdHandle(-10);
hConsoleOut = Win32Native.GetStdHandle(-11);
conInfo = new Win32Native.CONSOLE_INFO();
updateConsoleInfo();
cursorInfo = new Win32Native.CURSOR_INFO();
SetCursorType(CursorType.SingleLine);
backgroundAttrib = 7;
}
~ConsoleEx()
{
//base.Finalize();
FreeConsole();
}
public void SetMode(InputMode mode)
{
int i=0;
Win32Native.GetConsoleMode(hConsoleIn, ref i);
if (mode == InputMode.EchoInput)
{
i &= -7;
}
else
{
i |= 6;
}
Win32Native.SetConsoleMode(hConsoleIn, i);
}
public void Clear()
{
int i = 0;
Win32Native.COORD cOORD = new Win32Native.COORD();
cOORD.x = 0;
cOORD.y = 0;
Win32Native.FillConsoleOutputCharacter(hConsoleOut, ' ', (short)(conInfo.MaxSize.x * conInfo.MaxSize.y), cOORD, ref i);
Win32Native.FillConsoleOutputAttribute(hConsoleOut, backgroundAttrib, (short)(conInfo.MaxSize.x * conInfo.MaxSize.y), cOORD, ref i);
MoveCursor(1, 1);
}
public void EchoInput(bool value)
{
int i=0;
Win32Native.GetConsoleMode(hConsoleIn, ref i);
if (value)
{
i |= 4;
}
else
{
i &= -5;
}
Win32Native.SetConsoleMode(hConsoleIn, i);
}
public void SetColor(ConsoleColor foreColor, ConsoleColor backColor)
{
this.backColor = (int)backColor;
SetColor(foreColor);
}
public void SetColor(ConsoleColor foreColor)
{
Win32Native.SetConsoleTextAttribute(hConsoleOut, (int)foreColor + 16 * backColor);
}
public void SetClsColor(ConsoleColor backColor)
{
backgroundAttrib = (short)((short)backColor * 16);
}
public void MoveCursor(int x, int y)
{
conInfo.CursorPosition.x = (short)(x - 1);
conInfo.CursorPosition.y = (short)(y - 1);
if (cursorInfo.Visible)
{
int i = conInfo.CursorPosition.x + conInfo.CursorPosition.y * 65536;
Win32Native.SetConsoleCursorPosition(hConsoleOut, i);
}
}
public void SetCursorType(CursorType newType)
{
switch (newType)
{
case CursorType.Block:
cursorInfo.Size = 100;
cursorInfo.Visible = true;
break;
case CursorType.SingleLine:
cursorInfo.Size = 10;
cursorInfo.Visible = true;
break;
case CursorType.Off:
cursorInfo.Size = 100;
cursorInfo.Visible = false;
break;
}
Win32Native.SetConsoleCursorInfo(hConsoleOut, ref cursorInfo);
MoveCursor(conInfo.CursorPosition.x, conInfo.CursorPosition.y);
}
public void FreeConsole()
{
try
{
Win32Native.FreeConsole();
Win32Native.CloseHandle(hConsoleIn);
Win32Native.CloseHandle(hConsoleOut);
Win32Native.FreeConsole();
}
catch
{
}
}
private void updateConsoleInfo()
{
Win32Native.GetConsoleScreenBufferInfo(hConsoleOut, ref conInfo);
}
}
}
ExtendedConsole.Win32Native
Code:
// Decompiled by Salamander version 1.0.6
// Copyright 2002 Remotesoft Inc. All rights reserved.
// http://www.remotesoft.com/salamander
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace ExtendedConsole
{
class Win32Native
{
public enum BACKGROUNDCOLOR
{
bBLUE = 16,
bGREEN = 32,
bRED = 64,
bBRIGHT = 128,
}
internal struct COORD
{
internal short x;
internal short y;
}
internal struct RECT
{
public short Left;
public short Top;
public short Right;
public short Bottom;
}
internal struct CONSOLE_INFO
{
internal COORD Size;
internal COORD CursorPosition;
internal short Attribute;
internal RECT Window;
internal COORD MaxSize;
}
internal struct CURSOR_INFO
{
internal int Size;
internal bool Visible;
}
internal const int STD_OUTPUT_HANDLE = -11;
internal const int STD_INPUT_HANDLE = -10;
internal const short ENABLE_LINE_INPUT = 2;
internal const short ENABLE_ECHO_INPUT = 4;
internal const int FOREGROUND_BLUE = 1;
internal const int FOREGROUND_GREEN = 2;
internal const int FOREGROUND_RED = 4;
internal const int FOREGROUND_INTENSIFY = 8;
internal const int BACKGROUND_BLUE = 16;
internal const int BACKGROUND_GREEN = 32;
internal const int BACKGROUND_INTENSIFY = 128;
[DllImportAttribute("kernel32")]
public static extern void SetConsoleTitle(string lpTitleStr);
[DllImportAttribute("kernel32")]
public static extern void GetConsoleTitle(StringBuilder lpBuff, int buffSize);
[DllImportAttribute("kernel32")]
public static extern int SetConsoleTextAttribute(IntPtr hConsoleOutput, int wAttributes);
[DllImportAttribute("kernel32")]
public static extern int FillConsoleOutputCharacter(IntPtr Handle, char uChar, int Len, COORD start, ref int written);
[DllImportAttribute("kernel32")]
public static extern bool FillConsoleOutputAttribute(IntPtr Handle, short att, int Len, COORD start, ref int writted);
[DllImportAttribute("kernel32")]
public static extern void GetConsoleScreenBufferInfo(IntPtr Handle, ref CONSOLE_INFO info);
[DllImportAttribute("kernel32")]
public static extern bool SetConsoleCursorInfo(IntPtr Handle, ref CURSOR_INFO info);
[DllImportAttribute("kernel32")]
public static extern bool SetConsoleCursorPosition(IntPtr handle, int coord);
[DllImportAttribute("kernel32")]
public static extern IntPtr GetStdHandle(int nStdHandle);
[DllImportAttribute("kernel32")]
public static extern void GetConsoleMode(IntPtr hConsoleHandle, ref int lpMode);
[DllImportAttribute("kernel32")]
public static extern void SetConsoleMode(IntPtr hConsoleHandle, int dwMode);
[DllImportAttribute("kernel32")]
public static extern int CloseHandle(IntPtr hObject);
[DllImportAttribute("kernel32")]
public static extern int AllocConsole();
[DllImportAttribute("kernel32")]
public static extern int FreeConsole();
}
}
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Feb 19th, 2005, 04:33 PM
#6
Thread Starter
Sleep mode
It looks much nicer in C# . Why do you mean by decompiling it? you mean like you used a VB CONVERTER ?
-
Feb 19th, 2005, 04:40 PM
#7
Re: Password mask in a console app![Resolved]
 Originally Posted by Pirate
It looks much nicer in C#  . Why do you mean by decompiling it? you mean like you used a VB CONVERTER ?
lol no, if you read the first few lines of the c# code it tells you the program I used. I opened the VB dll file with a DECOMPILER and that program decompiles in C# code, which is very nice.
It wouldnt compile though, I had to change a lot of stuff to make it work
Yeah you can decompile .NET programs. You can use an obfuscator (shipped with vs.net) to just make the code more confusing (so when people decompile it, all the variable names are "obfuscated" and are changed so it's hard to figure out what the code does)
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Feb 19th, 2005, 04:41 PM
#8
Re: Password mask in a console app![Resolved]
 Originally Posted by Pirate
It looks much nicer in C#  . Why do you mean by decompiling it? you mean like you used a VB CONVERTER ?
umm p.s. what's a vb converter lol
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Feb 19th, 2005, 04:53 PM
#9
Thread Starter
Sleep mode
I see , I thought that compiler just shows the IL lang . Anyway , here's the converter I was talking about and there are more .http://www.developerfusion.co.uk/uti...btocsharp.aspx
-
Aug 12th, 2011, 04:34 AM
#10
New Member
Re: Password mask in a console app![Resolved]
Vanilla C# .NET, no custom anything that I'm aware of.
Code:
namespace Orb.App
{
/// <summary>
/// Adds some nice help to the console. Static extension methods don't exist (probably for a good reason) so the next best thing is congruent naming.
/// </summary>
static public class Console
{
/// <summary>
/// Like System.Console.ReadLine(), only with a mask.
/// </summary>
/// <param name="mask">a <c>char</c> representing your choice of console mask</param>
/// <returns>the string the user typed in </returns>
public static string ReadPassword(char mask)
{
string pass = "";
char chr = (char)0;
const int ENTER = 13;
do
{
chr = System.Console.ReadKey(true).KeyChar;
System.Console.Write(mask);
pass += (char)chr;
} while (chr != ENTER);
System.Console.WriteLine();
return pass;
}
/// <summary>
/// Like System.Console.ReadLine(), only with a mask.
/// </summary>
/// <returns>the string the user typed in </returns>
public static string ReadPassword()
{
return Orb.App.Console.ReadPassword('*');
}
}
}
-
Aug 24th, 2011, 04:05 PM
#11
Frenzied Member
Re: Password mask in a console app![Resolved]
Why not just do something along the lines of:
Code:
Char temp;
String pass = "";
do
{
temp = Console.ReadKey().KeyChar;
if (temp == 13) break;
pass += temp.ToString();
Console.Write((char)8);
Console.Write("*");
}while(true);
Console.WriteLine(pass);
Console.ReadLine();
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|