java vue ts aes加密登录账号
时间: 2023-11-25 14:42:57 浏览: 168
以下是使用Java和Vue.js实现AES加密登录账号的示例代码:
Java端代码:
```java
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class AESUtil {
private static final String IV_PARAMETER = "0123456789abcdef";
private static final String AES_ALGORITHM = "AES/CBC/PKCS5Padding";
private static final String AES_KEY = "1234567890123456";
public static String encrypt(String plainText) {
try {
IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes(StandardCharsets.UTF_8));
SecretKeySpec keySpec = new SecretKeySpec(AES_KEY.getBytes(StandardCharsets.UTF_8), "AES");
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);
byte[] encrypted = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encrypted);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
}
```
Vue.js端代码:
```javascript
import axios from 'axios';
import AESUtil from './AESUtil.js';
const LOGIN_API_URL = '/api/login';
export default {
methods: {
async login() {
const encrypted = AESUtil.encrypt(this.username + ':' + this.password);
const response = await axios.post(LOGIN_API_URL, null, {
headers: { 'Authorization': 'Basic ' + encrypted }
});
// Handle login response
}
},
// ...
}
```
在上面的示例中,AESUtil类提供了一个encrypt方法,用于将明文用户名和密码加密为Base64编码的字符串。在Vue.js中,我们使用axios库向服务器发送POST请求,并将加密后的身份验证信息作为Authorization头发送。服务器端可以使用相同的AES密钥和IV参数解密该信息以验证用户身份。
阅读全文