• 【密码学】Java实现DH函数时出现“Unsupported secret key algorithm: AES“错误


    问题描述

    jdk版本:8

    使用DH和AES算法,实现密钥的交换和加密,测试时报错

    1. java.security.NoSuchAlgorithmException: Unsupported secret key algorithm: AES
    2. at com.sun.crypto.provider.DHKeyAgreement.engineGenerateSecret(DHKeyAgreement.java:387)
    3. at javax.crypto.KeyAgreement.generateSecret(KeyAgreement.java:648)
    4. at com.example.jcrypt.service.impl.DHDemoImpl.getSecretKey(DHDemoImpl.java:162)
    5. at com.example.jcrypt.service.DHDemoTest.test(DHDemoTest.java:58)
    6. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    7. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    8. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    9. at java.lang.reflect.Method.invoke(Method.java:498)
    10. at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    11. at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    12. at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    13. at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    14. at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
    15. at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
    16. at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    17. at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    18. at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    19. at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    20. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    21. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    22. at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    23. at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    24. at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    25. at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    26. at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    27. at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    28. at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    29. at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    30. at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    31. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    32. at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    33. at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    34. at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    35. at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
    36. at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)

    原因分析

    今天在使用Java实现DH密钥交换函数时出现了 "java.security.NoSuchAlgorithmException: Unsupported secret key algorithm: AES"这样的错误。出错的原因在于:DH密钥长度至少为512位,而AES算法密钥没有这么长,密钥长度不一致引起的。

    解决方式

    在idea 的Edit Configurations... 中,添加如下配置

    -Djdk.crypto.KeyAgreement.legacyKDF=true

    如下图所示

    参考文章

    https://blog.csdn.net/ambtious_/article/details/102384296

  • 相关阅读:
    【网络安全】-网络安全的分类详解
    虚拟机安装zookeeper集群
    面试理论篇三
    代码随想录算法训练营第三十五天|860.柠檬水找零 406.根据身高重建队列 452. 用最少数量的箭引爆气球
    Linux权限管理— sudo授权
    logstash清除sincedb_path上传记录,重传日志数据
    【LeetCode】【剑指offer】【从上到下打印二叉树(一)】
    【小程序】导航栏和内容页面联动效果实现
    SpringBoot框架——8.MybatisPlus常见用法(常用注解+内置方法+分页查询)
    电商美工设计有哪些基本规范原则 优漫动游
  • 原文地址:https://blog.csdn.net/xiaoxiao_su123/article/details/133742678