Example usage for java.security.spec X509EncodedKeySpec X509EncodedKeySpec

List of usage examples for java.security.spec X509EncodedKeySpec X509EncodedKeySpec

Introduction

In this page you can find the example usage for java.security.spec X509EncodedKeySpec X509EncodedKeySpec.

Prototype

public X509EncodedKeySpec(byte[] encodedKey) 

Source Link

Document

Creates a new X509EncodedKeySpec with the given encoded key.

Usage

From source file:pl.kotcrab.crypto.RSAEncrypter.java

/** Constructs RSAEncrypter from {@link X509EncodedKeySpec#getEncoded()} encoded with base64
 * @param publicEncodedBase64 base64 string from {@link X509EncodedKeySpec#getEncoded()} bytes */
public RSAEncrypter(String publicEncodedBase64) {
    this(new X509EncodedKeySpec(Base64.decodeBase64(publicEncodedBase64)));
}

From source file:org.hypoport.jwt.common.Toolbox.java

public static ECPublicKey readECDHPublicKey(FileReader keyReader) throws Exception {
    return (ECPublicKey) KeyFactory.getInstance("EC")
            .generatePublic(new X509EncodedKeySpec(readPemFile(keyReader)));
}

From source file:com.github.ibole.infrastructure.security.ECDSA.java

public static void jdkECDSA() {
    try {/*  ww  w . j a v a2s.com*/
        // 1.?
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC");
        keyPairGenerator.initialize(256);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        ECPublicKey ecPublicKey = (ECPublicKey) keyPair.getPublic();
        ECPrivateKey ecPrivateKey = (ECPrivateKey) keyPair.getPrivate();

        // 2.??
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(ecPrivateKey.getEncoded());

        KeyFactory keyFactory = KeyFactory.getInstance("EC");
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
        Signature signature = Signature.getInstance("SHA256withECDSA");
        signature.initSign(privateKey);
        signature.update(src.getBytes());
        byte[] res = signature.sign();
        System.out.println("??" + Base64.encodeBase64String(res));

        // 3.???
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(ecPublicKey.getEncoded());
        keyFactory = KeyFactory.getInstance("EC");
        PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
        signature = Signature.getInstance("SHA256withECDSA");
        signature.initVerify(publicKey);
        signature.update(src.getBytes());
        boolean bool = signature.verify(res);
        System.out.println("?" + bool);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:architecture.common.license.validator.CheckSignatureValidator.java

public void validate(License license) throws LicenseException {
    try {//from   ww  w.  j av a  2s.  c  o  m
        //   DSA ? .
        String publicKey = "308201b83082012c06072a8648ce3804013082011f02818100fd7f53811d75122952df4a9c2eece4e7f611b7523cef4400c31e3f80b6512669455d402251fb593d8d58fabfc5f5ba30f6cb9b556cd7813b801d346ff26660b76b9950a5a49f9fe8047b1022c24fbba9d7feb7c61bf83b57e7c6a8a6150f04fb83f6d3c51ec3023554135a169132f675f3ae2b61d72aeff22203199dd14801c70215009760508f15230bccb292b982a2eb840bf0581cf502818100f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa3aea82f9574c0b3d0782675159578ebad4594fe67107108180b449167123e84c281613b7cf09328cc8a6e13c167a8b547c8d28e0a3ae1e2bb3a675916ea37f0bfa213562f1fb627a01243bcca4f1bea8519089a883dfe15ae59f06928b665e807b552564014c3bfecf492a0381850002818100faf2d25b2866aa68501094d1097bebc95c6bcf1c58766f18b35fbf5e9d761cc5bf913447e374c21d279777859f9f043d1dc0d58b93a2081b56b4f5269a81b076907a3b11b01ec5cfde5dae4dfd7d26346e53e611235e714e69ec1bc141c77a8a28c4c799df570a4c3240e7f2fee19d6ed4caaa1b15b5da4a967ee82e3eb4d4ca";

        byte pub[] = Hex.decodeHex(publicKey.toCharArray());
        X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(pub);

        KeyFactory keyFactory = KeyFactory.getInstance("DSA");
        java.security.PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);
        Signature sig = Signature.getInstance("DSA");

        sig.initVerify(pubKey);
        // license.getSignature().getBytes("UTF-8");
        byte decoded[] = Hex.decodeHex(license.getSignature().toCharArray());

        log.debug("decoded sig: " + Hex.encodeHexString(decoded));

        log.info((new StringBuilder()).append("Validating license. License fingerprint: ")
                .append(license.getSignature()).toString());

        sig.update(license.getFingerprint());

        boolean verified = sig.verify(decoded);

        if (!verified)
            throw new LicenseException("License signature is invalid.");
    } catch (Exception e) {
        log.fatal(e.getMessage(), e);
        throw new LicenseException(e);
    }
}

From source file:hh.learnj.test.license.test.rsa.RSATest.java

/**
 * ?/*from   w  w w.ja  v a  2 s . c o m*/
 * 
 * @return
 * @throws Exception
 */
static PublicKey getPublicKey() throws Exception {
    X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(decodeBase64(publicKey));
    KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA);
    return keyFactory.generatePublic(publicKeySpec);
}

From source file:cn.lynx.emi.license.ViewLicense.java

private static final String _decrypt(String data) {
    byte[] corekey = Base64.decodeBase64(LICENSE_CORE_KEY);
    byte[] rawData = Base64.decodeBase64(data);

    X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(corekey);
    try {//from  w ww.  j  a  v  a  2s.c o  m
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        Key publicKey = keyFactory.generatePublic(x509EncodedKeySpec);

        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, publicKey);

        return new String(cipher.doFinal(rawData), "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:pl.kotcrab.crypto.RSAEncrypter.java

/** Constructs RSAEncrypter from {@link X509EncodedKeySpec#getEncoded()}
 * @param publicEncoded bytes from {@link X509EncodedKeySpec#getEncoded()} */
public RSAEncrypter(byte[] publicEncoded) {
    this(new X509EncodedKeySpec(publicEncoded));
}

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

protected static byte[] writeEncryptedPublicKey(PublicKey publicKey, char[] passphrase) {
    X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
    return Encryptor.encryptRaw(x509EncodedKeySpec.getEncoded(), passphrase);
}

From source file:com.teasoft.teavote.util.Signature.java

private PublicKey getPublicKey()
        throws IOException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
    Resource resource = res.getResource("classpath:gotaSafui");
    byte[] pubKeyBytes;
    try (InputStream pubKeyInputStream = resource.getInputStream()) {
        pubKeyBytes = IOUtils.toByteArray(pubKeyInputStream);
        pubKeyBytes = Base64.decodeBase64(pubKeyBytes);
    }/* ww w.  j  av a 2 s.  c  o m*/
    X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(pubKeyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance("DSA", "SUN");
    PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);
    return pubKey;
}

From source file:net.sf.zekr.common.util.CryptoUtils.java

public static boolean verify(byte[] text, byte[] sigBytes) throws GeneralSecurityException {
    X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(PUBLIC_KEY);
    KeyFactory keyFactory = KeyFactory.getInstance("DSA");
    PublicKey pubKey = keyFactory.generatePublic(pubSpec);
    return verify(text, pubKey, sigBytes);
}