使用BouncyCastle进行SM2签名

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签名)