Why set a password using jasypt when encrypting text?

♀尐吖头ヾ 提交于 2019-12-05 06:09:47

问题


To encrypt a password I use (modified from 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 ?

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

回答1:


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

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


来源:https://stackoverflow.com/questions/20176040/why-set-a-password-using-jasypt-when-encrypting-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!