use of com.github.zhenwei.core.exception.WeGooCryptoException 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.core.exception.WeGooCryptoException 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.core.exception.WeGooCryptoException 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.core.exception.WeGooCryptoException in project LinLong-Java by zhenwei1108.
the class P7bBuilder method buildP7b.
public static ArrayList<X509Certificate> buildP7b(byte[] data) throws WeGooCryptoException {
ContentInfo contentInfo = ContentInfo.getInstance(data);
// P7B 的 contentType应该是这个
// ASN1ObjectIdentifier signedData = PKCSObjectIdentifiers.signedData;
SignedData signedData = SignedData.getInstance(contentInfo.getContent());
if (signedData == null)
throw new WeGooCryptoException(CryptoExceptionMassageEnum.build_err);
ASN1Set certs = signedData.getCertificates();
ArrayList<X509Certificate> list = new ArrayList<>();
for (ASN1Encodable cert : certs) {
list.add(CertBuilder.getInstance(cert).getCert());
}
return list;
}
use of com.github.zhenwei.core.exception.WeGooCryptoException in project LinLong-Java by zhenwei1108.
the class WeGooBuilder method forceAuth.
/**
* @param [provider]
* @return void
* @author zhangzhenwei
* @description 强制认证, 自定义provider需要使用
* CN=JCE Code Signing CA, OU=Java Software Code Signing, O=Oracle Corporation
* 签名.
* @date 2022/2/6 21:40
*/
private void forceAuth(Provider provider) throws WeGooCryptoException {
try {
var verificationResults = new IdentityHashMap<>();
verificationResults.put(provider, true);
var field = Class.forName("javax.crypto.JceSecurity").getDeclaredField("verificationResults");
field.setAccessible(true);
var modifiers = field.getClass().getDeclaredField("modifiers");
modifiers.setAccessible(true);
modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(verificationResults, verificationResults);
} catch (Exception e) {
throw new WeGooCryptoException(e);
}
}
Aggregations