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()]);
}
}
上一篇:基于Python手写数字图像处理原理


下一篇:Docker学不会?不妨看看这篇文章