MyShrMobileNewService.java 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package com.kingdee.eas.custom.server;
  2. import java.math.BigDecimal;
  3. import java.security.SecureRandom;
  4. import java.sql.SQLException;
  5. import java.sql.Timestamp;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.ArrayList;
  9. import java.util.Base64;
  10. import java.util.Date;
  11. import java.util.HashMap;
  12. import java.util.List;
  13. import java.util.Map;
  14. import javax.crypto.Cipher;
  15. import javax.crypto.KeyGenerator;
  16. import javax.crypto.SecretKey;
  17. import javax.crypto.spec.SecretKeySpec;
  18. import com.kingdee.bos.BOSException;
  19. import com.kingdee.bos.Context;
  20. import com.kingdee.bos.bsf.service.app.IHRMsfService;
  21. import com.kingdee.eas.base.permission.UserInfo;
  22. import com.kingdee.eas.common.EASBizException;
  23. import com.kingdee.eas.util.app.ContextUtil;
  24. import com.kingdee.shr.empresume.util.ErrorCodeUtil;
  25. public class MyShrMobileNewService implements IHRMsfService{
  26. private static final String ALGORITHM = "AES";
  27. private static final int KEY_SIZE = 128;
  28. public Object process(Context ctx, Map param) throws EASBizException, BOSException {
  29. String password = ctx.getAIS();
  30. UserInfo currentUserInfo = ContextUtil.getCurrentUserInfo(ctx);
  31. HashMap map = new HashMap();
  32. String data = currentUserInfo.getPerson().getId().toString()+","+System.currentTimeMillis();
  33. String encryptedData = null;
  34. //¼ÓÃÜ
  35. try {
  36. encryptedData = encrypt(data, password);
  37. } catch (Exception e) {
  38. e.printStackTrace();
  39. }
  40. System.out.println("encryptedData:"+encryptedData);
  41. map.put("encryptedData", encryptedData);
  42. ErrorCodeUtil.setSuccess(map, ctx);
  43. return map;
  44. }
  45. private String encrypt(String data, String password) throws Exception {
  46. SecretKey key = generateKey(password);
  47. Cipher cipher = Cipher.getInstance(ALGORITHM);
  48. cipher.init(Cipher.ENCRYPT_MODE, key);
  49. byte[] encryptedData = cipher.doFinal(data.getBytes());
  50. return Base64.getEncoder().encodeToString(encryptedData);
  51. }
  52. private String decrypt(String encryptedData, String password) throws Exception {
  53. SecretKey key = generateKey(password);
  54. Cipher cipher = Cipher.getInstance(ALGORITHM);
  55. cipher.init(Cipher.DECRYPT_MODE, key);
  56. byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
  57. return new String(decryptedData);
  58. }
  59. private SecretKey generateKey(String password) throws Exception {
  60. KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
  61. keyGenerator.init(KEY_SIZE, new SecureRandom(password.getBytes()));
  62. return new SecretKeySpec(keyGenerator.generateKey().getEncoded(), ALGORITHM);
  63. }
  64. }