Example usage for java.security PrivateKey getEncoded

List of usage examples for java.security PrivateKey getEncoded

Introduction

In this page you can find the example usage for java.security PrivateKey getEncoded.

Prototype

public byte[] getEncoded();

Source Link

Document

Returns the key in its primary encoding format, or null if this key does not support encoding.

Usage

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 w w . j  av  a 2  s.c  o m
    KeyPair keypair = keyGen.genKeyPair();
    PrivateKey privateKey = keypair.getPrivate();
    PublicKey publicKey = keypair.getPublic();

    byte[] privateKeyBytes = privateKey.getEncoded();
    byte[] publicKeyBytes = publicKey.getEncoded();
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    MainClass kpge = new MainClass();
    KeyPair kp = kpge.generateKeyPair(999);
    System.out.println("\n-- Private Key ----");
    PrivateKey priKey = kp.getPrivate();
    System.out.println("   Algorithm=" + priKey.getAlgorithm());
    System.out.println("   Encoded=" + priKey.getEncoded());
    System.out.println("   Format=" + priKey.getFormat());
}

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 w  w  . j  a va 2  s  .c  o m
    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:cn.lynx.emi.license.GenerateKeyPairs.java

public static void main(String[] args) throws NoSuchAlgorithmException {
    KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
    SecureRandom secrand = new SecureRandom();
    secrand.setSeed("cn.lynx.emi".getBytes());
    keygen.initialize(4096, secrand);//from   w  ww  .j a  v a 2 s  . com
    KeyPair keys = keygen.genKeyPair();

    PublicKey pubkey = keys.getPublic();
    PrivateKey prikey = keys.getPrivate();

    String myPubKey = Base64.encodeBase64String(pubkey.getEncoded());
    String myPriKey = Base64.encodeBase64String(prikey.getEncoded());
    System.out.println(prikey);
    System.out.println("pubKey=" + myPubKey);
    System.out.println("priKey=" + myPriKey);
}

From source file:com.clustercontrol.util.KeyCheck.java

/**
 * ????????//from  w w w  .j a  v a2  s  .  c  o m
 * 
 * @param args
 */
public static void main(String[] args) {
    PrivateKey privateKey = null;
    PublicKey publicKey = null;

    /// ??????? true
    /// ???????? false (?)
    boolean flag = false;
    if (flag) {
        try {
            // ?
            privateKey = getPrivateKey(
                    "???????privateKey.txt??");

            // ?
            publicKey = getPublicKey("???????");
            // publicKey = getPublicKey(publicKeyStr);
        } catch (Exception e) {
            System.out.println("hoge" + e.getMessage());
        }
    } else {
        KeyPairGenerator generator;
        try {
            generator = KeyPairGenerator.getInstance(ALGORITHM);
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            // ?? 1024
            generator.initialize(1024, random);
            KeyPair keyPair = generator.generateKeyPair();
            privateKey = keyPair.getPrivate();
            publicKey = keyPair.getPublic();
        } catch (NoSuchAlgorithmException ex) {
            System.out.println(ex.getMessage());
        }
    }

    //
    // ?
    System.out.println("?");
    System.out.println(byte2String(privateKey.getEncoded()));
    System.out.println("?");
    System.out.println(byte2String(publicKey.getEncoded()));

    // ???????
    String string = "20140701_nttdata";
    byte[] src = string.getBytes();
    System.out.println("??String");
    System.out.println(string);
    System.out.println("??byte");
    System.out.println(byte2String(src));

    // ?
    try {
        String encStr = encrypt(string, privateKey);
        System.out.println("?");
        System.out.println(encStr);

        // ?
        String decStr = decrypt(encStr, publicKey);
        System.out.println("?");
        System.out.println(decStr);
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

}

From source file:org.casbah.provider.KeyHelper.java

private static byte[] encodeKeyToDer(char[] keypass, PrivateKey key) {
    return key.getEncoded();
}

From source file:org.sinekartads.utils.X509Utils.java

public static String privateKeyToHex(PrivateKey privateKey) {

    byte[] privateKeyEnc = privateKey.getEncoded();
    return HexUtils.encodeHex(privateKeyEnc);
}

From source file:de.alpharogroup.crypto.key.PrivateKeyExtensions.java

/**
 * Transform the given {@link PrivateKey} to a base64 encoded {@link String} value.
 *
 * @param privateKey//from w w w.j  a  v  a2 s . co  m
 *            the private key
 * @return the new base64 encoded {@link String} value.
 */
public static String toBase64(final PrivateKey privateKey) {
    final byte[] encoded = privateKey.getEncoded();
    final String privateKeyAsBase64String = Base64.encodeBase64String(encoded);
    return privateKeyAsBase64String;
}

From source file:com.aqnote.shared.cryptology.cert.io.PKCSTransformer.java

public static String getKeyFileStringB64(PrivateKey privKey) throws Exception {
    return Base64.encodeBase64String(privKey.getEncoded());
}

From source file:net.nicholaswilliams.java.licensing.encryption.KeyFileUtilities.java

protected static byte[] writeEncryptedPrivateKey(PrivateKey privateKey, char[] passphrase) {
    PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
    return Encryptor.encryptRaw(pkcs8EncodedKeySpec.getEncoded(), passphrase);
}