Package cn.gmkit.sm9

Class SM9EncKey

java.lang.Object
cn.gmkit.sm9.SM9EncKey
All Implemented Interfaces:
AutoCloseable

public final class SM9EncKey extends Object implements AutoCloseable
SM9 用户解密私钥。

SM9EncMasterKey.extractKey(String) 派生,或从加密 PEM 文件导入。 解密时需要用户标识(ID),因此该对象同时持有句柄与 ID。使用完毕后应调用 close() 释放 native 资源(推荐使用 try-with-resources)。

  • Field Details

    • MAX_CIPHERTEXT_SIZE

      public static final int MAX_CIPHERTEXT_SIZE
      GmSSL 单次 SM9 解密接受的最大 DER 密文字节数。
      See Also:
  • Method Details

    • getId

      public String getId()
      返回该解密私钥对应的用户标识(ID)。
      Returns:
      用户标识
    • decrypt

      public byte[] decrypt(byte[] ciphertext)
      使用该用户解密私钥解密密文。
      Parameters:
      ciphertext - SM9 密文
      Returns:
      明文
      Throws:
      SM9Exception - ID/密文无效、密文超过上限、对象已关闭或 native 解密失败时抛出
    • exportEncryptedPrivateKeyInfoPem

      public void exportEncryptedPrivateKeyInfoPem(String password, String file)
      将该解密私钥以口令加密为 PEM 文件。
      Parameters:
      password - 加密口令
      file - 输出文件路径
      Throws:
      SM9Exception - 口令/路径为空、对象已关闭或 native 写入失败时抛出
    • importEncryptedPrivateKeyInfoPem

      public static SM9EncKey importEncryptedPrivateKeyInfoPem(String password, String file, String id)
      从加密 PEM 文件导入用户解密私钥。
      Parameters:
      password - 解密口令
      file - PEM 文件路径
      id - 该私钥对应的用户标识(解密时必填)
      Returns:
      导入的解密私钥
      Throws:
      SM9Exception - native 不可用、参数无效、口令错误或 PEM 读取失败时抛出
    • close

      public void close()
      释放底层 native 资源。重复调用安全。
      Specified by:
      close in interface AutoCloseable