首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Jasypt解密

使用Jasypt解密
EN

Stack Overflow用户
提问于 2014-02-27 11:45:54
回答 2查看 40.9K关注 0票数 11

如何使用Jasypt库解密加密密码?

代码语言:javascript
复制
package com.uk.mysqlmaven.jsf.test;

import org.jasypt.util.password.StrongPasswordEncryptor;
import org.jasypt.util.text.StrongTextEncryptor;


public class PasswordEncryptionDecryptionUsingJASYPT {
    public static void main(String[] args) {
        try {
            String password = "password";
            StrongPasswordEncryptor encryptor = new StrongPasswordEncryptor();
            String encryptedPassword = encryptor.encryptPassword(password);
            if (encryptor.checkPassword(password, encryptedPassword)) {
                //correct
                System.out.println("Encrypted: "+ encryptedPassword);
            } else {
                //bad again
                System.out.println("Error: ");
            }
            StrongTextEncryptor textEncryptor = new StrongTextEncryptor();
            textEncryptor.setPassword(encryptedPassword);
            String decryptedPassword = textEncryptor.decrypt(encryptedPassword);
            System.out.println("Decrypted: "+ decryptedPassword);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

当试图解密密码时,控制台中会显示错误:

代码语言:javascript
复制
Encrypted: JIOYXNa1+3+QefY2S7sas7LmhyOuDQcG8TTsQoTkqj0OtobCvwAFHXxoTr7z6HuP
org.jasypt.exceptions.EncryptionOperationNotPossibleException: Encryption raised an exception. A possible cause is you are using strong encryption algorithms and you have not installed the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files in this Java Virtual Machine
    at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.handleInvalidKeyException(StandardPBEByteEncryptor.java:999)
    at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.decrypt(StandardPBEByteEncryptor.java:976)
    at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.decrypt(StandardPBEStringEncryptor.java:725)
    at org.jasypt.util.text.StrongTextEncryptor.decrypt(StrongTextEncryptor.java:118)
    at com.uk.mysqlmaven.jsf.test.PasswordEncryptionDecryptionUsingJASYPT.main(PasswordEncryptionDecryptionUsingJASYPT.java:22)
EN

回答 2

Stack Overflow用户

发布于 2016-09-05 12:42:36

您可以尝试下面的示例。这将为您工作:请始终保持mpCryptoPassword值非常秘密的位置,只有应用程序应该能够阅读。

代码语言:javascript
复制
public class EncryptionDecryptionUsingJASYPT {

    private static String mpCryptoPassword = "BornToFight";

    public static void main(String[] args) {
        String value = "Original Text: Eclipse";

        System.out.println("Original Value : "+value);
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        encryptor.setPassword(mpCryptoPassword);
        String encryptedPassword = encryptor.encrypt(value);
        System.out.println(encryptedPassword);

        StandardPBEStringEncryptor decryptor = new StandardPBEStringEncryptor();
        decryptor.setPassword(mpCryptoPassword);
        System.out.println(decryptor.decrypt(encryptedPassword));
    }
}
票数 14
EN

Stack Overflow用户

发布于 2019-10-29 06:02:16

从命令生成的加密字符串不能给出所需的结果,因为它不能加密特殊字符,例如"!".and给出的错误“事件未找到”。

KAD@ashutosh MINGW64 ~/桌面 $ java ~/.m2/repository/org/jasypt/jasypt/1.9.3/jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input=“Test!email30#password password="some_salt”algorithm=PBEWithMD5AndDES“ bash:!email30#password:找不到事件

下面是一个使用org.jasypt.util.text.AES256TextEncryptor的示例,这是一个易于执行high-strength encryption of texts的实用类。

这个类内部保存一个以这种方式配置的StandardPBEStringEncryptor

  • 算法:PBEWithHMACSHA512AndAES_256.
  • 密钥获取迭代:1000

使用它所需的步骤是:

  1. 创建一个实例(使用new)。
  2. 设置密码(使用setPassword(字符串)或setPasswordCharArray(char[]))。
  3. 执行所需的加密(字符串)或解密(字符串)操作。

pom.xml:

代码语言:javascript
复制
<dependency>
        <groupId>com.github.ulisesbocchio</groupId>
        <artifactId>jasypt-spring-boot-starter</artifactId>
        <version>2.1.2</version>
    </dependency>

您可以使用JasypUpdate2.1.2(带引导2.1.1)或jasypt-1.9.3.jar

Java代码:

代码语言:javascript
复制
import org.jasypt.util.text.AES256TextEncryptor;
import java.security.NoSuchAlgorithmException;

public class JasyptPasswordEcryptor {
 public static void main(String[] args) throws NoSuchAlgorithmException {

    String password = "Test!email30#password";

    AES256TextEncryptor encryptor = new AES256TextEncryptor();
    encryptor.setPassword("some_salt");
    String myEncryptedText = encryptor.encrypt(password);
    System.out.println("Encrypted: "+myEncryptedText);

    String plainText = encryptor.decrypt(myEncryptedText);
    System.out.println("Decrypted: "+plainText);
 }
}

输出:

加密: fureWQHrflMinY+KBOcNeJyYmQv+7Ung/IclGz3iSBYKqTNdgslADg+TMcfFI/unaqZ/P3kDGPco2jZ4vIhrFw== 解密:测试!email30#password

Spring启动集成:

您可以在任意配置类或@EnableEncryptableProperties中使用@SpringBootApplication。见示例:

代码语言:javascript
复制
import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableEncryptableProperties
@SpringBootApplication
@ComponentScan(basePackages = {"com.company"})
@EntityScan(basePackages = {"com.company.persistence.entities"})
@EnableJpaRepositories(value = {"com.company.persistence.repository"})
@EnableTransactionManagement
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

和任何属性/yml文件:

代码语言:javascript
复制
email:
    password:
        # DO-NOT-USE/REMOVE THIS
        plain: 'Test!email30#password'
        # use this encrypted one
        encrypted: ENC(fureWQHrflMinY+KBOcNeJyYmQv+7Ung/IclGz3iSBYKqTNdgslADg+TMcfFI/unaqZ/P3kDGPco2jZ4vIhrFw==)

jasypt:
    encryptor:
        password: some_salt
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22067552

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档