jasypt的使用和获取jdk支持的密码学算法

package util;

import java.security.Provider;
import java.security.Provider.Service;
import java.security.Security;
import java.util.Set;

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;
import org.jasypt.util.text.BasicTextEncryptor;
import org.jasypt.util.text.StrongTextEncryptor;

/**
 * jasypt的使用和获取jdk支持的密码学算法
 * [email protected]
 */
public class ConfigEncryptUtils {
	private static final StandardPBEStringEncryptor ENCRYPTOR = new StandardPBEStringEncryptor();
	static {
		EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
		// Cipher.getInstance(this.algorithm, this.provider);
		// config.setAlgorithm("PBEWithMD5AndDES");
		config.setAlgorithm("PBEWithSHA1AndDESede");
		// 自己在用的时候更改此密码
		config.setPassword("config");

		//获取jdk支持的密码学算法
		Provider[] providers = Security.getProviders();
		for (Provider provider : providers) {
			// System.out.println(provider.getInfo());
			// System.out.println(JSON.toJSONString(provider));
			Set services = provider.getServices();
			for (Service service : services) {
				System.out.println(service.getAlgorithm());
			}
		}

		ENCRYPTOR.setConfig(config);
	}

	public static void main(String[] args) {
		String plaintext = "root";
		String ciphertext = ENCRYPTOR.encrypt(plaintext);
		System.out.println(plaintext + " : " + ciphertext);
		System.out.println(ciphertext + " : " + ENCRYPTOR.decrypt(ciphertext));

		// ciphertext="royp2TvbUziD+86p1l8sxg==";
		// System.out.println(ciphertext+" : "+ENCRYPTOR.decrypt(ciphertext));
		plaintext = "sa";
		ciphertext = ENCRYPTOR.encrypt(plaintext);
		System.out.println(plaintext + " : " + ciphertext);
		test002();
	}

	private static void test002() {
		 //加密     
        BasicTextEncryptor textEncryptor = new BasicTextEncryptor();     
        textEncryptor.setPassword("password");    
        String newPassword = textEncryptor.encrypt("123456");    
        System.out.println(newPassword);    
        //解密     
        BasicTextEncryptor textEncryptor2 = new BasicTextEncryptor();     
        textEncryptor2.setPassword("password");     
        String oldPassword = textEncryptor2.decrypt(newPassword);       
        System.out.println(oldPassword);    
        System.out.println("--------------------------");  
  
        //强加密算法需要去jdk官网下载jce.jar 
        StrongTextEncryptor ste = new StrongTextEncryptor();  
        //加密  
        ste.setPassword("password");  
        String encyptedResult= ste.encrypt("123456");  
        System.out.println("encyptedResult:"+encyptedResult);  
        //解密  
        String dencyptedResult = ste.decrypt(encyptedResult);  
        System.out.println(dencyptedResult);  
		
	}
}
package com.jiepu;
import java.security.Provider;
import java.security.Security;
import java.security.Provider.Service;
import java.util.Set;

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;

/**
 *把密文放到配置文件中的时候要注意: ENC(密文)
 * 
 * @author 杨尚川
 */
public class ConfigEncryptUtils {
	
	public static void main(String[] args) {
		//test001();
		test002();
	}

	private static void test002() {
		// 加密器
		StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
		// 加密配置
		EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
		//获取jdk支持的密码学算法  
        Provider[] providers = Security.getProviders();  
        for (Provider provider : providers) {  
            // System.out.println(provider.getInfo());  
            // System.out.println(JSON.toJSONString(provider));  
            Set services = provider.getServices();  
            for (Service service : services) {  
                System.out.println(service.getAlgorithm());  
            }  
        }  
	}

	private static void test001() {
		// 加密器
		StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
		// 加密配置
		EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
		config.setAlgorithm("PBEWithMD5AndDES");
		// 自己在用的时候更改此密码
		config.setPassword("apdplat");
		// 应用加密配置 DES算法
		encryptor.setConfig(config);
		
		String plaintext = "root";
		// 加密
		String ciphertext = encryptor.encrypt(plaintext);
		
		System.out.println(plaintext + "==>" + ciphertext);
		
		String result=encryptor.decrypt(ciphertext);
		System.out.println(ciphertext + "==>" + result);
	}
}

http://my.oschina.net/apdplat/blog/405306


你可能感兴趣的:(java,信息安全与密码学)