java.security.KeyPairGenerator:无法设置 .setDigests() 和无效的私钥

问题描述

我目前正在使用 Nativescript-Vue 开发一个 Android 应用程序,我想在其中生成一个私钥来签署有效负载

<!DOCTYPE html>
<html>
  <body onload="showPage(0)">
    <table id="my-table">
      <thead>
        <tr>
          <th>col1</th>
          <th>col2</th>
        </tr>    
      </thead>
      <tbody>
        <tr>
          <td>col43</td>
          <td>col546</td>

        </tr>
        <tr>
          <td>col143</td>
          <td>col1546</td>
        </tr>

        <tr>
          <td>col434534</td>
          <td>col5463453</td>
        </tr>
        <tr>
          <td>col1434533</td>
          <td>col1546345</td>
        </tr>    
      </tbody>
    </table>

    <button onclick="showPage(page - 1)">Prev</button>
    <button onclick="showPage(page + 1)">Next</button>  
  </body>
</html>

如果我保留 const KPGen = java.security.KeyPairGenerator; const keyP = android.security.keystore.KeyProperties; const kpg = KPGen.getInstance(keyP.KEY_ALGORITHM_EC,"AndroidKeyStore"); const builder = new android.security.keystore.KeyGenParameterSpec.Builder( "key1",keyP.PURPOSE_SIGN) .setAlgorithmParameterSpec(new java.security.spec.ECGenParameterSpec("secp256r1")) .setDigests(keyP.DIGEST_SHA256) kpg.initialize(builder.build()); const kp = kpg.generateKeyPair(); ,应用程序将抛出错误.setDigests(keyP.DIGEST_SHA256)

但是,如果我注释掉 Error: java.lang.Exception: Failed resolving method setDigests on class android.security.keystore.KeyGenParameterSpec$Builder 行,那么当我尝试初始化签名上的签名时我会得到一个 .setDigests(keyP.DIGEST_SHA256)

Error: java.security.InvalidKeyException: Keystore operation Failed

我不确定这两个问题是否相关,但有人知道目前出了什么问题吗?

我正在关注以下示例:https://developer.android.com/reference/android/security/keystore/KeyGenParameterSpec

解决方法

如果你去 Java 安全文档,它说函数 setDigests() 接受一个摘要类型的数组,而不是逗号分隔的参数。您发布的指南中给出的示例中有错误。