Java 使用jasypt加密解密

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22067552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:22:40  来源:igfitidea点击:

encryption decrypt using jasypt

javaencryptionwindows-8java-7jasypt

提问by UdayKiran Pulipati

How to decrypt the encrypted password using jasypt.

如何使用jasypt.解密加密密码。

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();
        }
    }
}

Error is displayed in console when decrypt the password using jasypt.

使用jasypt解密密码时控制台显示错误。

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)

回答by Ash_P

You can try the below example. This will work for you: Please always keep mpCryptoPassword value very secret location, only application should be able to read that.

你可以试试下面的例子。这对您有用:请始终将 mpCryptoPassword 值保持在非常秘密的位置,只有应用程序才能读取该值。

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));
    }
}

回答by Ashutosh

generated encrypted string from command does not give desired result as it can not encrypt special chars like "!".and gives error "event not found"

从命令生成的加密字符串没有给出所需的结果,因为它无法加密像“!”这样的特殊字符。并给出错误“找不到事件”

KAD@ashutosh MINGW64 ~/Desktop

$ java -cp ~/.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: event not found

KAD@ashutosh MINGW64 ~/桌面

$ java -cp ~/.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 " 算法=PBEWithMD5AndDES

bash: !email30#password: 事件未找到

Here is an example using org.jasypt.util.text.AES256TextEncryptorThis is a utility class for easily performing high-strength encryption of texts.

这是一个使用org.jasypt.util.text.AES256TextEncryptorThis is a utility class的示例,用于轻松执行high-strength encryption of texts.

This class internally holds a StandardPBEStringEncryptorconfigured this way:

此类在内部以StandardPBEStringEncryptor这种方式配置:

  • Algorithm: PBEWithHMACSHA512AndAES_256.

  • Key obtention iterations: 1000.

  • 算法:PBEWithHMACSHA512AndAES_256

  • 关键获取迭代:1000.

The required steps to use it are:

使用它所需的步骤是:

  1. Create an instance (using new).
  2. Set a password (using setPassword(String) or setPasswordCharArray(char[])).
  3. Perform the desired encrypt(String) or decrypt(String) operations.
  1. 创建一个实例(使用 new)。
  2. 设置密码(使用 setPassword(String) 或 setPasswordCharArray(char[]))。
  3. 执行所需的加密(字符串)或解密(字符串)操作。

pom.xml:

pom.xml:

<dependency>
        <groupId>com.github.ulisesbocchio</groupId>
        <artifactId>jasypt-spring-boot-starter</artifactId>
        <version>2.1.2</version>
    </dependency>

You can use jasypt latest 2.1.2(with boot 2.1.1) or jasypt-1.9.3.jar.

您可以使用 jasypt 最新 2.1.2(带引导 2.1.1)或jasypt-1.9.3.jar.

Java Code:

Java代码:

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);
 }
}

Output:

输出:

Encrypted: fureWQHrflMinY+KBOcNeJyYmQv+7Ung/IclGz3iSBYKqTNdgslADg+TMcfFI/unaqZ/P3kDGPco2jZ4vIhrFw==

Decrypted: Test!email30#password

加密:fureWQHrflMinY+KBOCNeJyYmQv+7Ung/IclGz3iSBYKqTNdgslADg+TMcfFI/unaqZ/P3kDGPco2jZ4vIhrFw==

解密:测试!email30#password

Spring Boot Integration:

Spring Boot 集成:

You can use @EnableEncryptablePropertiesin your any configuration class or @SpringBootApplication. See example:

您可以@EnableEncryptableProperties在任何配置类或@SpringBootApplication. 见示例:

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);
    }

}

And in any properties/yml file:

在任何属性/yml 文件中:

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