[Java] How to use the excellent library library Jasypt encryption to protect your sensitive information?

1 Introduction

Today we introduce a Java library - Jasypt , full name Java Simplified Encryption, for encryption and decryption. It allows developers to work with a minimum of cost and the encryption integrated into the project, and does not require encryption / decryption depth understanding.

By Mavenreference jar package 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 Encryption is the most frequently encountered needs, such as information communication, transaction flow, account information, etc., these are very sensitive information, many scenarios require encryption to store and read the show again when decrypting. API Jasypt provide very convenient, after setting encrypted key, the encrypted information can be, as follows:

BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
//设置加密密钥
textEncryptor.setPassword("MySalt");
//加密信息
String encryptedText = textEncryptor.encrypt("This is a secret message.");
System.out.println("encryptedText:" + encryptedText);
//解密
String decryptedText = textEncryptor.decrypt(encryptedText);
System.out.println("decryptedText:" + decryptedText);

The results of the implementation of the code is:

encryptedText:S+j0ZQBxJloVi/qrEwvgnnu9tmeFMnJcmMoTY8wBhbLIdR2IFDt+Fw==
decryptedText:This is a secret message.

3-way password encryption

User passwords are extremely sensitive information that should not be in plain text passwords stored in the database. We need to put the password in plain text is encrypted, then the ciphertext is stored in the database. When the user login, password verification is required, there are two solutions: One solution is to database ciphertext decryption into plain text, and then compared with the password entered by the user; Another solution is the password entered by the user encryption, ciphertext encrypted with the encrypted database comparison.

The second option is more reasonable, on the one hand because the encryption is easier than deciphering, better performance; the other is to reduce the number that appears in plain text, to ensure safety. The second option completely without decryption, we need only one way to encrypt passwords before it. The following code shows the application in this scenario:

BasicPasswordEncryptor encryptor = new BasicPasswordEncryptor();
//加密密码
String encryptedPassword = encryptor.encryptPassword("MyPassword");
//检查密码:正确
System.out.println(encryptor.checkPassword("MyPassword", encryptedPassword));
//检查密码:错误
System.out.println(encryptor.checkPassword("myPassword", encryptedPassword));

The results of the implementation of the code is:

true
false

4 to change the encryption algorithm

Jasypt provides us with flexible encryption / decryption operations can be defined from the use of different algorithms for encryption and decryption. The following code example shows how to use the encryption algorithm PBEWithMD5AndTripleDES :

StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
//设置密钥
encryptor.setPassword("MySalt");
//设置加密算法
encryptor.setAlgorithm("PBEWithMD5AndTripleDES");
//加密信息
String encryptedText = encryptor.encrypt("My secret message.");
System.out.println("encryptedText:" + encryptedText);
//解密
String decryptedText = encryptor.decrypt(encryptedText);
System.out.println("decryptedText:" + decryptedText);

The results of the implementation of the code is:

encryptedText:fdNthKMZzNC5zeNO6b119njcKpqD/02EuGm2fsRs8+c=
decryptedText:My secret message.

5 Multi-threading decryption

Decryption process is usually more difficult than the encryption, decryption Jasypt provided multithreaded operation, parallel decryption, which can provide better performance. General recommendations can set the number of threads with the same number of processor cores in the machine decrypt. code show as below:

PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
//设置线程数为6
encryptor.setPoolSize(6);
//设置密钥
encryptor.setPassword("MySalt");
//设置算法
encryptor.setAlgorithm("PBEWithMD5AndTripleDES");
//加密
String encryptedText = encryptor.encrypt("My secret message.");
System.out.println("encryptedText:" + encryptedText);
//解密
String decryptedText = encryptor.decrypt(encryptedText);
System.out.println("decryptedText:" + decryptedText);

Code execution results:

encryptedText:wuZLTiEZ52O/nD2ktecPP75LRj+1Bu3s7YyfK8XcOc0=
decryptedText:My secret message.

Summary 6

This article describes several operating a good Java encryption library Jasypt, and we want to help in encryption scene. In addition, Jasypt can be integrated with other frameworks such as Spring and Hibernate , will introduce later.


Welcome to public concern number < pumpkin slow, said >, you will continue to update ...

file

Welcome Gabor main micro-letters, make a point of the Friends of praise, ha ha ...

file

More books, more sharing; and more writing, more than finishing.

Guess you like

Origin www.cnblogs.com/larrydpk/p/12026512.html