Why set a password using jasypt when encrypting te

2019-04-12 12:58发布

问题:

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