use of com.github.zhenwei.core.enums.KeyPairAlgEnum 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.core.enums.KeyPairAlgEnum 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.core.enums.KeyPairAlgEnum in project LinLong-Java by zhenwei1108.
the class KeyBuilder method getRealPrivateKey.
/**
* @param [privateKey]
* @return byte[]
* @author zhangzhenwei
* @description 获取裸私钥
* @date 2022/2/11 23:06
* @since 1.0
*/
public byte[] getRealPrivateKey(byte[] privateKey) throws WeGooCryptoException {
try {
PrivateKeyInfo info = PrivateKeyInfo.getInstance(privateKey);
if (info == null) {
throw new WeGooKeyException(IExceptionEnum.params_err);
}
KeyPairAlgEnum algEnum = KeyPairAlgEnum.match(info.getPrivateKeyAlgorithm().getAlgorithm());
// SM2 算法
if (algEnum.getAlg().equals(KeyPairAlgEnum.SM2_256.getAlg())) {
DLSequence dlSequence = (DLSequence) DLSequence.fromByteArray(privateKey);
byte[] priKeys = ((DEROctetString) dlSequence.getObjectAt(2)).getOctets();
dlSequence = (DLSequence) DLSequence.fromByteArray(priKeys);
DEROctetString derPriKey = (DEROctetString) dlSequence.getObjectAt(1);
return derPriKey.getOctets();
} else {
return info.getPrivateKey().getOctets();
}
} catch (WeGooKeyException e) {
throw e;
} catch (Exception e) {
throw new WeGooKeyException(KeyExceptionMessageEnum.parse_private_key_err, e);
}
}
Aggregations