Files
TERES_web_frontend/src/utils/encryption.ts
guangfei.zhao d6ff547f20 feat(auth): implement secure authentication flow with RSA encryption
- Add jsencrypt and js-base64 dependencies for RSA encryption
- Create AuthGuard component for route protection
- Implement encryption utility for password security
- Refactor login page with tabbed interface and form validation
- Add login hooks for authentication state management
- Update user service to handle encrypted passwords
2025-10-10 18:25:20 +08:00

36 lines
883 B
TypeScript

import JSEncrypt from 'jsencrypt';
import { Base64 } from 'js-base64';
// RSA公钥
let RSA_PUBLIC_KEY = (import.meta.env.VITE_RSA_PUBLIC_KEY as string) || '';
/**
* RSA密码加密函数
* @param password 明文密码
* @returns 加密后的密码
*/
export const rsaPsw = (password: string): string => {
try {
const encrypt = new JSEncrypt();
const publicKey = RSA_PUBLIC_KEY;
console.log('publicKey', publicKey);
encrypt.setPublicKey(publicKey);
const encrypted = encrypt.encrypt(Base64.encode(password));
return encrypted || password;
} catch (error) {
console.error('RSA encryption failed:', error);
return password;
}
};
/**
* 获取RSA公钥
* @returns RSA公钥字符串
*/
export const getRSAPublicKey = (): string => {
return RSA_PUBLIC_KEY;
};
export const setRSAPublicKey = (key: string) => {
RSA_PUBLIC_KEY = key;
};