sonarqube问题

vx6bjr1n  于 2021-07-12  发布在  Java
关注(0)|答案(2)|浏览(603)

我有一些我们运行的旧代码。我不是java和异常后代等方面的Maven,所以我希望有人能够帮助我解决这个问题,因为sonar说它是一个拦截器。
代码如下:

  1. package xxx;
  2. import java.io.UnsupportedEncodingException;
  3. import java.security.InvalidKeyException;
  4. import java.security.Key;
  5. import java.security.MessageDigest;
  6. import java.security.NoSuchAlgorithmException;
  7. import java.util.Arrays;
  8. import javax.crypto.BadPaddingException;
  9. import javax.crypto.Cipher;
  10. import javax.crypto.IllegalBlockSizeException;
  11. import javax.crypto.KeyGenerator;
  12. import javax.crypto.NoSuchPaddingException;
  13. import javax.crypto.spec.SecretKeySpec;
  14. public class Encryptor {
  15. private static final String ALGORITHM = "AES";
  16. private static final String defaultSecretKey = "xxx";
  17. private Key secretKeySpec;
  18. public Encryptor() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException,
  19. UnsupportedEncodingException {
  20. this(null);
  21. }
  22. public Encryptor(String secretKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
  23. UnsupportedEncodingException {
  24. this.secretKeySpec = generateKey(secretKey);
  25. }
  26. public String encrypt(String plainText) throws InvalidKeyException, NoSuchAlgorithmException,
  27. NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
  28. Cipher cipher = Cipher.getInstance(ALGORITHM);
  29. cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
  30. byte[] encrypted = cipher.doFinal(plainText.getBytes("UTF-8"));
  31. return asHexString(encrypted);
  32. }
  33. public String decrypt(String encryptedString) throws InvalidKeyException, IllegalBlockSizeException,
  34. BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
  35. Cipher cipher = Cipher.getInstance(ALGORITHM);
  36. cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
  37. byte[] original = cipher.doFinal(toByteArray(encryptedString));
  38. return new String(original);
  39. }
  40. private Key generateKey(String secretKey) throws UnsupportedEncodingException, NoSuchAlgorithmException {
  41. if (secretKey == null) {
  42. secretKey = defaultSecretKey;
  43. }
  44. byte[] key = (secretKey).getBytes("UTF-8");
  45. MessageDigest sha = MessageDigest.getInstance("SHA-256");
  46. key = sha.digest(key);
  47. key = Arrays.copyOf(key, 16); // use only the first 128 bit
  48. KeyGenerator kgen = KeyGenerator.getInstance("AES");
  49. kgen.init(256); // 192 and 256 bits may not be available
  50. return new SecretKeySpec(key, ALGORITHM);
  51. }
  52. private final String asHexString(byte buf[]) {
  53. StringBuffer strbuf = new StringBuffer(buf.length * 2);
  54. int i;
  55. for (i = 0; i < buf.length; i++) {
  56. if (((int) buf[i] & 0xff) < 0x10) {
  57. strbuf.append("0");
  58. }
  59. strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
  60. }
  61. return strbuf.toString();
  62. }
  63. private final byte[] toByteArray(String hexString) {
  64. int arrLength = hexString.length() >> 1;
  65. byte buf[] = new byte[arrLength];
  66. for (int ii = 0; ii < arrLength; ii++) {
  67. int index = ii << 1;
  68. String l_digit = hexString.substring(index, index + 2);
  69. buf[ii] = (byte) Integer.parseInt(l_digit, 16);
  70. }
  71. return buf;
  72. }
  73. public static void main(String[] args) throws Exception {
  74. if (args.length == 1) {
  75. String plainText = args[0];
  76. Encryptor aes = new Encryptor();
  77. String encryptedString = aes.encrypt(plainText);
  78. //this line only ensures that decryption works
  79. String decryptedString = aes.decrypt(encryptedString);
  80. System.out.println("Original Password: " + plainText + " and Encrypted Password: " + encryptedString);
  81. } else {
  82. System.out.println("USAGE: java AES string-to-encrypt");
  83. }
  84. }
  85. }

问题就在这条线上:

  1. public static void main(String[] args) throws Exception {

声纳说删除这个条款
有人知道怎么解决这个问题吗?为什么会这样?
谢谢。
m。

ikfrs5lh

ikfrs5lh1#

使用最小公分母,或者更具体的异常类(exception class)始终是一种很好的方法,该异常类提供了对其所有子代的最佳抽象。
考虑以下方法声明:

  1. public String encrypt(String plainText)
  2. throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException,
  3. UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException,
  4. UnsupportedEncodingException {
  5. // body
  6. }

对所有这些例外情况的仔细检查表明,它们都扩展了 GeneralSecurityException . 因此,可以将上述代码重构为:

  1. public String encrypt(String plainText) throws GeneralSecurityException,
  2. UnsupportedEncodingException {
  3. // body
  4. }

唯一的例外,它不继承 GeneralSecurityException ,是 UnsupportedEncodingException 所以你必须明确声明它。
从客户端考虑:您更愿意使用哪个版本?

  1. try {
  2. String encrypted = cipher.encrypt("Test");
  3. } catch(InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException
  4. | UnsupportedEncodingException | IllegalBlockSizeException | BadPaddingException e) {
  5. // cannot encrypt
  6. } catch(UnsupportedEncodingException e) {
  7. // wrong encoding
  8. }
  1. try {
  2. String encrypted = cipher.encrypt("Test");
  3. } catch(GeneralSecurityException e) {
  4. // cannot encrypt
  5. } catch(UnsupportedEncodingException e) {
  6. // wrong encoding
  7. }
展开查看全部
nmpmafwu

nmpmafwu2#

感谢所有评论:
这就是解决方案(删除泛型异常并添加显式异常):

  1. public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException {

相关问题