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);
}
}
精彩评论