Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rarnu opened this issue Feb 2, 2024 · 0 comments
Open

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

rarnu opened this issue Feb 2, 2024 · 0 comments

Comments

@rarnu
Copy link

rarnu commented Feb 2, 2024

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)
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant