Posts made in July 19th, 2010
Plain-text encryption/decryption in Java
There could be cases in which you are planning to keep some sensitive data in your database but you do not wish to have this information available to anyone (not even the DBA should be able to see the content) except the specific user. To do this you can encrypt the sensitive plan-text provided by the user. Save the encrypted message in the database and decrypt it anytime the user would like to view/edit the data. Doing this is quite easy with java. You would just need to add a few jar files to your project and a few lines of code to encrypt and decrypt the data.
The libraries you need to download are available at: http://www.jasypt.org/
The following sample code encrypts and decrypts a String. Please not that I have created an object of BasicTextEncryptor twice the reason for that is to show that you would need to set the correct password used in the encryption process to decrypt the plain-text message.
import org.jasypt.util.text.BasicTextEncryptor;
public class Secret {
public static void main(String[] args) {
String userPassword="PassWord";
String message = "Some text which is to be encrypted by the user password. Later it is decrypted to the original plain-text using the same password.";
//Encrypt
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
textEncryptor.setPassword(userPassword);
String myEncryptedText = textEncryptor.encrypt(message);
System.out.println("Encrypted text:\n"+myEncryptedText);
//Decrypt
BasicTextEncryptor textDecryptor = new BasicTextEncryptor();
textDecryptor.setPassword(userPassword);
String plainText = textDecryptor.decrypt(myEncryptedText);
System.out.println("Decrypted text:\n"+plainText);
}
}
Read More