Monday 28 March 2011

BasicTextEncryptor


This example is showing you how to use the Jasypt API to write a simple code to do string encryption and decryption. In this example we are going to use theBasicTextEncryptor class which use the PBEWithMD5AndDES algorithm. This class is an implementation of the TextEncryoptor interface.
You can download the library from their website, it's already included with the dependency libraries required by Jasypt such as the commons-codec and commons-lang.
package encrypt;

import org.jasypt.util.text.BasicTextEncryptor;

/**
 *
 * @author Prasobh.K
 */
public class dfas {

    public static void main(String[] args) throws Exception {

        String text = "i am Prasobh.K";
        System.out.println("Text      = " + text);
        BasicTextEncryptor bte = new BasicTextEncryptor();
        bte.setPassword("anystring");

        String encrypted = bte.encrypt(text);
        System.out.println("Encrypted = " + encrypted);

        String decrypted = bte.decrypt(encrypted);
        System.out.println("Original  = " + decrypted);

    }
}
A result produced by the code above are:
Text      = i am Prasobh.K
Encrypted = WZRnj/nvhHZDhs0jLpU0SpcNSm052X9N
Original  = i am Prasobh.K


No comments:

Post a Comment