CipherUtils.java 893 B

123456789101112131415161718192021222324252627282930313233343536
  1. package com.benyun.common.utils.security;
  2. import java.security.Key;
  3. import java.security.NoSuchAlgorithmException;
  4. import javax.crypto.KeyGenerator;
  5. /**
  6. * 对称密钥密码算法工具类
  7. *
  8. * @author liugang
  9. */
  10. public class CipherUtils
  11. {
  12. /**
  13. * 生成随机秘钥
  14. *
  15. * @param keyBitSize 字节大小
  16. * @param algorithmName 算法名称
  17. * @return 创建密匙
  18. */
  19. public static Key generateNewKey(int keyBitSize, String algorithmName)
  20. {
  21. KeyGenerator kg;
  22. try
  23. {
  24. kg = KeyGenerator.getInstance(algorithmName);
  25. }
  26. catch (NoSuchAlgorithmException e)
  27. {
  28. String msg = "Unable to acquire " + algorithmName + " algorithm. This is required to function.";
  29. throw new IllegalStateException(msg, e);
  30. }
  31. kg.init(keyBitSize);
  32. return kg.generateKey();
  33. }
  34. }