package security;

import org.junit.Test;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.encrypt.BytesEncryptor;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.security.crypto.encrypt.TextEncryptor;
import org.springframework.security.crypto.keygen.KeyGenerators;
import org.springframework.security.crypto.password.PasswordEncoder;

//@RunWith(SpringJUnit4ClassRunner.class)
//@SpringApplicationConfiguration(classes = Application.class)
public class SpringEncrypt {
	
	private final String password = "123456";
	
	@Test
	public void testPasswordEncrypt() {
		PasswordEncoder encoder = new BCryptPasswordEncoder();
		int i = 0;
		while(i<10) {
			String hashedPassword = encoder.encode(password);
			System.out.println(hashedPassword);
			i++;
		}
	}
	
	@Test
	public void testByteEncrypt() {
		try{
		String text = "young";
		String salt = KeyGenerators.string().generateKey();
		System.out.println("the salt is :"+salt);
		
		TextEncryptor textEncryptor = Encryptors.text(password, salt);
		String encryptText = textEncryptor.encrypt(text);
		System.out.println("encrypt text is : " + encryptText);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}

@@ -12,16 +12,111 @@
 urity;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0Aimport static org.junit.Assert.assertTrue;%0A
 import o
@@ -207,75 +207,8 @@
 er;%0A
-import org.springframework.security.crypto.encrypt.BytesEncryptor;%0A
 impo
@@ -698,116 +698,312 @@
 %7B%0A%09%09
-PasswordEncoder encoder = new BCryptPasswordEncoder();%0A%09%09int i = 0;%0A%09%09while(i%3C10) %7B%0A%09%09%09String hashed
+%0A%09%09List%3CString%3E encryptPasswords = new ArrayList%3CString%3E();%0A%09%09int i = 0;%0A%09%09while(i%3C10) %7B%0A%09%09%09PasswordEncoder encoder = new BCryptPasswordEncoder();%0A%09%09%09String hashedPassword = encoder.encode(password);%0A%09%09%09encryptPasswords.add(hashedPassword);%0A%09%09%09i++;%0A%09%09%7D%0A%09%09for(String text : encryptPasswords) %7B%0A%09%09%09
 Password
  = e
@@ -1002,82 +1002,130 @@
 word
- = e
+E
 ncoder
-.
+ 
 encode
-(password);%0A%09%09%09System.out.println(hashedPassword);%0A%09%09%09i++
+r = new BCryptPasswordEncoder();%0A%09%09%09System.out.println(text);%0A%09%09%09assertTrue(encoder.matches(password, text))
 ;%0A%09%09
@@ -1159,12 +1159,12 @@
 test
-Byte
+Text
 Encr
@@ -1175,15 +1175,8 @@
 ) %7B%0A
-%09%09try%7B%0A
 %09%09St
@@ -1196,16 +1196,65 @@
 young%22;%0A
+%09%09System.out.println(%22original text is:%22 +text);%0A
 %09%09String
@@ -1409,24 +1409,62 @@
 d, salt);%0A%09%09
+%0A%09%09for(int i = 0 ; i %3C 10 ; i++) %7B%0A%09%09%09
 String encry
@@ -1501,16 +1501,17 @@
 (text);%0A
+%09
 %09%09System
@@ -1566,62 +1566,791 @@
 ;%0A%09%09
-%7D catch (Exception e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%09
+%09System.out.println(%22decrypt test is %22 + textEncryptor.decrypt(encryptText));%0A%09%09%09System.out.println(%22--------------------------------------------------%22);%0A%09%09%7D%0A%09%7D%0A%09%09%0A%09%09@Test%0A%09%09public void testQueryableEncrypt() %7B%0A%09%09%09String text = %22young%22;%0A%09%09%09System.out.println(%22original text is:%22 +text);%0A%09%09%09String salt = KeyGenerators.string().generateKey();%0A%09%09%09System.out.println(%22the salt is :%22+salt);%0A%09%09%09%0A%09%09%09TextEncryptor textEncryptor = Encryptors.queryableText(password, salt);%0A%09%09%09%0A%09%09%09for(int i = 0 ; i %3C 10 ; i++) %7B%0A%09%09%09%09String encryptText = textEncryptor.encrypt(text);%0A%09%09%09%09System.out.println(%22encrypt text is : %22 + encryptText);%0A%09%09%09%09System.out.println(%22decrypt test is %22 + textEncryptor.decrypt(encryptText));%0A%09%09%09%09System.out.println(%22--------------------------------------------------%22);%0A%09%09%09%7D
 %0A%09%7D%0A

Encrypt code change