![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Mar 2005
Location: Lubbock, TX
Posts: 30
Rep Power: 0
![]() |
Security code
I need to know how to program a program that when you input a password it does not show the password or shows asterisks in thier place
I've found how to do it on Java, but I need to do it in C++, I know that better. Thanks --David |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Search the C and C++ forums using those terms. This is addressed fairly regularly.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Mar 2005
Location: Lubbock, TX
Posts: 30
Rep Power: 0
![]() |
I have been trying for awhile but I can't word it right for the search engine. Feel kinda dumb lol
|
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I found this just searching on "password." I will tell you up front that there's a lot of trash in it, but you should read it anyway, just to reinforce things. Let me give you a quick rundown.
There is no built in function that accomplishes what you want in C or C++. It would not be portable from platform to platform. Java can presume a virtual machine that represents a common platform wherever the code is run. That's the theory, anyway. C/C++ cannot do that as they produce machine code, which varies widely from one hardware CPU to another. There is no guarantee what the input device will look like and no guarantee what the output device will look like and no guarantee that a software driver writer will have addressed the issue. That said, C/C++ are flexible. You may write code that does about any dam' thing you want. You just have to know your OS/platform and what assistive functions may be available to you in libraries. The password thangy would be written one way on a *nix platform and another way on a Win box and possibly another way on something else like a Mac. You haven't specified which of those conditions prevails in your situation. (The "How to Post..." threads address the issue of providing needed information and asking questions efficiently, if you haven't read one.) Perhaps you'd care to elaborate.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Mar 2005
Location: Lubbock, TX
Posts: 30
Rep Power: 0
![]() |
Right, I am running on an x86 processor, XP (soon to be dual boot with SUSE), currently using Dev-C++ on XP. I read that post, and I couldn't get too much information from it. I'm still fairly new to programming, first year at Texas Tech comp sci major, and have been on and off programming for a few years.
Thanks for all the help y'all are giving me David |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Your quickest way, at the moment, is to use the conio facilities that come with Dev-C++. They have raped this substantially in recent years, but you still have the opportunity to use "getch", which gathers a character without echoing it. Echoing asterisks, if you desire, is your job. Handling backspaces is also your job.
You can reproduce the conio functions with the Win 32 console API. More than you want to attempt at the moment, I suspect. For *nix, you have to take a different route.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,031
Rep Power: 5
![]() |
The alternative to conio.h functions like getch() that DaWei mentioned would be to call the Win32 API. On all versions of Win32 except WinCE (the Windows family on mobile devices, such as PDAs and smartphones), you have the option of console-mode programs. Actually, a console program can create windows, and a regular program can open consoles (ie for debugging purposes), but generally, you stick with one or the other.
On versions of Windows with console support, there is a rich collection of API functions for handling many tasks, such as getting/setting the text color, text position, screen buffer size, and much more. You can very easily read input events (both keyboard and mouse) that pertain to your application, and act accordingly. Look up ReadConsole() and ReadConsoleInput() in your Win32 API docs, or on MSDN. There's also this site that's got a lot of helpful information for beginners regarding stuff you can do with consoles, among other things. If you're still itching for an example, the following code should work just fine under DevC++. The TCHAR stuff is for source-level portability among Win32 flavors; some use 8-bit characters, and others use 16-bit Unicode characters. TCHAR is defined at compile time to be the appropriate type. It's also supposed to read STD_INPUT_HANDLE where it says STD_INPUT_HAN DLE. For some reason, the forum kept buggering my post. #include <windows.h>
#include <iostream>
#include "tchar.h"
int main(void);
TCHAR readKey(void);
TCHAR *getPassword(TCHAR *buffer, int length);
int main(void)
{
TCHAR *password;
const int MAX_DIGITS = 10; // number of digits allowed in password
std::cout << "Please enter your password: ";
password = getPassword(NULL, MAX_DIGITS);
std::cout << "\n\nYour password is \"" << password << "\".\n\n";
std::cout << "Done, hit a key to exit."<< std::flush;
readKey();
return 0;
}
// reads a key, returning its ASCII or Unicode value. clears the input buffer
// prior to reading a key, so will block until new keyboard input is received.
TCHAR readKey(void)
{
INPUT_RECORD input;
DWORD count;
FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
do
{
ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE), &input, 1, &count);
} while((input.EventType != KEY_EVENT) || (!input.Event.KeyEvent.bKeyDown) || (!input.Event.KeyEvent.uChar.UnicodeChar));
return (TCHAR)input.Event.KeyEvent.uChar.AsciiChar;
}
// retrieves a password, masking entered digits with an asterisk. buffer
// may be supplied by the caller, or allocated automagically by passing NULL.
TCHAR *getPassword(TCHAR *buffer, int length)
{
TCHAR key;
int position = 0;
if(!buffer) // if buffer is NULL, allocate space
buffer = new TCHAR[length+1];
do
{
key = readKey();
switch(key)
{
case '\b': // backspace
if(position > 0)
{
buffer[--position] = 0;
std::cout << "\b \b"; // do a 'destructive' backspace
}
break;
case '\n': // enter/return
case '\r':
break;
default:
if(position < length)
{
buffer[position++] = key;
std::cout << '*';
}
}
} while(key != '\n' && key != '\r'); // loop until user hits enter/return
return buffer;
}
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|