美文网首页
使用BouncyCastle进行SM2签名

使用BouncyCastle进行SM2签名

作者: debuger | 来源:发表于2018-04-18 16:52 被阅读453次

    BouncyCastleProvider provider = new BouncyCastleProvider();

    // 添加BouncyCastle作为安全提供

    Security.addProvider(provider);

    String priKeyName = null;

    KeyStore store = KeyStore.getInstance("PKCS12", "BC");

    char passphrase[] = "123456".toCharArray();

    store.load(new FileInputStream("d:\\bctest\\sm2.pfx"), passphrase);

    if (store.aliases().hasMoreElements()) {

            priKeyName = store.aliases().nextElement();

        }

    System.out.println(priKeyName);

    Certificate cert = (Certificate) store.getCertificate(priKeyName);

     // 获取私钥

        PrivateKey prikey = (PrivateKey) store.getKey(priKeyName, passphrase);

        X509Certificate cerx509 = (X509Certificate) cert;

        System.out.println(cerx509.getSubjectDN());

     //签名

     byte[] data = "Hello World!!!".getBytes();

    Signature sig1 = Signature.getInstance("SM3withSM2","BC");

    sig1.initSign(prikey);

     sig1.update(data);

     byte[] sig = sig1.sign();

    相关文章

      网友评论

          本文标题:使用BouncyCastle进行SM2签名

          本文链接:https://www.haomeiwen.com/subject/xbfbkftx.html