convert


import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.openssl.PEMException;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.List;

public class A {
public static PrivateKeyInfo[] getPrivateKeyInfo(InputStream inputStream) throws IOException {
PEMParser pemRd = new PEMParser(new InputStreamReader(inputStream));
List<PrivateKeyInfo> list = new ArrayList<>();
while (true) {
Object object = pemRd.readObject();
if (object == null) {
break;
}
if (object instanceof PEMKeyPair) {
list.add(((PEMKeyPair)object).getPrivateKeyInfo());
} else if (object instanceof PrivateKeyInfo) {
list.add((PrivateKeyInfo)object);
}
}
return list.toArray(new PrivateKeyInfo[list.size()]);
}

public static PrivateKey[] getPrivateKey(PrivateKeyInfo[] info) throws PEMException {
List<PrivateKey> list = new ArrayList<>();
for (int i = 0; i < info.length; i++) {
PrivateKey pair = new JcaPEMKeyConverter().getPrivateKey(info[i]);
list.add(pair);
}
return list.toArray(new PrivateKey[list.size()]);
}
}
posted @ 2021-12-30 10:28  zJanly  阅读(224)  评论(0编辑  收藏  举报