java 明文密码加密解密,springboot明文密码加密解密
时间: 2023-10-31 16:57:25 浏览: 56
Java中常用的密码加密方法有MD5、SHA、AES等,下面分别介绍它们的使用方法。
MD5加密:
MD5是一种不可逆的加密方式,将明文密码转换成一串固定长度的字符串,一般用于存储密码的加密。Java中可以通过java.security.MessageDigest类实现MD5加密。示例代码如下:
```java
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Util {
public static String md5(String password) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] bytes = md5.digest(password.getBytes());
StringBuilder stringBuilder = new StringBuilder();
for (byte b : bytes) {
int temp = b & 0xff;
String hexString = Integer.toHexString(temp);
if (hexString.length() == 1) {
stringBuilder.append("0").append(hexString);
} else {
stringBuilder.append(hexString);
}
}
return stringBuilder.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String password = "123456";
String encryptedPassword = md5(password);
System.out.println(encryptedPassword);
}
}
```
SHA加密:
SHA也是一种不可逆的加密方式,与MD5类似,但SHA更安全。Java中可以通过java.security.MessageDigest类实现SHA加密。示例代码如下:
```java
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class SHAUtil {
public static String sha(String password) {
try {
MessageDigest sha = MessageDigest.getInstance("SHA");
byte[] bytes = sha.digest(password.getBytes());
StringBuilder stringBuilder = new StringBuilder();
for (byte b : bytes) {
int temp = b & 0xff;
String hexString = Integer.toHexString(temp);
if (hexString.length() == 1) {
stringBuilder.append("0").append(hexString);
} else {
stringBuilder.append(hexString);
}
}
return stringBuilder.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String password = "123456";
String encryptedPassword = sha(password);
System.out.println(encryptedPassword);
}
}
```
AES加密:
AES是一种对称加密方式,即加密和解密使用相同的密钥。Java中可以通过javax.crypto.Cipher类实现AES加密。示例代码如下:
```java
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
public class AESUtil {
private static final String AES = "AES";
public static String encrypt(String content, String password) {
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance(AES);
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(password.getBytes());
keyGenerator.init(128, secureRandom);
SecretKey secretKey = keyGenerator.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, AES);
Cipher cipher = Cipher.getInstance(AES);// 创建密码器
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(byteContent);
return Base64.getEncoder().encodeToString(result);// 加密
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String decrypt(String content, String password) {
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance(AES);
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(password.getBytes());
keyGenerator.init(128, secureRandom);
SecretKey secretKey = keyGenerator.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, AES);
Cipher cipher = Cipher.getInstance(AES);// 创建密码器
cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(Base64.getDecoder().decode(content));
return new String(result, "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String content = "123456";
String password = "password";
String encryptedContent = encrypt(content, password);
System.out.println("加密后:" + encryptedContent);
String decryptedContent = decrypt(encryptedContent, password);
System.out.println("解密后:" + decryptedContent);
}
}
```
Spring Boot中可以使用Spring Security提供的PasswordEncoder接口来实现密码加密和解密。示例代码如下:
```java
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
public class PasswordUtil {
public static String encode(String password) {
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
return passwordEncoder.encode(password);
}
public static boolean match(String password, String encodedPassword) {
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
return passwordEncoder.matches(password, encodedPassword);
}
public static void main(String[] args) {
String password = "123456";
String encodedPassword = encode(password);
System.out.println("加密后:" + encodedPassword);
boolean match = match(password, encodedPassword);
System.out.println("匹配结果:" + match);
}
}
```