Jaspyt Password Encryptor

Jaspyt Password Encryptor

Jasypt is a java library and quite familiar to do basic encryption with minimum effort and it works well. Today I will show you how to encrypt password using java class. Jasypt library already provide library and command to do the encryption and to do this you will have download its full zip from it website here. If you don’t want to download its library and looking for something handy to do the encryption please use below java class:

  • JaspytPasswordEncryptor.java:
package com.wfs.otc.common.utils;

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;

public class JaspytPasswordEncryptor {
	
	public static void main(String[] args) {
		
		String passwordToEncrypt = "Java Honk";
		
		String encryptedPassword = getEncryptedString("javahonk_decryption_key ", "PBEWithMD5AndDES", passwordToEncrypt);
		String decryptedPassword = getDecryptedString("javahonk_decryption_key ", "PBEWithMD5AndDES", encryptedPassword);
		
		System.out.println("Password to encrypt: "+passwordToEncrypt);
		System.out.println("Encrypted password: "+encryptedPassword);
		System.out.println("Decrypted password: "+decryptedPassword);
	}

	/*
	 * You could use default key and algorithm value as below:
	 * jaspytEncryptionKey:javahonk_decryption_key                     
	 * jaspytAlgorithm:PBEWithMD5AndDES
	 */
	
	public static String getEncryptedString(
			String jaspytEncryptionKey,
			String jaspytAlgorithm,
			String passwordToEncrypt){
		
		StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
		encryptor.setPassword(jaspytEncryptionKey);                     
		encryptor.setAlgorithm(jaspytAlgorithm);
		String encryptText = encryptor.encrypt(passwordToEncrypt);
		return encryptText;
	}
	
	public static String getDecryptedString(
			String jaspytEncryptionKey,
			String jaspytAlgorithm,
			String passwordToDecrypt){
		
		StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
		encryptor.setPassword(jaspytEncryptionKey);                     
		encryptor.setAlgorithm(jaspytAlgorithm);
		String decryptText = encryptor.decrypt(passwordToDecrypt);
		return decryptText;
	}
}
  • Output:

Jaspyt Password Encryptor

Leave a Reply

Your email address will not be published. Required fields are marked *