Question

I am developing java application which consumes with the web service, which then validates the user, I have the user enter his username and password. For using this application user required a valid username and password.

I have one context menu which will get activated when there is correct login. Otherwise i want it to get disabled.

And I want only a one time validation. So that, if any other user use that application from same system he dont need to enter the password again.

that means i need to save the password in local system, to use this password throughout the application

Any help regarding saving the password anyhow ?

Was it helpful?

Solution

Well, you can use a public and private key to encrypt or decrypt password.

Edit:

First of all you have to create a public/private key pair. You need the tool openssl for this (http://www.openssl.org/source/ or directly for Windows http://www.openssl.org/related/binaries.html). Install it, open "cmd" (if you are on windows), navigate to your openssl installation path and enter following lines to generate the keys for server and client:

openssl genrsa -out serverPrivateKey.pem 2048
openssl rsa -in serverPrivateKey.pem -pubout -outform DER -out serverPublicKey.der

openssl genrsa -out clientPrivateKey.pem 2048
openssl pkcs8 -topk8 -nocrypt -in clientPrivateKey.pem -outform der -out clientPrivateKey.der
openssl rsa -in clientPrivateKey.pem -pubout -outform PEM -out clientPublicKey.pem

Now in your web service java application you can import the public key for encryption:

File pubKeyFile = new File("keys/serverPublicKey.der");
byte[] buffer = new byte[(int) pubKeyFile.length()];

DataInputStream in = new DataInputStream(new FileInputStream(pubKeyFile));
in.readFully(buffer);
in.close();

KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPublicKey publicKey = (RSAPublicKey) keyFactory.generatePublic(new X509EncodedKeySpec(buffer));

and encrypt your password:

String text = password;

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encrypted = cipher.doFinal(text.getBytes());

and save it to your local file system:

FileOutputStream fos = new FileOutputStream("/tmp/encrypted");
fos.write(encrypted);
fos.flush();
fos.close();

The other way for decryption.

Import the private key:

File privKeyFile = new File("keys/clientPrivateKey.der");
byte[] buffer = new byte[(int) privKeyFile.length()];

DataInputStream in = new DataInputStream(new FileInputStream(privKeyFile));
in.readFully(buffer);
in.close();

KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPrivateKey privateKey = (RSAPrivateKey) keyFactory.generatePrivate(new PKCS8EncodedKeySpec(buffer));

read the encrypted file:

File cryptedData = new File("/tmp/encrypted");
buffer = new byte[(int) cryptedData.length()];

in = new DataInputStream(new FileInputStream(cryptedData));
in.readFully(buffer);
in.close();

and decrypt it:

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
cipher.init(Cipher.DECRYPT_MODE, privateKey);

byte[] decrypted = cipher.doFinal(buffer);

String data = new String(decrypted);

System.out.println(data);

You just have to keep your private key secret on the system where your web service is running. You can provide a web service function which provides the public key to the clients for encryption. Your clients just send the encrypted text string to the web service which decrypts it and authenticate your clients.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top