use of com.github.zhenwei.provider.jce.provider.WeGooProvider in project LinLong-Java by zhenwei1108.
the class WeGooJavaKeyStore method engineLoad.
public void engineLoad(InputStream var1, char[] var2) throws IOException, NoSuchAlgorithmException, CertificateException {
synchronized (this.entries) {
MessageDigest var5 = null;
CertificateFactory var6 = null;
Hashtable var7 = null;
ByteArrayInputStream var8 = null;
Object var9 = null;
if (var1 != null) {
DataInputStream var4;
if (var2 != null) {
var5 = this.getPreKeyedHash(var2);
var4 = new DataInputStream(new DigestInputStream(var1, var5));
} else {
var4 = new DataInputStream(var1);
}
int var10 = var4.readInt();
int var11 = var4.readInt();
if (var10 != -17957139 || var11 != 1 && var11 != 2) {
throw new IOException("Invalid keystore format");
} else {
if (var11 == 1) {
var6 = CertificateFactory.getInstance("X509", new WeGooProvider());
} else {
var7 = new Hashtable(3);
}
this.entries.clear();
int var12 = var4.readInt();
for (int var13 = 0; var13 < var12; ++var13) {
int var14 = var4.readInt();
String var15;
byte[] var23;
if (var14 != 1) {
if (var14 != 2) {
throw new IOException("Unrecognized keystore entry");
}
TrustedCertEntry var27 = new TrustedCertEntry();
var15 = var4.readUTF();
var27.date = new Date(var4.readLong());
if (var11 == 2) {
String var28 = var4.readUTF();
if (var7.containsKey(var28)) {
var6 = (CertificateFactory) var7.get(var28);
} else {
var6 = CertificateFactory.getInstance(var28, new WeGooProvider());
var7.put(var28, var6);
}
}
var23 = IOUtils.readExactlyNBytes(var4, var4.readInt());
var8 = new ByteArrayInputStream(var23);
var27.cert = var6.generateCertificate(var8);
var8.close();
this.entries.put(var15, var27);
} else {
KeyEntry var16 = new KeyEntry();
var15 = var4.readUTF();
var16.date = new Date(var4.readLong());
var16.protectedPrivKey = IOUtils.readExactlyNBytes(var4, var4.readInt());
int var17 = var4.readInt();
if (var17 > 0) {
ArrayList var18 = new ArrayList(var17 > 10 ? 10 : var17);
for (int var19 = 0; var19 < var17; ++var19) {
if (var11 == 2) {
String var20 = var4.readUTF();
if (var7.containsKey(var20)) {
var6 = (CertificateFactory) var7.get(var20);
} else {
var6 = CertificateFactory.getInstance(var20, new WeGooProvider());
var7.put(var20, var6);
}
}
var23 = IOUtils.readExactlyNBytes(var4, var4.readInt());
var8 = new ByteArrayInputStream(var23);
var18.add(var6.generateCertificate(var8));
var8.close();
}
var16.chain = (Certificate[]) var18.toArray(new Certificate[var17]);
}
this.entries.put(var15, var16);
}
}
if (var2 != null) {
byte[] var24 = var5.digest();
byte[] var25 = IOUtils.readExactlyNBytes(var4, var24.length);
if (!MessageDigest.isEqual(var24, var25)) {
UnrecoverableKeyException var26 = new UnrecoverableKeyException("Password verification failed");
throw (IOException) (new IOException("Keystore was tampered with, or password was incorrect")).initCause(var26);
}
}
}
}
}
}
use of com.github.zhenwei.provider.jce.provider.WeGooProvider in project LinLong-Java by zhenwei1108.
the class WeGooKeyProtector method recover.
public Key recover(EncryptedPrivateKeyInfo var1) throws UnrecoverableKeyException {
AlgorithmId var7 = var1.getAlgorithm();
if (!var7.getOID().toString().equals("1.3.6.1.4.1.42.2.17.1.1")) {
throw new UnrecoverableKeyException("Unsupported key protection algorithm");
} else {
byte[] var8 = var1.getEncryptedData();
byte[] var9 = new byte[20];
System.arraycopy(var8, 0, var9, 0, 20);
int var6 = var8.length - 20 - 20;
int var4 = var6 / 20;
if (var6 % 20 != 0) {
++var4;
}
byte[] var10 = new byte[var6];
System.arraycopy(var8, 20, var10, 0, var6);
byte[] var11 = new byte[var10.length];
int var2 = 0;
int var5 = 0;
byte[] var3;
for (var3 = var9; var2 < var4; var5 += 20) {
this.md.update(this.passwdBytes);
this.md.update(var3);
var3 = this.md.digest();
this.md.reset();
if (var2 < var4 - 1) {
System.arraycopy(var3, 0, var11, var5, var3.length);
} else {
System.arraycopy(var3, 0, var11, var5, var11.length - var5);
}
++var2;
}
byte[] var12 = new byte[var10.length];
for (var2 = 0; var2 < var12.length; ++var2) {
var12[var2] = (byte) (var10[var2] ^ var11[var2]);
}
this.md.update(this.passwdBytes);
Arrays.fill(this.passwdBytes, (byte) 0);
this.passwdBytes = null;
this.md.update(var12);
var3 = this.md.digest();
this.md.reset();
for (var2 = 0; var2 < var3.length; ++var2) {
if (var3[var2] != var8[20 + var6 + var2]) {
throw new UnrecoverableKeyException("Cannot recover key");
}
}
try {
// return PKCS8Key.parseKey(new DerValue(var12));
PrivateKeyInfo info = PrivateKeyInfo.getInstance(var12);
if (info == null) {
throw new UnrecoverableKeyException("Recover key can not null");
}
KeyPairAlgEnum algEnum = KeyPairAlgEnum.match(info.getPrivateKeyAlgorithm().getAlgorithm());
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(var12);
KeyFactory factory = KeyFactory.getInstance(algEnum.getAlg(), new WeGooProvider());
return factory.generatePrivate(spec);
} catch (Exception var14) {
throw new UnrecoverableKeyException(var14.getMessage());
}
}
}
use of com.github.zhenwei.provider.jce.provider.WeGooProvider in project LinLong-Java by zhenwei1108.
the class KeyBuilder method convertPrivateKey.
/**
* @param [privateKey]
* @return java.security.PrivateKey
* @author zhangzhenwei
* @description 私钥转换 byte[] to {@link PrivateKey}
* @date 2022/2/11 22:34
* @since 1.0
*/
public PrivateKey convertPrivateKey(byte[] privateKey) throws Exception {
try {
PrivateKeyInfo info = PrivateKeyInfo.getInstance(privateKey);
if (info == null) {
throw new WeGooKeyException(IExceptionEnum.params_err);
}
KeyPairAlgEnum algEnum = KeyPairAlgEnum.match(info.getPrivateKeyAlgorithm().getAlgorithm());
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(privateKey);
KeyFactory factory = KeyFactory.getInstance(algEnum.getAlg(), new WeGooProvider());
return factory.generatePrivate(spec);
} catch (WeGooCryptoException e) {
throw e;
} catch (Exception e) {
throw new WeGooKeyException(KeyExceptionMessageEnum.structure_private_key_err, e);
}
}
use of com.github.zhenwei.provider.jce.provider.WeGooProvider in project LinLong-Java by zhenwei1108.
the class P7Builder method enveloped.
public byte[] enveloped(X509Certificate certificate, byte[] data) throws BaseWeGooException, IOException {
ASN1EncodableVector envelopedDataVector = new ASN1EncodableVector();
ASN1EncodableVector recipientInfosVector = new ASN1EncodableVector();
ASN1EncodableVector recipientInfoVector = new ASN1EncodableVector();
ASN1EncodableVector encryptedContentInfoVector = new ASN1EncodableVector();
ASN1Integer version = new ASN1Integer(0);
envelopedDataVector.add(version);
// 版本
recipientInfoVector.add(version);
IssuerAndSerialNumber issuerAndSerialNumber = new IssuerAndSerialNumber(new X500Name(certificate.getSubjectDN().getName()), certificate.getSerialNumber());
// 序列号
recipientInfoVector.add(issuerAndSerialNumber);
// 匹配算法标识
PublicKey publicKey = certificate.getPublicKey();
String algorithm = publicKey.getAlgorithm();
// todo 算法oid
ASN1ObjectIdentifier identifier = GMObjectIdentifiers.sms4_cbc;
boolean isEc = algorithm.equalsIgnoreCase("EC");
ASN1ObjectIdentifier p7Oid = isEc ? new ASN1ObjectIdentifier("1.2.156.10197.6.1.4.2.3") : ContentInfo.envelopedData;
// RSA/SM2加解密算法
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(identifier);
recipientInfoVector.add(algorithmIdentifier);
WeGooProvider provider = new WeGooProvider();
KeyBuilder keyBuilder = new KeyBuilder(provider);
Key key = keyBuilder.buildKey(KeyEnum.SM4_128);
CipherBuilder cipherBuilder = new CipherBuilder(provider);
// 加密
byte[] encData = cipherBuilder.cipher(CipherAlgEnum.SM2, publicKey, key.getEncoded(), null, true);
recipientInfoVector.add(new DEROctetString(encData));
DERSequence recipientInfo = new DERSequence(recipientInfoVector);
recipientInfosVector.add(recipientInfo);
DERSet recipientInfos = new DERSet(recipientInfosVector);
// recipientInfos
envelopedDataVector.add(recipientInfos);
// ContentType
encryptedContentInfoVector.add(isEc ? new ASN1ObjectIdentifier("1.2.156.10197.6.1.4.2.1") : ContentInfo.data);
// contentEncryptionAlgorithm
CipherAlgEnum encAlg = CipherAlgEnum.SM4_ECB_PKCS7Padding;
ASN1ObjectIdentifier sms4_ecb = GMObjectIdentifiers.sms4_ecb;
if (encAlg.getModeEnum().isNeedIV()) {
// todo
encryptedContentInfoVector.add(new AlgorithmIdentifier(sms4_ecb, new DEROctetString(new byte[16])));
} else {
encryptedContentInfoVector.add(new AlgorithmIdentifier(sms4_ecb));
}
// todo iv
byte[] symEncData = cipherBuilder.cipher(encAlg, key, data, null, true);
// encryptedContent
encryptedContentInfoVector.add(new DERTaggedObject(false, 0, new DEROctetString(symEncData)));
DERSequence encryptedContentInfo = new DERSequence(encryptedContentInfoVector);
// encryptedContentInfo
envelopedDataVector.add(encryptedContentInfo);
// envelopedData
DERSequence envelopedData = new DERSequence(envelopedDataVector);
return new ContentInfo(p7Oid, envelopedData).getEncoded();
}
use of com.github.zhenwei.provider.jce.provider.WeGooProvider in project LinLong-Java by zhenwei1108.
the class CipherTest method encDecDataTest.
@Test
public void encDecDataTest() throws BaseWeGooException {
WeGooProvider provider = new WeGooProvider();
CipherBuilder builder = new CipherBuilder(provider);
KeyBuilder keyBuilder = new KeyBuilder(provider);
// SM2加解密
KeyPair keyPair = keyBuilder.buildKeyPair(KeyPairAlgEnum.SM2_256);
byte[] data = "this is my encrypt data test".getBytes(StandardCharsets.UTF_8);
IvParameterSpec spec = new IvParameterSpec(new byte[16]);
byte[] encryptedData = builder.cipher(CipherAlgEnum.SM2, keyPair.getPublic(), data, spec, true);
data = builder.cipher(CipherAlgEnum.SM2, keyPair.getPrivate(), encryptedData, spec, false);
System.out.println("解密结果: " + new String(data));
// RSA加解密
keyPair = keyBuilder.buildKeyPair(KeyPairAlgEnum.RSA_2048);
encryptedData = builder.cipher(CipherAlgEnum.RSA, keyPair.getPublic(), data, spec, true);
data = builder.cipher(CipherAlgEnum.RSA, keyPair.getPrivate(), encryptedData, spec, false);
System.out.println("解密结果: " + new String(data));
}
Aggregations