本文整理汇总了Java中org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter.getPrivateKey方法的典型用法代码示例。如果您正苦于以下问题:Java JcaPEMKeyConverter.getPrivateKey方法的具体用法?Java JcaPEMKeyConverter.getPrivateKey怎么用?Java JcaPEMKeyConverter.getPrivateKey使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter
的用法示例。
在下文中一共展示了JcaPEMKeyConverter.getPrivateKey方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readPrivateKey
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; //导入方法依赖的package包/类
/**
* Reads a base64-format PEM key and returns a Java PrivateKey for it.
* @param privateKey PEM-encoded private key
*/
public static PrivateKey readPrivateKey(String privateKey) {
try (StringReader keyReader = new StringReader(privateKey);
PEMParser pemReader = new PEMParser(keyReader)) {
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
Object keyPair = pemReader.readObject();
if (keyPair instanceof PrivateKeyInfo) {
return converter.getPrivateKey((PrivateKeyInfo) keyPair);
} else {
return converter.getPrivateKey(((PEMKeyPair) keyPair).getPrivateKeyInfo());
}
} catch (IOException x) {
// Shouldn't occur, since we're only reading from strings
throw new RuntimeException(x);
}
}
示例2: getPrivateKeyFromPEM
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; //导入方法依赖的package包/类
private static PrivateKey getPrivateKeyFromPEM(final Reader keyReader)
throws IOException {
final JcaPEMKeyConverter jcaPEMKeyConverter = new JcaPEMKeyConverter();
final PEMParser pem = new PEMParser(keyReader);
PrivateKey key;
Object pemContent = pem.readObject();
if(pemContent instanceof PEMKeyPair) {
PEMKeyPair pemKeyPair = (PEMKeyPair)pemContent;
KeyPair keyPair = jcaPEMKeyConverter.getKeyPair(pemKeyPair);
key = keyPair.getPrivate();
} else if (pemContent instanceof PrivateKeyInfo) {
PrivateKeyInfo privateKeyInfo = (PrivateKeyInfo) pemContent;
key = jcaPEMKeyConverter.getPrivateKey(privateKeyInfo);
} else {
throw new IllegalArgumentException("Unsupported private key format '" + pemContent.getClass().getSimpleName() + '"');
}
pem.close();
return key;
}
示例3: loadKeyPair
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; //导入方法依赖的package包/类
private PrivateKey loadKeyPair() throws IOException {
PEMParser reader = new PEMParser(file);
Object pemObject;
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
//PEMDecryptorProvider decryptionProv = new JcePEMDecryptorProviderBuilder().build(passphrase);
while((pemObject = reader.readObject()) != null) {
logger.debug("PemObject type: " + pemObject.getClass().getName());
if(pemObject instanceof PEMKeyPair) {
logger.debug("it match");
PrivateKeyInfo pki = ((PEMKeyPair) pemObject).getPrivateKeyInfo();
logger.debug("content: " + pki.getEncoded("UTF-8"));
return converter.getPrivateKey(pki);
} else {
logger.debug("Dont match");
}
}
logger.debug("fsdfsfs");
return null;
}
示例4: buildCertificateRequest
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; //导入方法依赖的package包/类
@Override
public String buildCertificateRequest() {
try {
CompanyInfo companyInfo = wsaaDao.loadActiveCompanyInfo();
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
PEMKeyPair pemPrivateKey = fromPem(companyInfo.getPrivateKey());
PrivateKey privateKey = converter.getPrivateKey(pemPrivateKey
.getPrivateKeyInfo());
PEMKeyPair pemPublicKey = fromPem(companyInfo.getPrivateKey());
PublicKey publicKey = converter.getPublicKey(pemPublicKey
.getPublicKeyInfo());
X500Principal subject = new X500Principal(companyInfo.buildSource());
ContentSigner signGen = new JcaContentSignerBuilder(SIGNING_ALGORITHM)
.build(privateKey);
PKCS10CertificationRequest csr = new JcaPKCS10CertificationRequestBuilder(
subject, publicKey).build(signGen);
return toPem(csr);
} catch (IOException | OperatorCreationException e) {
throw Throwables.propagate(e);
}
}
示例5: readPrivateKey
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; //导入方法依赖的package包/类
private PrivateKey readPrivateKey(String privateKeyPath, String keyPassword) throws IOException {
FileReader fileReader = new FileReader(privateKeyPath);
PEMParser keyReader = new PEMParser(fileReader);
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
PEMDecryptorProvider decryptionProv = new JcePEMDecryptorProviderBuilder().build(keyPassword.toCharArray());
Object keyPair = keyReader.readObject();
PrivateKeyInfo keyInfo;
if (keyPair instanceof PEMEncryptedKeyPair) {
PEMKeyPair decryptedKeyPair = ((PEMEncryptedKeyPair) keyPair).decryptKeyPair(decryptionProv);
keyInfo = decryptedKeyPair.getPrivateKeyInfo();
} else {
keyInfo = ((PEMKeyPair) keyPair).getPrivateKeyInfo();
}
keyReader.close();
return converter.getPrivateKey(keyInfo);
}
示例6: getUserSig
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; //导入方法依赖的package包/类
@Override
public String getUserSig(String identifier, long expire)throws QCloudException {
try {
Security.addProvider(new BouncyCastleProvider());
Reader reader = new CharArrayReader(imConfig.getPrivateKey().toCharArray());
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
PEMParser parser = new PEMParser(reader);
Object obj = parser.readObject();
parser.close();
PrivateKey privKeyStruct = converter.getPrivateKey((PrivateKeyInfo) obj);
String jsonString = "{"
+ "\"TLS.account_type\":\"" + 0 +"\","
+"\"TLS.identifier\":\"" + identifier +"\","
+"\"TLS.appid_at_3rd\":\"" + 0 +"\","
+"\"TLS.sdk_appid\":\"" + imConfig.getSdkAppId() +"\","
+"\"TLS.expire_after\":\"" + expire +"\","
+"\"TLS.version\": \"201512300000\""
+"}";
String time = String.valueOf(System.currentTimeMillis()/1000);
String SerialString =
"TLS.appid_at_3rd:" + 0 + "\n" +
"TLS.account_type:" + 0 + "\n" +
"TLS.identifier:" + identifier + "\n" +
"TLS.sdk_appid:" + imConfig.getSdkAppId() + "\n" +
"TLS.time:" + time + "\n" +
"TLS.expire_after:" + expire +"\n";
//Create Signature by SerialString
Signature signature = Signature.getInstance("SHA256withECDSA", "BC");
signature.initSign(privKeyStruct);
signature.update(SerialString.getBytes(Charset.forName("UTF-8")));
byte[] signatureBytes = signature.sign();
String sigTLS = Base64.encodeBase64String(signatureBytes);
//Add TlsSig to jsonString
JSONObject jsonObject= JSON.parseObject(jsonString);
jsonObject.put("TLS.sig", (Object)sigTLS);
jsonObject.put("TLS.time", (Object)time);
jsonString = jsonObject.toString();
//compression
Deflater compresser = new Deflater();
compresser.setInput(jsonString.getBytes(Charset.forName("UTF-8")));
compresser.finish();
byte [] compressBytes = new byte [512];
int compressBytesLength = compresser.deflate(compressBytes);
compresser.end();
return new String(Base64Url.base64EncodeUrl(Arrays.copyOfRange(compressBytes,0,compressBytesLength)));
}catch (Exception e) {
throw new QCloudException(e);
}
}
示例7: getPrivateKey
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; //导入方法依赖的package包/类
public Key getPrivateKey(String pemBlock) throws IOException {
PEMParser p = new PEMParser(new StringReader(cleanupPEM(pemBlock)));
Object o = p.readObject();
if (o == null)
throw new InvalidParameterException("Could not read certificate. Expected the certificate to begin with '-----BEGIN CERTIFICATE-----'.");
if (o instanceof PEMKeyPair) {
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
return converter.getPrivateKey(((PEMKeyPair) o).getPrivateKeyInfo());
}
if (o instanceof Key)
return (Key) o;
if (o instanceof KeyPair)
return ((KeyPair) o).getPrivate();
throw new InvalidParameterException("Expected KeyPair or Key.");
}
示例8: loadKeyInternal
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; //导入方法依赖的package包/类
public static KeyPair loadKeyInternal(String file, PublicKey publicKey) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
PEMParser parser = new PEMParser(bufferedReader);
Object key;
PrivateKeyInfo privateKeyInfo = (key = parser.readObject()) instanceof PrivateKeyInfo ? (PrivateKeyInfo)key : ((PEMKeyPair)key).getPrivateKeyInfo();
parser.close();
bufferedReader.close();
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
converter.setProvider("BC");
return new KeyPair(publicKey, converter.getPrivateKey(privateKeyInfo));
}
示例9: getPrivateKey
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; //导入方法依赖的package包/类
public static PrivateKey getPrivateKey(String keyString) {
PrivateKey privateKey = null;
try {
BufferedReader br = new BufferedReader(new StringReader(keyString));
PEMParser pp = new PEMParser(br);
PKCS8EncryptedPrivateKeyInfo pemPrivateKeyInfo = (PKCS8EncryptedPrivateKeyInfo) pp.readObject();
pp.close();
InputDecryptorProvider pkcs8Prov = new JceOpenSSLPKCS8DecryptorProviderBuilder().build("1234".toCharArray());
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
privateKey = converter.getPrivateKey(pemPrivateKeyInfo.decryptPrivateKeyInfo(pkcs8Prov));
} catch (Exception e) {
ProtoLogger.error("!!! Error importing private key !!!");
}
return privateKey;
}
示例10: GenTLSSignatureEx
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; //导入方法依赖的package包/类
/**
* @brief 生成 tls 票据,精简参数列表
* @param skdAppid 应用的 sdkappid
* @param identifier 用户 id
* @param privStr 私钥文件内容
* @param expire 有效期,以秒为单位,推荐时长一个月
* @return
* @throws IOException
*/
public static GenTLSSignatureResult GenTLSSignatureEx(
long skdAppid,
String identifier,
String privStr,
long expire) throws IOException {
GenTLSSignatureResult result = new GenTLSSignatureResult();
Security.addProvider(new BouncyCastleProvider());
Reader reader = new CharArrayReader(privStr.toCharArray());
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
PEMParser parser = new PEMParser(reader);
Object obj = parser.readObject();
parser.close();
PrivateKey privKeyStruct = converter.getPrivateKey((PrivateKeyInfo) obj);
String jsonString = "{"
+ "\"TLS.account_type\":\"" + 0 +"\","
+"\"TLS.identifier\":\"" + identifier +"\","
+"\"TLS.appid_at_3rd\":\"" + 0 +"\","
+"\"TLS.sdk_appid\":\"" + skdAppid +"\","
+"\"TLS.expire_after\":\"" + expire +"\","
+"\"TLS.version\": \"201512300000\""
+"}";
String time = String.valueOf(System.currentTimeMillis()/1000);
String SerialString =
"TLS.appid_at_3rd:" + 0 + "\n" +
"TLS.account_type:" + 0 + "\n" +
"TLS.identifier:" + identifier + "\n" +
"TLS.sdk_appid:" + skdAppid + "\n" +
"TLS.time:" + time + "\n" +
"TLS.expire_after:" + expire +"\n";
try {
//Create Signature by SerialString
Signature signature = Signature.getInstance("SHA256withECDSA", "BC");
signature.initSign(privKeyStruct);
signature.update(SerialString.getBytes(Charset.forName("UTF-8")));
byte[] signatureBytes = signature.sign();
String sigTLS = Base64.encodeBase64String(signatureBytes);
//Add TlsSig to jsonString
JSONObject jsonObject= new JSONObject(jsonString);
jsonObject.put("TLS.sig", (Object)sigTLS);
jsonObject.put("TLS.time", (Object)time);
jsonString = jsonObject.toString();
//compression
Deflater compresser = new Deflater();
compresser.setInput(jsonString.getBytes(Charset.forName("UTF-8")));
compresser.finish();
byte [] compressBytes = new byte [512];
int compressBytesLength = compresser.deflate(compressBytes);
compresser.end();
String userSig = new String(base64_url.base64EncodeUrl(Arrays.copyOfRange(compressBytes,0,compressBytesLength)));
result.urlSig = userSig;
}
catch(Exception e)
{
e.printStackTrace();
result.errMessage = "generate usersig failed";
}
return result;
}
示例11: setPrivateKey
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; //导入方法依赖的package包/类
private synchronized void setPrivateKey(PrivateKeyInfo privateKey) throws PEMException {
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider(bouncyCastleProvider);
this.privateKey = converter.getPrivateKey(privateKey);
}
示例12: login
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; //导入方法依赖的package包/类
@Override
public Credentials login(Service service) {
try {
CompanyInfo companyInfo = wsaaDao.loadActiveCompanyInfo();
checkNotNull(companyInfo.getName(),
"Debe configurar el nombre de la empresa antes de realizar el login");
checkNotNull(companyInfo.getUnit(),
"Debe configurar la unidad oranizacional antes de realizar el login");
checkNotNull(companyInfo.getCuit(),
"Debe configurar el CUIT antes de realizar el login");
checkNotNull(companyInfo.getPrivateKey(),
"Debe configurar la clave privada antes de realizar el login");
checkNotNull(companyInfo.getPublicKey(),
"Debe configurar la clave p�blica antes de realizar el login");
checkNotNull(companyInfo.getCertificate(),
"Debe configurar el certificado antes de realizar el login");
X509CertificateHolder certificateHolder = fromPem(companyInfo
.getCertificate());
CertificateFactory certFactory = CertificateFactory
.getInstance("X.509");
X509Certificate certificate = (X509Certificate) certFactory
.generateCertificate(new ByteArrayInputStream(
certificateHolder.getEncoded()));
PEMKeyPair pemKeyPair = fromPem(companyInfo.getPrivateKey());
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
PrivateKey privKey = converter.getPrivateKey(pemKeyPair
.getPrivateKeyInfo());
String cms = LoginTicketRequest
.create(companyInfo.buildSource(), service, setupDao.readSetup().getEnvironment())
.toXml(xstream).toCms(certificate, privKey).toString();
String loginTicketResponseXml = loginCms.loginCms(cms);
LoginTicketResponse response = (LoginTicketResponse) xstream
.fromXML(loginTicketResponseXml);
return response.getCredentials();
} catch (IOException | CertificateException e) {
throw Throwables.propagate(e);
}
}
示例13: parsePrivateKey
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; //导入方法依赖的package包/类
/**
* Parses a PrivateKey instance from a PEM representation.
*
* When the provided key is encrypted, the provided pass phrase is applied.
*
* @param pemRepresentation a PEM representation of a private key (cannot be null or empty)
* @param passPhrase optional pass phrase (must be present if the private key is encrypted).
* @return a PrivateKey instance (never null)
*/
public static PrivateKey parsePrivateKey(InputStream pemRepresentation, String passPhrase) throws IOException {
if ( passPhrase == null ) {
passPhrase = "";
}
try (Reader reader = new InputStreamReader(pemRepresentation); //
PEMParser pemParser = new PEMParser(reader)) {
final Object object = pemParser.readObject();
final JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider( "BC" );
final KeyPair kp;
if ( object instanceof PEMEncryptedKeyPair )
{
// Encrypted key - we will use provided password
final PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder().build( passPhrase.toCharArray() );
kp = converter.getKeyPair( ( (PEMEncryptedKeyPair) object ).decryptKeyPair( decProv ) );
}
else if ( object instanceof PKCS8EncryptedPrivateKeyInfo )
{
// Encrypted key - we will use provided password
try
{
final PKCS8EncryptedPrivateKeyInfo encryptedInfo = (PKCS8EncryptedPrivateKeyInfo) object;
final InputDecryptorProvider provider = new JceOpenSSLPKCS8DecryptorProviderBuilder().build( passPhrase.toCharArray() );
final PrivateKeyInfo privateKeyInfo = encryptedInfo.decryptPrivateKeyInfo( provider );
return converter.getPrivateKey( privateKeyInfo );
}
catch ( PKCSException | OperatorCreationException e )
{
throw new IOException( "Unable to decrypt private key.", e );
}
}
else if ( object instanceof PrivateKeyInfo )
{
return converter.getPrivateKey( (PrivateKeyInfo) object );
}
else
{
// Unencrypted key - no password needed
kp = converter.getKeyPair( (PEMKeyPair) object );
}
return kp.getPrivate();
}
}