[英]Decrypt 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();
}
}
}
嘗試解密密碼時,控制台中顯示錯誤:
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)
你可以試試下面的例子。 這對您有用:請始終將 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));
}
}
從命令生成的加密字符串沒有給出所需的結果,因為它無法加密像“!”這樣的特殊字符。並給出錯誤“找不到事件”
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: 事件未找到
這是一個使用org.jasypt.util.text.AES256TextEncryptor
的示例 這是一個用於輕松執行high-strength encryption of texts
實用程序類。
這個類在內部保存了一個以這種方式配置的StandardPBEStringEncryptor
:
算法: PBEWithHMACSHA512AndAES_256
。
關鍵獲取迭代: 1000
。
使用它所需的步驟是:
pom.xml:
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
您可以使用 jasypt latest 2.1.2(with boot 2.1.1) 或jasypt-1.9.3.jar
。
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);
}
}
輸出:
加密:fureWQHrflMinY+KBOCNeJyYmQv+7Ung/IclGz3iSBYKqTNdgslADg+TMcfFI/unaqZ/P3kDGPco2jZ4vIhrFw==
解密:測試!email30#password
Spring Boot 集成:
您可以使用@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);
}
}
在任何屬性/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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.