Java Key Create getKey()

Here you can find the source of getKey()

Description

get Key

License

Apache License

Declaration

private static Key getKey() 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.*;

import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;

public class Main {
    private static Key getKey() {
        try {//w w  w .  j av  a2 s  .c o m
            InputStream fis = ClassLoader.getSystemResourceAsStream("key.pk8");
            if (fis == null) {
                throw new RuntimeException("Cannot find keyfile");
            }
            try {
                // openssl pkcs8 -inform pem -outform der -topk8 -nocrypt -in key.pem -out key.pk8
                return KeyFactory.getInstance("RSA")
                        .generatePrivate(new PKCS8EncodedKeySpec(getByteArrayFromStream(fis)));
            } finally {
                fis.close();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static byte[] getByteArrayFromStream(InputStream is) throws IOException {
        byte[] b = new byte[10000];
        int read;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        while ((read = is.read(b, 0, b.length)) > 0) {
            out.write(b, 0, read);
        }

        return out.toByteArray();
    }
}

Related

  1. getKey()
  2. getKey()
  3. getKey(byte[] arrayBytesTemp)
  4. getKey(byte[] keyData)
  5. getKey(InputStream is)
  6. getKey(int size)