开发者

Text encryption in Console

开发者 https://www.devze.com 2023-03-03 23:24 出处:网络
How to immediately encrypt any text while typing in the Java Console?I am talking about input and encryption happening concurrently(Just like how we type our passwords,which gets encrypted immediately

How to immediately encrypt any text while typing in the Java Console? I am talking about input and encryption happening concurrently(Just like how we type our passwords,which gets encrypted immediately,is that possible on console?). Or c开发者_开发知识库an anybody tell me which is the simple and the best way to encrypt the text while typing itself?


If you want to encrypt an OutputStream, maybe you can use a CipherOutputStream. An example is here.


Ok..Considering it is not possible to type a text and the encryption of the same text to go hand-in-hand IN-THE-CONSOLE, i learnt that this was the simplest way of encrypting and decrypting a text (of course,it needs external jar files) -

import org.jasypt.util.text.BasicTextEncryptor;

public class EncryptDecrypt {

    public static void main(String[] args) {

        String Password="password123";
        String text = "hello.I just encrypted this text and immediately decrypted it";

        //Encrypt
        BasicTextEncryptor btenc = new BasicTextEncryptor();
        btenc.setPassword(userPassword);
        String myEncryptedText = btenc.encrypt(text);
        System.out.println("Encrypted text:\n"+myEncryptedText);

        //Decrypt
        BasicTextEncryptor btdec = new BasicTextEncryptor();
        btdec.setPassword(userPassword);
        String plainText = btdec.decrypt(myEncryptedText);

        System.out.println("Decrypted text:\n"+plainText);
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号