Search in sources :

Example 6 with SM2

use of cn.hutool.crypto.asymmetric.SM2 in project hutool by looly.

the class SM2Test method getPublicKeyByPrivateKeyTest.

@Test
public void getPublicKeyByPrivateKeyTest() {
    // issue#I38SDP,openSSL生成的PKCS#1格式私钥
    String priKey = "MHcCAQEEIE29XqAFV/rkJbnJzCoQRJLTeAHG2TR0h9ZCWag0+ZMEoAoGCCqBHM9VAYItoUQDQgAESkOzNigIsH5ehFvr9y" + "QNQ66genyOrm+Q4umCA4aWXPeRzmcTAWSlTineiReTFN2lqor2xaulT8u3a4w3AM/F6A==";
    PrivateKey privateKey = KeyUtil.generatePrivateKey("sm2", new OpenSSHPrivateKeySpec(SecureUtil.decode(priKey)));
    final ECPrivateKeyParameters privateKeyParameters = ECKeyUtil.toPrivateParams(privateKey);
    final SM2 sm2 = new SM2(privateKeyParameters, ECKeyUtil.getPublicParams(privateKeyParameters));
    String src = "Sm2Test";
    byte[] data = sm2.encrypt(src, KeyType.PublicKey);
    byte[] sign = sm2.sign(src.getBytes(StandardCharsets.UTF_8));
    Assert.assertTrue(sm2.verify(src.getBytes(StandardCharsets.UTF_8), sign));
    byte[] dec = sm2.decrypt(data, KeyType.PrivateKey);
    Assert.assertArrayEquals(dec, src.getBytes(StandardCharsets.UTF_8));
}
Also used : ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) PrivateKey(java.security.PrivateKey) OpenSSHPrivateKeySpec(org.bouncycastle.jcajce.spec.OpenSSHPrivateKeySpec) SM2(cn.hutool.crypto.asymmetric.SM2) Test(org.junit.Test)

Example 7 with SM2

use of cn.hutool.crypto.asymmetric.SM2 in project hutool by looly.

the class SM2Test method sm2Test.

@Test
public void sm2Test() {
    final SM2 sm2 = SmUtil.sm2();
    // 获取私钥和公钥
    Assert.assertNotNull(sm2.getPrivateKey());
    Assert.assertNotNull(sm2.getPrivateKeyBase64());
    Assert.assertNotNull(sm2.getPublicKey());
    Assert.assertNotNull(sm2.getPrivateKeyBase64());
    // 公钥加密,私钥解密
    byte[] encrypt = sm2.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
    byte[] decrypt = sm2.decrypt(encrypt, KeyType.PrivateKey);
    Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
}
Also used : SM2(cn.hutool.crypto.asymmetric.SM2) Test(org.junit.Test)

Example 8 with SM2

use of cn.hutool.crypto.asymmetric.SM2 in project hutool by looly.

the class SM2Test method sm2VerifyTest.

@Test
public void sm2VerifyTest() {
    // 指定的公钥
    String publicKeyHex = "04db9629dd33ba568e9507add5df6587a0998361a03d3321948b448c653c2c1b7056434884ab6f3d1c529501f166a336e86f045cea10dffe58aa82ea13d7253763";
    // 需要加密的明文,得到明文对应的字节数组
    byte[] dataBytes = "我是一段测试aaaa".getBytes(StandardCharsets.UTF_8);
    // 签名值
    String signHex = "2881346e038d2ed706ccdd025f2b1dafa7377d5cf090134b98756fafe084dddbcdba0ab00b5348ed48025195af3f1dda29e819bb66aa9d4d088050ff148482a1";
    final SM2 sm2 = new SM2(null, publicKeyHex);
    sm2.usePlainEncoding();
    boolean verify = sm2.verify(dataBytes, HexUtil.decodeHex(signHex));
    Assert.assertTrue(verify);
}
Also used : SM2(cn.hutool.crypto.asymmetric.SM2) Test(org.junit.Test)

Example 9 with SM2

use of cn.hutool.crypto.asymmetric.SM2 in project hutool by looly.

the class SM2Test method sm2WithPointTest.

@Test
public void sm2WithPointTest() {
    String d = "FAB8BBE670FAE338C9E9382B9FB6485225C11A3ECB84C938F10F20A93B6215F0";
    String x = "9EF573019D9A03B16B0BE44FC8A5B4E8E098F56034C97B312282DD0B4810AFC3";
    String y = "CC759673ED0FC9B9DC7E6FA38F0E2B121E02654BF37EA6B63FAF2A0D6013EADF";
    String data = "434477813974bf58f94bcf760833c2b40f77a5fc360485b0b9ed1bd9682edb45";
    String id = "31323334353637383132333435363738";
    final SM2 sm2 = new SM2(d, x, y);
    final String sign = sm2.signHex(data, id);
    Assert.assertTrue(sm2.verifyHex(data, sign));
}
Also used : SM2(cn.hutool.crypto.asymmetric.SM2) Test(org.junit.Test)

Example 10 with SM2

use of cn.hutool.crypto.asymmetric.SM2 in project hutool by looly.

the class SM2Test method sm2BcdTest.

@Test
public void sm2BcdTest() {
    String text = "我是一段测试aaaa";
    final SM2 sm2 = SmUtil.sm2();
    // 公钥加密,私钥解密
    String encryptStr = sm2.encryptBcd(text, KeyType.PublicKey);
    String decryptStr = StrUtil.utf8Str(sm2.decryptFromBcd(encryptStr, KeyType.PrivateKey));
    Assert.assertEquals(text, decryptStr);
}
Also used : SM2(cn.hutool.crypto.asymmetric.SM2) Test(org.junit.Test)

Aggregations

SM2 (cn.hutool.crypto.asymmetric.SM2)19 Test (org.junit.Test)19 KeyPair (java.security.KeyPair)3 PrivateKey (java.security.PrivateKey)3 PublicKey (java.security.PublicKey)1 ECPrivateKeyParameters (org.bouncycastle.crypto.params.ECPrivateKeyParameters)1 OpenSSHPrivateKeySpec (org.bouncycastle.jcajce.spec.OpenSSHPrivateKeySpec)1 Ignore (org.junit.Ignore)1