0

Usecase: I want to mask input text(mainly password) in run.bat file, i am using java as programming language,overall my major objective is that whenever user provide input for password in run.batch it should be encrypted,is it possible to do so? Here is my run.batch file :

@echo OFF
setlocal DISABLEDELAYEDEXPANSION
java -jar "stack.jar" -username "stackoverflow" -password "qwerty@567" -configpath "config.ini"
pause

Here i want to mask qwerty@567 which is password given by user end Here is something that i tried but it works only for console

import java.io.Console;
public class Main {
public static void main(String[] args) {
Console console = System.console();
char[] passwordArray = console.readPassword("Enter your password: ");
console.printf("Password is: %s%n", new String(passwordArray));
}
}
Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
  • 2
    I am confused. How can you write the password in the batch file when you ask the user to enter it? Do you write the batch file from your java code? – Abra Mar 27 '20 at 05:55
  • Try this - https://stackoverflow.com/questions/10819469/hide-input-on-command-line – RRIL97 Mar 27 '20 at 06:03
  • @Abra my intention is to run .jar file for which i need run.batch in which user will pass parameter as shown above, now i want whenever user will give anything as password in run.batch file it should be encrypted – Rajat Krishnan Mar 27 '20 at 07:07
  • if you can't use the Java tools @RR_IL showed, then perhaps https://stackoverflow.com/a/20343074/917548 – Roger Lindsjö Mar 27 '20 at 11:16

2 Answers2

0

The password can be an argument to your batch file: run.bat.

Modified batch file (from your question)

@echo OFF
setlocal DISABLEDELAYEDEXPANSION
java -jar "stack.jar" -username "stackoverflow" -password %1 -configpath "config.ini"
pause

And launch your batch file as:

run.bat my_password

Now the password does not appear in the batch file.

Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
Abra
  • 19,142
  • 7
  • 29
  • 41
  • can you please elaborate that how to launch run.bat my_password command because as soon as i click on run.batch the program starts running so i am not getting any option for entering the password during runtime – Rajat Krishnan Mar 28 '20 at 13:31
  • @RajatKrishnan just replace _my_password_ with the actual password. Then you don't need to ask the user to enter it. In other words, when the user wants to run your batch file, he writes his password on the command line. For example if I want to run your batch file, and my password is GEORGE, I would open a command prompt and write `run.bat GEORGE` – Abra Mar 28 '20 at 13:37
0

In windows 10, If you take the input directly as a variable in batch, using Set /P, you can change the Color code to black on black for the input using the ANSI Color code <esc>[30m (with <esc> replaced by the Ansi escape character)

Use <esc>[0m after taking input to reset the color.

T3RR0R
  • 2,747
  • 3
  • 10
  • 25
  • Thanks for support,but i guess this is not something i am looking for – Rajat Krishnan Mar 28 '20 at 10:37
  • Given it achieves the objective as laid out in your question, it would be useful to clarify in what way this is not what your looking for. I notice in one of your comments you mention encrypting the password. If this is what you are looking for, you should update your question to reflect that. – T3RR0R Mar 28 '20 at 11:43
  • its clear sir that i want to encrypt my password but can you show that how are you passing the command in run.batch with the given solution? – Rajat Krishnan Mar 28 '20 at 14:48
  • 1
    In your question you use the terms `mask` and `not be visible` in reference to `Input`, you do use the phrase `Encryption` anywhere within your question, hence why I recommend editing the question, as it is not clear. – T3RR0R Mar 28 '20 at 15:53
  • [demonstration of the suggested input masking](https://pastebin.com/G7dPerim) – T3RR0R Mar 28 '20 at 16:02
  • Ok its done, any suggestions now, like how to achieve the asked operation? – Rajat Krishnan Mar 28 '20 at 16:03
  • Depending on the level of security you need, Particularly if encryption is genuinely required, Batch is not the right language to be using. [Powershell offers better tools for this task](https://www.pdq.com/blog/secure-password-with-powershell-encrypting-credentials-part-1/) – T3RR0R Mar 28 '20 at 16:07