4

要加密我使用的密码(从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);
4

1 回答 1

10

它确实有效,并且确实需要密码来加密和解密。为了简化示例,我启动了两个 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
于 2013-11-24T16:33:12.707 回答