在MVC模式中用RSA加密后的密码存储在数据库中,然后用生成的私钥进行解密,再和数据库中的数据进行对比的具体代码实现
时间: 2024-06-12 18:06:52 浏览: 14
以下是一个简单的Java代码示例,用于演示如何在MVC模式中使用RSA加密密码并将其存储在数据库中,然后使用私钥解密并与数据库中的数据进行比较:
```
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.RSAKeyGenParameterSpec;
import javax.crypto.Cipher;
public class RSAUtils {
private static final String ALGORITHM = "RSA";
public static KeyPair generateKeyPair() throws Exception {
RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(2048, RSAKeyGenParameterSpec.F4);
KeyPairGenerator generator = KeyPairGenerator.getInstance(ALGORITHM);
generator.initialize(spec);
return generator.generateKeyPair();
}
public static byte[] encrypt(byte[] data, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
}
public static byte[] decrypt(byte[] data, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(data);
}
}
```
这个类包含了生成密钥对、加密和解密数据的静态方法。下面是如何在MVC模式中使用这些方法来存储和比较密码的示例:
```
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
public class UserController {
private UserDao userDao;
private PrivateKey privateKey;
public UserController() throws Exception {
KeyPair keyPair = RSAUtils.generateKeyPair();
this.privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
this.userDao = new UserDao(publicKey);
}
public void registerUser(String username, String password) throws Exception {
byte[] encryptedPassword = RSAUtils.encrypt(password.getBytes(), userDao.getPublicKey());
String encodedPassword = Base64.getEncoder().encodeToString(encryptedPassword);
userDao.saveUser(username, encodedPassword);
}
public boolean login(String username, String password) throws Exception {
String encodedPassword = userDao.getPassword(username);
byte[] encryptedPassword = Base64.getDecoder().decode(encodedPassword);
byte[] decryptedPassword = RSAUtils.decrypt(encryptedPassword, privateKey);
String decryptedPasswordStr = new String(decryptedPassword);
return decryptedPasswordStr.equals(password);
}
}
```
在这个示例中,UserController是一个MVC模式中的控制器,它依赖于一个UserDao对象和一个PrivateKey对象。UserDao对象包含了数据库连接和公钥,用于保存和获取加密后的密码。UserController的registerUser方法将原始密码加密后保存到数据库中。login方法从数据库中获取加密的密码,使用私钥解密后与用户输入的密码进行比较,如果相同则返回true。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)