Vijay Chougule
Vijay Chougule

Reputation: 123

How to save password using encrypt and decrypt in java using gwt

Hi i am working on project in java using GWT. I want to save password using encrypt and decrypt. which is the best way to save password with encrypt and decrypt in java? Any API shall i use? any help?

Thanks in Advance

Upvotes: 4

Views: 2032

Answers (1)

m.antkowicz
m.antkowicz

Reputation: 13581

You can use GWT-Crypto library

Usage is quite simple and is presented in following code:

    //this will be used for encrypting and decrypting strings
    private TripleDesCipher encryptor;  

    ...

    //creating key for encryptor
    TripleDesKeyGenerator generator = new TripleDesKeyGenerator();
    byte[] key = generator.decodeKey("04578a8f0be3a7109d9e5e86839e3bc41654927034df92ec"); //you can pass your own string here

    //initializing encryptor with generated key
    encryptor = new TripleDesCipher();
    encryptor.setKey(key);

    ...

The example functions using the encryptor can look like:

    private String encryptString(String string)
    {
        try 
        {
            string = encryptor.encrypt( string );
        } 
        catch (DataLengthException e1) 
        {
            e1.printStackTrace();
        } 
        catch (IllegalStateException e1) 
        {
            e1.printStackTrace();
        } 
        catch (InvalidCipherTextException e1) 
        {
            e1.printStackTrace();
        }

        return string;
    }

    private String decryptString(String string)
    {
        try 
        {
            string = encryptor.decrypt(string);
        } 
        catch (DataLengthException e) 
        {
            e.printStackTrace();
        } catch (IllegalStateException e) 
        {
            e.printStackTrace();
        } catch (InvalidCipherTextException e)
        {
            e.printStackTrace();
        }

        return string;
    }

Upvotes: 2

Related Questions