1 Introduction
Today we introduce a Java library, Jasypt, called Java Simplified Encryption, for encryption and decryption.It enables developers to integrate encryption into their projects with minimal effort and does not require in-depth knowledge of encryption/decryption.
Referencing jar packages through Maven is as follows:
<dependency> <groupId>org.jasypt</groupId> <artifactId>jasypt</artifactId> <version>1.9.3</version> <scope>compile</scope> </dependency>
2 Simple text encryption
Text encryption is the most common requirement encountered in encryption, such as communication messages, transaction streaming, account information, etc. These are very sensitive information. In many scenarios, encrypted storage is required, and then decrypted when reading the display.The API provided by Jasypt is very convenient. Once you have set the encrypted key, you can encrypt the information. The code is as follows:
BasicTextEncryptor textEncryptor = new BasicTextEncryptor(); //Set Encryption Key textEncryptor.setPassword("MySalt"); //Encrypted Information String encryptedText = textEncryptor.encrypt("This is a secret message."); System.out.println("encryptedText:" + encryptedText); //Decrypt String decryptedText = textEncryptor.decrypt(encryptedText); System.out.println("decryptedText:" + decryptedText);
The result of code execution is:
encryptedText:S+j0ZQBxJloVi/qrEwvgnnu9tmeFMnJcmMoTY8wBhbLIdR2IFDt+Fw== decryptedText:This is a secret message.
3 One-way password encryption
User passwords are extremely sensitive information and should not be stored in the database in clear text.We need to encrypt the plain text of the password before storing it in the database.When a user logs on, password verification is required. There are two schemes: one is to decrypt the password in the database into plain text and then compare it with the password entered by the user; the other is to encrypt the password entered by the user and compare the encrypted password with the database password.
The second scheme is more reasonable, on the one hand because encryption is easier and performs better than decryption; on the other hand, it reduces the number of plaintext occurrences to ensure security.The second scheme does not require decryption at all, so all we need is one-way password encryption.The following code shows the application in this scenario:
BasicPasswordEncryptor encryptor = new BasicPasswordEncryptor(); //Encrypt Password String encryptedPassword = encryptor.encryptPassword("MyPassword"); //Check password: correct System.out.println(encryptor.checkPassword("MyPassword", encryptedPassword)); //Check password: Error System.out.println(encryptor.checkPassword("myPassword", encryptedPassword));
The result of code execution is:
true false
4 Change encryption algorithm
Jasypt provides us with flexible encryption/decryption operations that can be customized to use different algorithms for encryption and decryption.The following code example shows how to use the encryption algorithm PBEWithMD5AndTripleDES:
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); //Set Key encryptor.setPassword("MySalt"); //Set encryption algorithm encryptor.setAlgorithm("PBEWithMD5AndTripleDES"); //Encrypted Information String encryptedText = encryptor.encrypt("My secret message."); System.out.println("encryptedText:" + encryptedText); //Decrypt String decryptedText = encryptor.decrypt(encryptedText); System.out.println("decryptedText:" + decryptedText);
The result of code execution is:
encryptedText:fdNthKMZzNC5zeNO6b119njcKpqD/02EuGm2fsRs8+c= decryptedText:My secret message.
5 Multithread Decryption
Decryption is usually a more difficult process than encryption, and Jasypt provides multithreaded decryption operations that can be decrypted in parallel, providing better performance.It is generally recommended that the number of threads consistent with the number of machine processor cores be set for decryption.The code is as follows:
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor(); //Set the number of threads to 6 encryptor.setPoolSize(6); //Set Key encryptor.setPassword("MySalt"); //Set algorithm encryptor.setAlgorithm("PBEWithMD5AndTripleDES"); //encryption String encryptedText = encryptor.encrypt("My secret message."); System.out.println("encryptedText:" + encryptedText); //Decrypt String decryptedText = encryptor.decrypt(encryptedText); System.out.println("decryptedText:" + decryptedText);
The result of code execution is:
encryptedText:wuZLTiEZ52O/nD2ktecPP75LRj+1Bu3s7YyfK8XcOc0= decryptedText:My secret message.
6 Summary
This paper introduces several operations of Jasypt, an excellent Java encryption library, and hopes to help you in encrypting scenarios.In addition, Jasypt can integrate with other frameworks, such as Spring and Hibernate, which will be introduced later.
Welcome to Public Number <Pumpkin Slow>, it will be updated for you continuously.
Welcome to WeChat, the blogger, to be a little complimentary friend. Ha-ha...
Read more, share more; write more, organize more.