CipherUtils.java 856 B

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