rsaEncryption.ts 953 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * RSA加密工具
  3. *
  4. * 使用JSEncrypt库进行RSA加密
  5. */
  6. import JSEncrypt from 'jsencrypt';
  7. /**
  8. * 使用RSA公钥加密数据
  9. *
  10. * @param data 要加密的明文数据
  11. * @param publicKey PEM格式的RSA公钥
  12. * @returns Base64编码的加密数据
  13. */
  14. export function rsaEncrypt(data: string, publicKey: string): string {
  15. const encrypt = new JSEncrypt();
  16. encrypt.setPublicKey(publicKey);
  17. const encrypted = encrypt.encrypt(data);
  18. if (!encrypted) {
  19. throw new Error('RSA加密失败');
  20. }
  21. return encrypted;
  22. }
  23. /**
  24. * 加密实名认证数据
  25. *
  26. * @param realName 真实姓名
  27. * @param idCard 身份证号
  28. * @param publicKey RSA公钥
  29. * @returns 加密后的数据
  30. */
  31. export function encryptVerificationData(
  32. realName: string,
  33. idCard: string,
  34. publicKey: string
  35. ): string {
  36. // 组合数据:real_name|id_card
  37. const combinedData = `${realName}|${idCard}`;
  38. return rsaEncrypt(combinedData, publicKey);
  39. }