MATLAB: How to create an encrypted password in MATLAB

"hiddenasteriskeditencryptencryptionguihideMATLABpasswordstringtext;

I am writing a set of MATLAB scripts that communicate with a database. I need to prompt the user for a password to get into the database. Currently, when the user enters the password you can actually see the password as the user is typing. Is there a way to get a character (such as *) to appear on the display instead of letters/numbers while the user is entering the password?

Best Answer

Currently, you cannot have letters or numbers appear as particular characters in MATLAB. This has been forwarded to our development staff to be considered for a future version of MATLAB.
Please consider the following workarounds:
1. Use an ActiveX control
2. Set the foreground color to be the same as the background color, the typing will not be visible.
 
h = uicontrol('Style','Edit','ForeGroundColor',[.75 .75 .75]);
3. Use a font that does not appear as normal letters.
 
h = uicontrol('Style','Edit','Fontname','symbol');
4. A file exchange submission that replaces the characters typed with asterisks can be found in the following link:
Also, keep in mind if you are creating an application that will be used on a number of different platforms you will need to use a font name particular for each platform.
See the following as a possible MATLAB code workaround:
 
function passwd_test
u = uicontrol('style','edit','enable','inactive','backgroundcolor','w',...
'units','norm',...
'position',[.1 .1 .5 .1],...
'tag','edit')
u = uicontrol('style','text','enable','inactive','backgroundcolor','w',...
'units','norm',...
'position',[.1 .25 .5 .1],...
'tag','text')
set(gcf,'keypressfcn', {@handle_passwd})
function handle_passwd(h,eventData)
CK = get(gcf,'currentkey');
h_disp = findobj(gcbf,'tag','text');
h_asterix = findobj(gcbf,'tag','edit');
if gco == h_asterix,
str = get(h_disp,'string');
if strcmp(CK,'backspace'),
% Do something here if the backspace key was pressed
if length(str) > 1,
str = str(1:end-1);
end
elseif strcmp(CK,'space')
str = [str,' '];
else
str = [str,CK];
end
set(h_disp,'string',str);
set(h_asterix,'string',char(ones(1,length(str))*double('*')));
end