Java get private key

djangofan / private_key.java

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

import java . security . Key
import java . security . KeyStore
if ( args . length < 3 )
throw new IllegalArgumentException ( ‘Expected args: ‘ )
def keystoreName = args [ 0 ]
def keystoreFormat = args [ 1 ]
def keystorePassword = args [ 2 ]
def alias = args [ 3 ]
def keyPassword = args [ 4 ]
def keystore = KeyStore . getInstance ( keystoreFormat )
keystore . load ( new FileInputStream ( keystoreName ), keystorePassword . toCharArray ())
def key = keystore . getKey ( alias , keyPassword . toCharArray ())
println «——BEGIN PRIVATE KEY——«
println key . getEncoded (). encodeBase64 ()
println «——END PRIVATE KEY——«

getting null point exception when i ran the below code in groovy script

import java.security.Key
import java.security.KeyStore

def keystorePassword =»XXXXXXXX»
def keystoreName = «C:\Users\mmodupalli\Desktop\qeee.jks»
def keystore = KeyStore.getInstance(«JKS»)
keystore.load(new FileInputStream(keystoreName), keystorePassword.toCharArray())
def key = keystore.getKey(«testhie.itsguardian.com», keystorePassword.toCharArray())
log.info key.getEncoded().encodeBase64()

Источник

RevenueGitHubAdmin / GetCertificateAndKeyFromKeyStore.java

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

/*
* The following snippet retrieves, from an already loaded keystore, the certificate and the
* private key used to sign the requests. It takes as assumption that the keystore contains
* only one certificate.
*
* Gists provided for illustrative purposes only. Developers can use these as a support tool
* but the Office of the Revenue Commissioners (Revenue) does not provide any warranty with
* these gists.
*/
public void getCertificateAndKeyFromKeyStore ( KeyStore keyStore , String encodedPassword ) throws KeyStoreException , UnrecoverableEntryException , NoSuchAlgorithmException
// Retrieving the certificate.
String certificateAlias = keyStore . aliases (). nextElement ();
X509Certificate certificate = ( X509Certificate ) keyStore . getCertificate ( certificateAlias );
// Retrieving the private key.
KeyStore . PrivateKeyEntry privateKeyEntry = ( KeyStore . PrivateKeyEntry ) keyStore . getEntry (
certificateAlias ,
new KeyStore . PasswordProtection ( encodedPassword . toCharArray ())
);
PrivateKey privateKey = privateKeyEntry . getPrivateKey ();
>

Источник

destan / ParseRSAKeys.java

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

#! /usr/bin/env bash
openssl genrsa -out private_key.pem 4096
openssl rsa -pubout -in private_key.pem -out public_key.pem
# convert private key to pkcs8 format in order to import it from Java
openssl pkcs8 -topk8 -in private_key.pem -inform pem -out private_key_pkcs8.pem -outform pem -nocrypt

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

import java . io . IOException ;
import java . net . URISyntaxException ;
import java . nio . file . Files ;
import java . nio . file . Paths ;
import java . security . KeyFactory ;
import java . security . NoSuchAlgorithmException ;
import java . security . PrivateKey ;
import java . security . interfaces . RSAPublicKey ;
import java . security . spec . InvalidKeySpecException ;
import java . security . spec . PKCS8EncodedKeySpec ;
import java . security . spec . X509EncodedKeySpec ;
import java . util . Base64 ;
/**
* This file is intended to be used on a IDE for testing purposes.
* ClassLoader.getSystemResource won’t work in a JAR
*/
public class Main
public static void main ( String [] args ) throws InvalidKeySpecException , NoSuchAlgorithmException , IOException , URISyntaxException
String privateKeyContent = new String ( Files . readAllBytes ( Paths . get ( ClassLoader . getSystemResource ( «private_key_pkcs8.pem» ). toURI ())));
String publicKeyContent = new String ( Files . readAllBytes ( Paths . get ( ClassLoader . getSystemResource ( «public_key.pem» ). toURI ())));
privateKeyContent = privateKeyContent . replaceAll ( » \\ n» , «» ). replace ( «——BEGIN PRIVATE KEY——» , «» ). replace ( «——END PRIVATE KEY——» , «» );
publicKeyContent = publicKeyContent . replaceAll ( » \\ n» , «» ). replace ( «——BEGIN PUBLIC KEY——» , «» ). replace ( «——END PUBLIC KEY——» , «» );;
KeyFactory kf = KeyFactory . getInstance ( «RSA» );
PKCS8EncodedKeySpec keySpecPKCS8 = new PKCS8EncodedKeySpec ( Base64 . getDecoder (). decode ( privateKeyContent ));
PrivateKey privKey = kf . generatePrivate ( keySpecPKCS8 );
X509EncodedKeySpec keySpecX509 = new X509EncodedKeySpec ( Base64 . getDecoder (). decode ( publicKeyContent ));
RSAPublicKey pubKey = ( RSAPublicKey ) kf . generatePublic ( keySpecX509 );
System . out . println ( privKey );
System . out . println ( pubKey );
>
>

Источник

liudong / gist:3993726

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

import java . security . KeyPairGenerator ;
import java . security . KeyPair ;
import java . security . PrivateKey ;
import java . security . PublicKey ;
import java . security . KeyFactory ;
import java . security . spec . EncodedKeySpec ;
import java . security . spec . PKCS8EncodedKeySpec ;
import java . security . spec . X509EncodedKeySpec ;
import java . security . spec . InvalidKeySpecException ;
import java . security . NoSuchAlgorithmException ;
import com . sun . jersey . core . util . Base64 ;
public class GeneratePublicPrivateKeys
private static void generateKeys ( String keyAlgorithm , int numBits )
try
// Get the public/private key pair
KeyPairGenerator keyGen = KeyPairGenerator . getInstance ( keyAlgorithm );
keyGen . initialize ( numBits );
KeyPair keyPair = keyGen . genKeyPair ();
PrivateKey privateKey = keyPair . getPrivate ();
PublicKey publicKey = keyPair . getPublic ();
System . out . println ( » \n » + «Generating key/value pair using » + privateKey . getAlgorithm () + » algorithm» );
// Get the bytes of the public and private keys
byte [] privateKeyBytes = privateKey . getEncoded ();
byte [] publicKeyBytes = publicKey . getEncoded ();
// Get the formats of the encoded bytes
String formatPrivate = privateKey . getFormat (); // PKCS#8
String formatPublic = publicKey . getFormat (); // X.509
System . out . println ( «Private Key : » + Base64 . encode ( String . valueOf ( privateKeyBytes )));
System . out . println ( «Public Key : » + Base64 . encode ( String . valueOf ( publicKeyBytes )));
// The bytes can be converted back to public and private key objects
KeyFactory keyFactory = KeyFactory . getInstance ( keyAlgorithm );
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec ( privateKeyBytes );
PrivateKey privateKey2 = keyFactory . generatePrivate ( privateKeySpec );
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec ( publicKeyBytes );
PublicKey publicKey2 = keyFactory . generatePublic ( publicKeySpec );
// The original and new keys are the same
System . out . println ( » Are both private keys equal? » + privateKey . equals ( privateKey2 ));
System . out . println ( » Are both public keys equal? » + publicKey . equals ( publicKey2 ));
> catch ( InvalidKeySpecException specException )
System . out . println ( «Exception» );
System . out . println ( «Invalid Key Spec Exception» );
> catch ( NoSuchAlgorithmException e )
System . out . println ( «Exception» );
System . out . println ( «No such algorithm: » + keyAlgorithm );
>
>
public static void main ( String [] args )
// Generate a 1024-bit Digital Signature Algorithm (DSA) key pair
generateKeys ( «DSA» , 1024 );
// Generate a 576-bit DH key pair
generateKeys ( «DH» , 576 );
// Generate a 1024-bit RSA key pair
generateKeys ( «RSA» , 1024 );
>
>

You post is interestnig , is there away I can create a privatre key instance via a signature given stiring?

I have pub/private keys generated already

KeyPairGenerator keyPairGenerator is going to createa key pair, but in my case I alrady have it and then further want to use them for signign.

//ecdsaSign.initSign(keyPair.getPrivate());
byte[] pkInfo = «51114cac71a9575bc1b39104d176a39d81bd1a705b9a1ad32efd2222f13e59ad».getBytes();
// PrivateKey pvtKey = DSAPrivateKey ecdsaSign.initSign(pvtKey);
//byte[] publicKeyBytes = keyPair.getPublic().getEncoded();
byte[] publicKeyBytes = «025fe2d166a5a8ff005eb0c799a474174f5d061de266438c69d36c2032c6bff51a».getBytes();

Источник

Читайте также:  Document
Оцените статью