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

blu*_*sky 3 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);
Run Code Online (Sandbox Code Playgroud)

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

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

BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
String myEncryptedText = textEncryptor.encrypt(myText);
String plainText = textEncryptor.decrypt(myEncryptedText);
Run Code Online (Sandbox Code Playgroud)

use*_*494 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);
    }
Run Code Online (Sandbox Code Playgroud)

输出:

Encrypted text is: +pBbr+KOb7D6Ap/5vYJIUoHbhOruls+L
Decrypted text is: Hello World
Run Code Online (Sandbox Code Playgroud)

  • 必须要有信任 - 如果使用更复杂的公钥/私钥对来加密和解密信息,那么私钥需要存储在安全的地方.在java世界中 - 一个密钥库,甚至密钥库都受密码保护...因此在内核或程序或内存中保存密码的地方都有其优缺点. (3认同)