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

时间:2013-11-24 15:02:55

标签: java encryption jasypt

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

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

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

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

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

1 个答案:

答案 0 :(得分:9)

它确实有效,并且需要密码才能进行加密和解密。为了简化示例,我启动了两个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);
    }

输出:

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