加解密

2023-11-13 10:06:23 浏览数 (1)

加解密

1.pom依赖

代码语言:javascript复制
<dependency>
	<groupId>org.jasypt</groupId>
    <artifactId>jasypt-spring31</artifactId>
    <version>1.9.2</version>
</dependency>

2.代码

代码语言:javascript复制
import java.lang.reflect.InvocationTargetException;
import org.jasypt.util.text.BasicTextEncryptor;
import org.junit.Test;

public class JasyptTest{
    @Test
    public void jasyptTest(){
        
        BasicTextEncryptor encryptor = new BasicTextEncryptor();
        try{
            String privateKey1 = "key1";
            String privateKey2 = "key2";
            BasicTextEncryptor.class.getMethod(str1 str2,String.class)
                .invoke(encryptor, "key3");
        }catch(Exception e){
            System.out.println("init fail...");
        }
        
        String result = encryptor.encrypt("password");
        System.out.println("加密结果:" result);
        
        String pass = encryptor.decrypt(result);
        System.out.println("解密结果:" pass);
    }
}

0 人点赞