package com.ssm.core.util;

import com.ssm.core.utils.AESUtils;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEByteEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.jasypt.util.text.BasicTextEncryptor;

import java.math.BigDecimal;
import java.util.Random;

/**
 * @Created with Intellij IDEA
 * @Author :
 * @Date : 2018/5/18 - 10:37
 * @Copyright (C), 2018-2018
 * @Descripition : Jasypt安全框架加密类工具包
 */
public class JasyptUtils {

    /**
     * Jasypt生成加密结果
     *
     * @param password 配置文件中设定的加密密码 jasypt.encryptor.password
     * @param value    待加密值
     * @return
     */
    public static String encryptPwd(String password, String value) {
        PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor();
        encryptOr.setConfig(cryptOr(password));
        String result = encryptOr.encrypt(value);
        return result;
    }

    /**
     * 解密
     *
     * @param password 配置文件中设定的加密密码 jasypt.encryptor.password
     * @param value    待解密密文
     * @return
     */
    public static String decyptPwd(String password, String value) {
        PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor();
        encryptOr.setConfig(cryptOr(password));
        String result = encryptOr.decrypt(value);
        return result;
    }

    /**
     * @param password salt
     * @return
     */
    public static SimpleStringPBEConfig cryptOr(String password) {
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(password);
        config.setAlgorithm(StandardPBEByteEncryptor.DEFAULT_ALGORITHM);
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName(null);
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setStringOutputType("base64");
        return config;
    }

    public static void main(String[] args) throws Exception {
//        String docnum = "SYA00620230814001";
//        String substring = docnum.substring(docnum.length() - 11, docnum.length() - 5);
//        System.out.println("substring = " + substring);
        //spring.datasource.url=jdbc:mysql://127.0.0.1:3306/dcmy-newedition?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8&allowMultiQueries=true
        //spring.datasource.username=ENC(O1BQh3ko5C6HZslX7Mi6Yw==)
        //spring.datasource.password=ENC(jrlD4uGPqs4isJc70wkGgdX8IeVhqlaigr9NmoZU/gnB+u0J6mUBZZEI2zPu1E9rvkTEjt/EsIRzGpLHy88080PchNTKTTRxX7UwIeWgDAsMxW/wwdUBCct8a2NvAw27hVPFuD8KXSD5uEa9eQMl8Ws+t7vWnfDufO+cA9NVgZe+ngaRITTegg/XKCZkGo/JtjQY54UKF0+EtiCGuBTYbP5L+xeI8u9K7pcA1zEtVwoAoS5MkTlVtqV8FycWJKAc)
        //spring.datasource.username=ENC(O1BQh3ko5C6HZslX7Mi6Yw==)
        //spring.datasource.password=ENC(jrlD4uGPqs4isJc70wkGgdX8IeVhqlaigr9NmoZU/gnB+u0J6mUBZZEI2zPu1E9rvkTEjt/EsIRzGpLHy88080PchNTKTTRxX7UwIeWgDAsMxW/wwdUBCct8a2NvAw27hVPFuD8KXSD5uEa9eQMl8Ws+t7vWnfDufO+cA9NVgZe+ngaRITTegg/XKCZkGo/JtjQY54UKF0+EtiCGuBTYbP5L+xeI8u9K7pcA1zEtVwoAoS5MkTlVtqV8FycWJKAc)
//        System.out.println(AESUtils.encrypt("13927759532"));
//        System.out.println(AESUtils.encrypt("1392779532"));
//        System.out.println(AESUtils.encrypt("root123456"));
//        System.out.println(AESUtils.decrypt("993f014869ca9314b5edcb80eca2eb1f"));
//        System.out.println(AESUtils.encrypt("13662780023"));
////        'spring.datasource.username=ENC(O1BQh3ko5C6HZslX7Mi6Yw==)\nspring.datasource.password=ENC(TJU64ImcZASn8YLoEFQLOA==)';
//        System.out.println(AESUtils.encrypt("13543507408"));
        BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
        //加密所需的salt
        textEncryptor.setPassword("wwqYunxi");
        System.out.println("用户===》"+textEncryptor.decrypt("qaa0BOUMC69RlSjW1EBYjg=="));
        System.out.println("密码===》"+textEncryptor.decrypt("5l0HQCNMy2H5gLhLP3NwcI/F4Q7uI1d+qE14OoS2LdQtxV0VPgvINj0Oh69MPW8SEt/KLbld+yQ="));
        System.out.println("用户===》"+textEncryptor.encrypt("root"));
        System.out.println("密码===》"+textEncryptor.encrypt("KKOwmQr4JjY6eFPeLZ/dpzxESv+Bb8uQTNYCKyPV"));

 //org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'fukajinSchedule': Unsatisfied dependency expressed through field 'ssmRechargeService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ssmRechargeServiceImpl': Unsatisfied dependency expressed through field 'ssmStoreMemberService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ssmStoreMemberServiceImpl': Unsatisfied dependency expressed through field 'quartzManage'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'quartzManage': Unsatisfied dependency expressed through field 'scheduler'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scheduler' defined in class path resource [com/ssm/config/QuartzConfig.class]: Invocation of init method failed; nested exception is org.quartz.JobPersistenceException: Failed to obtain DB connection from data source 'quartzDS': java.sql.SQLException: Connections could not be acquired from the underlying database! [See nested exception: java.sql.SQLException: Connections could not be acquired from the underlying database!]
        //        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
        //        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
        //        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
        //        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1378)
        //        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575)
        //        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
        //        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
    }

}