jasypt java加密包




import org.jasypt.digest.StandardStringDigester;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.util.password.BasicPasswordEncryptor;
import org.jasypt.util.password.StrongPasswordEncryptor;

public class PasswordEncrypt {

private BasicPasswordEncryptor basicPasswordEncryptor = null;

private StrongPasswordEncryptor strongPasswordEncryptor = null;

private StandardPBEStringEncryptor standardPBEStringEncryptor = null;

private StandardStringDigester standardStringDigester = null;

public PasswordEncrypt() {
basicPasswordEncryptor = new BasicPasswordEncryptor();
}

public PasswordEncrypt(String strategy) {
if ("basic".equals(strategy)) {
basicPasswordEncryptor = new BasicPasswordEncryptor();
}
else if ("strong".equals(strategy)) {
strongPasswordEncryptor = new StrongPasswordEncryptor();
}
else if ("pbe".equals(strategy)) {
standardPBEStringEncryptor = new StandardPBEStringEncryptor();
}
else if ("digester".equals(strategy)) {
standardStringDigester = new StandardStringDigester();
standardStringDigester.setAlgorithm("SHA-1");
standardStringDigester.setIterations(50000);
}
}

public String basicEncryptPassword(String userPassword) {
return basicPasswordEncryptor.encryptPassword(userPassword);
}

public String strongEncryptPassword(String userPassword) {
return strongPasswordEncryptor.encryptPassword(userPassword);
}

public String pbeEncryptPassword(String userPassword) {
return standardPBEStringEncryptor.encrypt(userPassword);
}

public String digesterEncryptPassword(String userPassword) {
return standardStringDigester.digest(userPassword);
}

public boolean checkBasicEncryptPassword(String inputPassword, String encryptPassword) {
boolean result = false;

if (basicPasswordEncryptor.checkPassword(inputPassword, encryptPassword)) {
result = true;
}

return result;
}

public boolean checkStrongEncryptPassword(String inputPassword, String encryptPassword) {
boolean result = false;

if (strongPasswordEncryptor.checkPassword(inputPassword, encryptPassword)) {
result = true;
}

return result;
}

public boolean checkPBEEncryptPassword(String inputPassword, String encryptPassword) {
boolean result = false;


if (pbeEncryptPassword(inputPassword).equals(encryptPassword)) {
result = true;
}

return result;
}

public boolean checkStandardStringDigester(String inputPassword, String encryptPassword) {
boolean result = false;

if (standardStringDigester.matches(inputPassword,encryptPassword)) {
result = true;
}

return result;
}
public static void main(String[] args) {
PasswordEncrypt PasswordEncrypt = new PasswordEncrypt("strong");
String encryptPassword = PasswordEncrypt.strongEncryptPassword("welcome1");
System.out.println(encryptPassword);

boolean result = PasswordEncrypt.checkStrongEncryptPassword("welco1me1", encryptPassword);
System.out.println(result);
}
}

你可能感兴趣的:(java,加密方法)