Java 为什么在加密文本时使用 jasypt 设置密码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20176040/
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-12 23:44:42  来源:igfitidea点击:

Why set a password using jasypt when encrypting text?

javaencryptionjasypt

提问by blue-sky

To encrypt a password I use (modified from http://www.jasypt.org/encrypting-texts.html):

要加密我使用的密码(从http://www.jasypt.org/encrypting-texts.html修改):

BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
textEncryptor.setPassword(myEncryptionPassword);
String myEncryptedText = textEncryptor.encrypt(myText);
String plainText = textEncryptor.decrypt(myEncryptedText);

Why is a password required to be set on BasicTextEncryptor ?

为什么需要在 BasicTextEncryptor 上设置密码?

I may be not understanding something fundamental here but does this not make sense, although it does not work :

我可能没有理解这里的一些基本知识,但这没有意义,尽管它不起作用:

BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
String myEncryptedText = textEncryptor.encrypt(myText);
String plainText = textEncryptor.decrypt(myEncryptedText);

采纳答案by user3020494

It does work and it does require password for encryption and decryption. To simplify the example I have initiated two sessions of StandardPBEStringEncryptor as encryptor and decryptor

它确实有效,并且确实需要密码进行加密和解密。为了简化示例,我启动了 StandardPBEStringEncryptor 的两个会话作为加密器和解密器

public static void main(String[] args) {
    StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
    encryptor.setPassword("mySecretPassword");        
    String encryptedText = encryptor.encrypt("Hello World");
    System.out.println("Encrypted text is: " + encryptedText);

    StandardPBEStringEncryptor decryptor = new StandardPBEStringEncryptor();
    decryptor.setPassword("mySecretPassword");  
    String decryptedText = decryptor.decrypt(encryptedText);
    System.out.println("Decrypted text is: " + decryptedText);
    }

output:

输出:

Encrypted text is: +pBbr+KOb7D6Ap/5vYJIUoHbhOruls+L
Decrypted text is: Hello World