Skip to content

Sm2Test 中拆开执行报错而放在一起执行正确的问题 #17

Open
@rarnu

Description

@rarnu

https://github.com/GmSSL/GmSSL-Java/blob/main/src/test/java/org/gmssl/Sm2Test.java

具体原因是因为 @BeforeTest 每次都重新生成key

@Before
  public void beforeTest(){
      sm2_key = new Sm2Key();
      sm2_key.generateKey();
  }

需要将key固化到磁盘,然后在其他的测试用例内加载,即可解决问题

class TestSm2 {

    private val sm2Key = Sm2Key(has_private_key = true)

    @Test
    fun generateKeys() {
        sm2Key.generateKey()
        sm2Key.exportPublicKeyInfoPem("publickey.pem")
        sm2Key.exportEncryptedPrivateKeyInfoPem("12345678", "privatekey.pk8")
    }

    private fun loadKeys() {
        sm2Key.importPublicKeyInfoPem("publickey.pem")
        sm2Key.importEncryptedPrivateKeyInfoPem("12345678", "privatekey.pk8")
    }

    @Test
    fun testEncrypt() {
        loadKeys()
        val testStr = "gmssl"
        val ciphertext = sm2Key.encrypt(testStr.toByteArray())
        val textHex = HexUtil.byteToHex(ciphertext)
        println(textHex)
    }

    @Test
    fun testDecrypt() {
        loadKeys()
        val textHex = "306f0221008d769703b894dbf832409e2497f87a52bd203d83e7d56616a95ff8597ee0b5fb022100befc69ee3141f53e94ddfa2155f1bd3289e5c7b6cdc828bd4b7fdd49f4360d0f04206300ec08d80eab55c7999fb5c114da7398d76d575bc20ddf881627497fd14aa404051136b536c6"
        val ciphertext = HexUtil.hexToByte(textHex)
        val plaintext = sm2Key.decrypt(ciphertext)
        val plaintextStr = String(plaintext)
        println(plaintextStr)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions