use of com.github.zhenwei.provider.jce.provider.WeGooProvider in project LinLong-Java by zhenwei1108.
the class KeyBuilder method convertPublicKey.
/**
* @param [publicKey]
* @return java.security.PublicKey
* @author zhangzhenwei
* @description 公钥转换 byte[] to {@link PublicKey}
* @date 2022/2/11 22:34
* @since 1.0
*/
public PublicKey convertPublicKey(byte[] publicKey) throws WeGooKeyException {
try {
SubjectPublicKeyInfo keyInfo = SubjectPublicKeyInfo.getInstance(publicKey);
if (keyInfo == null) {
throw new WeGooKeyException(IExceptionEnum.params_err);
}
X509EncodedKeySpec spec = new X509EncodedKeySpec(publicKey);
KeyFactory factory = KeyFactory.getInstance(keyInfo.getAlgorithm().getAlgorithm().toString(), new WeGooProvider());
return factory.generatePublic(spec);
} catch (WeGooCryptoException e) {
throw e;
} catch (Exception e) {
throw new WeGooKeyException(KeyExceptionMessageEnum.structure_public_key_err, e);
}
}
use of com.github.zhenwei.provider.jce.provider.WeGooProvider in project LinLong-Java by zhenwei1108.
the class KeyStoreBuilder method gen.
public byte[] gen(String type, PrivateKey privateKey, String alias, String passWd, Certificate[] certChain) throws WeGooCryptoException {
try {
KeyStore store = KeyStore.getInstance(type, new WeGooProvider());
store.load(null);
store.setKeyEntry(alias, privateKey, passWd.toCharArray(), certChain);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
store.store(outputStream, passWd.toCharArray());
return outputStream.toByteArray();
} catch (Exception e) {
throw new WeGooCryptoException(CryptoExceptionMassageEnum.generate_jks_err, e);
}
}
use of com.github.zhenwei.provider.jce.provider.WeGooProvider in project LinLong-Java by zhenwei1108.
the class KeyStoreBuilder method parse.
public KeyStore parse(String type, String passWd, byte[] jks) throws WeGooCryptoException {
try {
KeyStore store = KeyStore.getInstance(type, new WeGooProvider());
ByteArrayInputStream stream = new ByteArrayInputStream(jks);
store.load(stream, passWd.toCharArray());
return store;
} catch (Exception e) {
throw new WeGooCryptoException(CryptoExceptionMassageEnum.parse_jks_err, e);
}
}
use of com.github.zhenwei.provider.jce.provider.WeGooProvider in project LinLong-Java by zhenwei1108.
the class CertTest method genCert.
public Certificate genCert() throws Exception {
KeyBuilder keyBuilder = new KeyBuilder(new WeGooProvider());
KeyPair keyPair = keyBuilder.buildKeyPair(KeyPairAlgEnum.RSA_1024);
byte[] certificate = CertBuilder.generateCertificate("O=zhenwei,CN=wegoo,C=CN", "O=zhenwei,CN=wegoo,C=CN", keyPair.getPublic(), keyPair.getPrivate());
CertBuilder builder = CertBuilder.getInstance(certificate);
return builder.getCert();
}
use of com.github.zhenwei.provider.jce.provider.WeGooProvider in project LinLong-Java by zhenwei1108.
the class KeyPairTest method genSM2Key.
@Test
public void genSM2Key() throws BaseWeGooException {
KeyBuilder builder = new KeyBuilder(new WeGooProvider());
KeyPair keyPair = builder.buildKeyPair(KeyPairAlgEnum.SM2_256);
System.out.println(Base64Util.encode(keyPair.getPrivate().getEncoded()));
System.out.println(Base64Util.encode(keyPair.getPublic().getEncoded()));
SignBuilder signBuilder = new SignBuilder(new WeGooProvider());
byte[] signatureSourceData = signBuilder.signatureSourceData(SignAlgEnum.SM3_WITH_SM2, keyPair.getPrivate(), "sadfadf".getBytes(StandardCharsets.UTF_8));
System.out.println(Base64Util.encode(signatureSourceData));
}
Aggregations