List of usage examples for java.security.spec PKCS8EncodedKeySpec PKCS8EncodedKeySpec
public PKCS8EncodedKeySpec(byte[] encodedKey)
From source file:Main.java
public static void main(String[] argv) throws Exception { String algorithm = "DSA"; // or RSA, DH, etc. // Generate a 1024-bit Digital Signature Algorithm (DSA) key pair KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm); keyGen.initialize(1024);/*from w ww . j a va 2 s .com*/ KeyPair keypair = keyGen.genKeyPair(); PrivateKey privateKey = keypair.getPrivate(); PublicKey publicKey = keypair.getPublic(); byte[] privateKeyBytes = privateKey.getEncoded(); byte[] publicKeyBytes = publicKey.getEncoded(); KeyFactory keyFactory = KeyFactory.getInstance(algorithm); EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes); PrivateKey privateKey2 = keyFactory.generatePrivate(privateKeySpec); EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes); PublicKey publicKey2 = keyFactory.generatePublic(publicKeySpec); // The orginal and new keys are the same boolean same = privateKey.equals(privateKey2); same = publicKey.equals(publicKey2); }
From source file:S3ClientSideEncryptionAsymmetricMasterKey.java
public static void main(String[] args) throws Exception { // 1. Load keys from files byte[] bytes = FileUtils.readFileToByteArray(new File(keyDir + "/private.key")); KeyFactory kf = KeyFactory.getInstance("RSA"); PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(bytes); PrivateKey pk = kf.generatePrivate(ks); bytes = FileUtils.readFileToByteArray(new File(keyDir + "/public.key")); PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(bytes)); KeyPair loadedKeyPair = new KeyPair(publicKey, pk); // 2. Construct an instance of AmazonS3EncryptionClient. EncryptionMaterials encryptionMaterials = new EncryptionMaterials(loadedKeyPair); AWSCredentials credentials = new BasicAWSCredentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"); AmazonS3EncryptionClient encryptionClient = new AmazonS3EncryptionClient(credentials, new StaticEncryptionMaterialsProvider(encryptionMaterials)); Region usEast1 = Region.getRegion(Regions.US_EAST_1); encryptionClient.setRegion(usEast1); encryptionClient.setEndpoint("https://play.minio.io:9000"); final S3ClientOptions clientOptions = S3ClientOptions.builder().setPathStyleAccess(true).build(); encryptionClient.setS3ClientOptions(clientOptions); // Create the bucket encryptionClient.createBucket(bucketName); // 3. Upload the object. byte[] plaintext = "Hello World, S3 Client-side Encryption Using Asymmetric Master Key!".getBytes(); System.out.println("plaintext's length: " + plaintext.length); encryptionClient.putObject(new PutObjectRequest(bucketName, objectKey, new ByteArrayInputStream(plaintext), new ObjectMetadata())); // 4. Download the object. S3Object downloadedObject = encryptionClient.getObject(bucketName, objectKey); byte[] decrypted = IOUtils.toByteArray(downloadedObject.getObjectContent()); Assert.assertTrue(Arrays.equals(plaintext, decrypted)); System.out.println("decrypted length: " + decrypted.length); //deleteBucketAndAllContents(encryptionClient); }
From source file:ImportKey.java
/** * <p>//from w ww. jav a 2 s . com * Takes two file names for a key and the certificate for the key, and * imports those into a keystore. Optionally it takes an alias for the key. * <p> * The first argument is the filename for the key. The key should be in * PKCS8-format. * <p> * The second argument is the filename for the certificate for the key. * <p> * If a third argument is given it is used as the alias. If missing, the key * is imported with the alias importkey * <p> * The name of the keystore file can be controlled by setting the keystore * property (java -Dkeystore=mykeystore). If no name is given, the file is * named <code>keystore.ImportKey</code> and placed in your home directory. * * @param args * [0] Name of the key file, [1] Name of the certificate file [2] * Alias for the key. **/ public static void main(String args[]) { // change this if you want another password by default String keypass = "password"; // change this if you want another alias by default String defaultalias = "tomcat"; // change this if you want another keystorefile by default String keystorename = null; // parsing command line input String keyfile = ""; String certfile = ""; if (args.length < 3 || args.length > 4) { System.out.println("Usage: java comu.ImportKey keystore keyfile certfile [alias]"); System.exit(0); } else { keystorename = args[0]; keyfile = args[1]; certfile = args[2]; if (args.length > 3) defaultalias = args[3]; } try { // initializing and clearing keystore KeyStore ks = KeyStore.getInstance("JKS", "SUN"); ks.load(null, keypass.toCharArray()); System.out.println("Using keystore-file : " + keystorename); ks.store(new FileOutputStream(keystorename), keypass.toCharArray()); ks.load(new FileInputStream(keystorename), keypass.toCharArray()); // loading Key InputStream fl = fullStream(keyfile); byte[] key = new byte[fl.available()]; KeyFactory kf = KeyFactory.getInstance("RSA"); fl.read(key, 0, fl.available()); fl.close(); PKCS8EncodedKeySpec keysp = new PKCS8EncodedKeySpec(key); PrivateKey ff = kf.generatePrivate(keysp); // loading CertificateChain CertificateFactory cf = CertificateFactory.getInstance("X.509"); InputStream certstream = fullStream(certfile); Collection c = cf.generateCertificates(certstream); Certificate[] certs = new Certificate[c.toArray().length]; if (c.size() == 1) { certstream = fullStream(certfile); System.out.println("One certificate, no chain."); Certificate cert = cf.generateCertificate(certstream); certs[0] = cert; } else { System.out.println("Certificate chain length: " + c.size()); certs = (Certificate[]) c.toArray(new Certificate[c.size()]); } // storing keystore ks.setKeyEntry(defaultalias, ff, keypass.toCharArray(), certs); System.out.println("Key and certificate stored."); System.out.println("Alias:" + defaultalias + " Password:" + keypass); ks.store(new FileOutputStream(keystorename), keypass.toCharArray()); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:Main.java
public static DSAPrivateKey getDSAPriKeyfromEncoded(byte[] encKey) throws NoSuchAlgorithmException, InvalidKeySpecException { PKCS8EncodedKeySpec pubKeySpec = new PKCS8EncodedKeySpec(encKey); KeyFactory kf = KeyFactory.getInstance("DSA"); return (DSAPrivateKey) kf.generatePrivate(pubKeySpec); }
From source file:Main.java
public static RSAPrivateKey getRSAPriKeyfromEncoded(byte[] encKey) throws NoSuchAlgorithmException, InvalidKeySpecException { PKCS8EncodedKeySpec pubKeySpec = new PKCS8EncodedKeySpec(encKey); KeyFactory kf = KeyFactory.getInstance("RSA"); return (RSAPrivateKey) kf.generatePrivate(pubKeySpec); }
From source file:Main.java
public static PrivateKey getPrivateKey(String key) { try {//w ww .jav a 2 s .c om Base64 base64_decoder = new Base64(); byte[] byteKey = base64_decoder.decode(key.getBytes()); // , // Base64.DEFAULT); PKCS8EncodedKeySpec X509privateKey = new PKCS8EncodedKeySpec(byteKey); KeyFactory kf = KeyFactory.getInstance("RSA"); return kf.generatePrivate(X509privateKey); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static PrivateKey getPrivateKey(byte[] keyBytes) throws NoSuchAlgorithmException, InvalidKeySpecException { PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(RSA); PrivateKey privateKey = keyFactory.generatePrivate(keySpec); return privateKey; }
From source file:Main.java
static Key base64ToRsaPrivateKey(String b64) { if (b64 != null) { try {//from www .ja va 2 s.c o m byte[] keyBytes = decodeB64(b64); if (keyBytes != null) { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(keyBytes); return keyFactory.generatePrivate(privateKeySpec); } } catch (Exception e) { e.printStackTrace(); } } return null; }
From source file:mx.bigdata.cfdi.security.KeyLoader.java
public static PrivateKey loadPKCS8PrivateKey(InputStream in, String passwd) throws Exception { byte[] decrypted = (passwd != null) ? getBytes(in, passwd.toCharArray()) : getBytes(in); PKCS8EncodedKeySpec keysp = new PKCS8EncodedKeySpec(decrypted); KeyFactory kf = KeyFactory.getInstance("RSA"); return kf.generatePrivate(keysp); }
From source file:com.jinhe.tss.framework.license.LicenseFactory.java
/** * ?license???/* w w w .j a v a 2 s . co m*/ * @param license * @throws Exception */ public static synchronized void sign(License license) throws Exception { String privateKey = FileHelper.readFile(new File(PRIVATE_KEY_FILE)); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(EasyUtils.decodeHex(privateKey.trim())); PrivateKey privKey = keyFactory.generatePrivate(privKeySpec); Signature sig = Signature.getInstance(SIGN_ALGORITHM); sig.initSign(privKey); sig.update(license.getFingerprint()); license.licenseSignature = EasyUtils.encodeHex(sig.sign()); }