Java 8 X509EncodedKeySpec - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 X509EncodedKeySpec class.

This code example shows how to use the following methods:
        return cipher;
    } 
 
    public static PublicKey getPubkey(EncryptionRequest request) throws GeneralSecurityException
    { 
        return KeyFactory.getInstance( "RSA" ).generatePublic( new X509EncodedKeySpec( request.getPublicKey() ) );
    } 
 
    public static byte[] encrypt(Key key, byte[] b) throws GeneralSecurityException
    { 
        Cipher hasher = Cipher.getInstance( "RSA" );
        hasher.init( Cipher.ENCRYPT_MODE, key );
        return hasher.doFinal( b );
    } 
} 
7
CodeRank
This code example shows how to use the following methods:
     */ 
    public static PublicKey generatePublicKey(String encodedPublicKey) {
        try { 
            byte[] decodedKey = Base64.decode(encodedPublicKey);
            KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM);
            return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeySpecException e) {
            Log.e(TAG, "Invalid key specification.");
            throw new IllegalArgumentException(e);
        } catch (Base64DecoderException e) {
            Log.e(TAG, "Base64 decoding failed.");
            throw new IllegalArgumentException(e);
        } 
    } 
 
    /** 
     * Verifies that the signature from the server matches the computed 
     * signature on the data.  Returns true if the data is correctly signed. 
7
CodeRank
This code example shows how to use the following methods:
		} 
	} 
 
	public void setPublicKey(byte[] encoded) {
		if (encoded == null) return;
		final X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encoded);
		if (type != null) {
			publicKey = decodePublicKeyAs(pubKeySpec, type);
		} else { 
			publicKey = decodePublicKeyAs(pubKeySpec, KEY_TYPE_RSA);
			if (publicKey != null) {
				type = KEY_TYPE_RSA;
			} else { 
				publicKey = decodePublicKeyAs(pubKeySpec, KEY_TYPE_DSA);
				if (publicKey != null) {
					type = KEY_TYPE_DSA;
				} 
			} 
		} 
	} 
7
CodeRank
This code example shows how to use the following methods:
        writeUTF(new String(Base64.encodeBase64(key.getEncoded())));
    } 
 
    public X509EncodedKeySpec readKey() throws IOException {
        byte[] otherHalf = Base64.decodeBase64(readUTF());
        return new X509EncodedKeySpec(otherHalf);
    } 
 
    /** 
     * Performs a Diffie-Hellman key exchange and produce a common secret between two ends of the connection. 
     * 
     * <p> 
     * DH is also useful as a coin-toss algorithm. Two parties get the same random number without trusting 
     * each other. 
     */ 
    public KeyAgreement diffieHellman(boolean side) throws IOException, GeneralSecurityException {
        KeyPair keyPair;
        PublicKey otherHalf;
 
        if (side) {

6
CodeRank
This code example shows how to use the following methods:
This code example shows how to use the following methods:
    public static PublicKey publicKeyFrom(byte[] derBytes)
            throws InvalidKeySpecException 
    { 
        try { 
            KeyFactory keyFactory = KeyFactory.getInstance(RSA);
            EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(derBytes);
            PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
            return publicKey;
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException(e);
        } 
    } 
 
    // Extracted from: 
    // http://oauth.googlecode.com/svn/code/branches/jmeter/jmeter/src/main/java/org/apache/jmeter/protocol/oauth/sampler/PrivateKeyReader.java 
    // See p.41 of http://www.emc.com/emc-plus/rsa-labs/pkcs/files/h11300-wp-pkcs-1v2-2-rsa-cryptography-standard.pdf 
    /**************************************************************************** 
     * Amazon Modifications: Copyright 2014 Amazon.com, Inc. or its affiliates.  
     * All Rights Reserved. 
     ***************************************************************************** 
6
CodeRank
This code example shows how to use the following methods:
        return cipher;
    } 
 
    public static PublicKey getPubkey(EncryptionRequest request) throws GeneralSecurityException
    { 
        return KeyFactory.getInstance( "RSA" ).generatePublic( new X509EncodedKeySpec( request.getPublicKey() ) );
    } 
 
    public static byte[] encrypt(Key key, byte[] b) throws GeneralSecurityExceptionCipher hasher = Cipher.getInstance( "RSA" );
        hasher.init( Cipher.ENCRYPT_MODE, key );
        return hasher.doFinal( b );
    } 
} 
6
CodeRank
This code example shows how to use the following methods:
      return null; 
    } 
     
    try { 
      KeyFactory fac = KeyFactory.getInstance("RSA");
      EncodedKeySpec spec = new X509EncodedKeySpec(publicKey);
      return fac.generatePublic(spec);
    } 
    catch (NoSuchAlgorithmException e) {
      throw new IllegalStateException(e);
    } 
    catch (InvalidKeySpecException e) {
      throw new IllegalStateException(e);
    } 
  } 
 
  /** 
   * Creates a private key from the PKCS#8-encoded value of the given bytes. 
   * 
   * @param privateKey The PKCS#8-encoded private key bytes. 
6
CodeRank
This code example shows how to use the following methods:
		} 
	} 
 
	public void setPublicKey(byte[] encoded) {
		if (encoded == null) return;
		final X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encoded);
		if (type != null) {
			publicKey = decodePublicKeyAs(pubKeySpec, type);
		} else { 
			publicKey = decodePublicKeyAs(pubKeySpec, KEY_TYPE_RSA);
			if (publicKey != null) {
				type = KEY_TYPE_RSA;
			} else { 
				publicKey = decodePublicKeyAs(pubKeySpec, KEY_TYPE_DSA);
				if (publicKey != null) {
					type = KEY_TYPE_DSA;
				} 
			} 
		} 
	} 
6
CodeRank
This code example shows how to use the following methods:
     */ 
    public static PublicKey generatePublicKey(String encodedPublicKey) {
        try { 
            byte[] decodedKey = Base64.decode(encodedPublicKey);
            KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM);
            return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeySpecException e) {
            Log.e(TAG, "Invalid key specification.");
            throw new IllegalArgumentException(e);
        } catch (Base64DecoderException e) {
            Log.e(TAG, "Base64 decoding failed.");
            throw new IllegalArgumentException(e);
        } 
    } 
 
    /** 
     * Verifies that the signature from the server matches the computed 
     * signature on the data.  Returns true if the data is correctly signed. 
6
CodeRank
This code example shows how to use the following methods:
  public void setChallengeToken(byte[] challangeToken) {
    challengeToken = challangeToken;
  } 
 
  private static PublicKey getPublicKey(byte[] keyBytes) throws GeneralSecurityException {
    return KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(keyBytes));
  } 
 
  private static SecretKey generateSharedKey() throws NoSuchAlgorithmException {
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    keyGenerator.init(128);
    return keyGenerator.generateKey();
  } 
 
  private static byte[] loginHash(String name, PublicKey publicKey, SecretKey sharedKey) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    return getHash("SHA-1", new byte[][] { name.getBytes("ISO_8859_1"), sharedKey.getEncoded(), publicKey.getEncoded() });
  } 
 
  private static byte[] getHash(String algrithm, byte[][] data) throws NoSuchAlgorithmException {
    MessageDigest digest = MessageDigest.getInstance(algrithm);
6
CodeRank
This code example shows how to use the following methods:
     */ 
    public static PublicKey generatePublicKey(String encodedPublicKey) {
        try { 
            byte[] decodedKey = Base64.decode(encodedPublicKey);
            KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM);
            return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeySpecException e) {
            Log.e(TAG, "Invalid key specification.");
            throw new IllegalArgumentException(e);
        } catch (Base64DecoderException e) {
            Log.e(TAG, "Base64 decoding failed.");
            throw new IllegalArgumentException(e);
        } 
    } 
 
    /** 
     * Verifies that the signature from the server matches the computed 
     * signature on the data.  Returns true if the data is correctly signed. 
6
CodeRank
This code example shows how to use the following methods:
		} 
	} 
 
	public void setPublicKey(byte[] encoded) {
		if (encoded == null) return;
		final X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encoded);
		if (type != null) {
			publicKey = decodePublicKeyAs(pubKeySpec, type);
		} else { 
			publicKey = decodePublicKeyAs(pubKeySpec, KEY_TYPE_RSA);
			if (publicKey != null) {
				type = KEY_TYPE_RSA;
			} else { 
				publicKey = decodePublicKeyAs(pubKeySpec, KEY_TYPE_DSA);
				if (publicKey != null) {
					type = KEY_TYPE_DSA;
				} 
			} 
		} 
	} 
6
CodeRank
This code example shows how to use the following methods:
    static { 
        keys.put("DH_public", new X509EncodedKeySpec(DH_public));
        keys.put("DH_private", new PKCS8EncodedKeySpec(DH_private));
        keys.put("DSA_public", new X509EncodedKeySpec(DSA_public));
        keys.put("DSA_private", new PKCS8EncodedKeySpec(DSA_private));
        keys.put("RSA_public", new X509EncodedKeySpec(RSA_public));
        keys.put("RSA_private", new PKCS8EncodedKeySpec(RSA_private));
    } 
 
    public static PrivateKey getPrivateKey(String algorithmName) throws NoSuchAlgorithmException, InvalidKeySpecException
    { 
        KeyFactory factory = KeyFactory.getInstance(algorithmName);
        return factory.generatePrivate(keys.get(algorithmName + "_private"));
    } 
 
    public static PublicKey getPublicKey(String algorithmName) throws NoSuchAlgorithmException, InvalidKeySpecException
    { 
        KeyFactory factory = KeyFactory.getInstance(algorithmName);
        return factory.generatePublic(keys.get(algorithmName + "_public"));
    } 
6
CodeRank
This code example shows how to use the following methods:
    public PublicKey getPublicKey(Provider provider)
        throws NoSuchAlgorithmException, InvalidKeySpecException, IOException
    { 
        SubjectPublicKeyInfo subjectPKInfo = pkac.getSubjectPublicKeyInfo();
 
        X509EncodedKeySpec xspec = new X509EncodedKeySpec(new DERBitString(subjectPKInfo).getBytes());
 
        return KeyFactory.getInstance(subjectPKInfo.getAlgorithmId().getAlgorithm().getId(), provider)
            .generatePublic(xspec);
    } 
} 
6
CodeRank
This code example shows how to use the following methods:
            new PemProcessor<KeySpec>(ImmutableMap.<String, PemProcessor.ResultParser<KeySpec>> of(PUBLIC_PKCS1_MARKER,
                  DecodeRSAPublicKeySpec.INSTANCE, PUBLIC_X509_MARKER, new PemProcessor.ResultParser<KeySpec>() {
 
                     @Override 
                     public X509EncodedKeySpec parseResult(byte[] bytes) throws IOException {
                        return new X509EncodedKeySpec(bytes);
                     } 
 
                  }))); 
   } 
 
   /** 
    * Decode PKCS#1 encoded public key into RSAPublicKeySpec. 
    * <p> 
    * Keys here can be in two different formats. They can have the algorithm encoded, or they can have only the modulus 
    * and the public exponent. 
    * <p> 
    * The latter is not a valid PEM encoded file, but it is a valid DER encoded RSA key, so this method should also 
    * support it. 
    *  
6
CodeRank
This code example shows how to use the following methods:
     */ 
    public static PublicKey generatePublicKey(String encodedPublicKey) {
        try { 
            byte[] decodedKey = Base64.decode(encodedPublicKey);
            KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM);
            return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeySpecException e) {
            Log.e(TAG, "Invalid key specification.");
            throw new IllegalArgumentException(e);
        } catch (Base64DecoderException e) {
            Log.e(TAG, "Base64 decoding failed.");
            throw new IllegalArgumentException(e);
        } 
    } 
 
    /** 
     * Verifies that the signature from the server matches the computed 
     * signature on the data. Returns true if the data is correctly signed. 
6
CodeRank
This code example shows how to use the following methods:
	public static CryptorRSA fromString(String publicKey, String privateKey) throws Exception
	{ 
		return  
			new CryptorRSAJCE( 
				publicKey != null ?
					KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(
						Base64.decode(publicKey))
					) : 
					null, 
				privateKey != null ?
					KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(
						Base64.decode(privateKey))
					) : 
					null 
			);					 
	} 
	 
} 
6
CodeRank
This code example shows how to use the following methods:
     */ 
    public static PublicKey generatePublicKey(String encodedPublicKey) {
        try { 
            byte[] decodedKey = Base64.decode(encodedPublicKey);
            KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM);
            return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeySpecException e) {
            Log.e(TAG, "Invalid key specification.");
            throw new IllegalArgumentException(e);
        } catch (Base64DecoderException e) {
            Log.e(TAG, "Base64 decoding failed.");
            throw new IllegalArgumentException(e);
        } 
    } 
 
    /** 
     * Verifies that the signature from the server matches the computed 
     * signature on the data.  Returns true if the data is correctly signed. 
6
CodeRank
This code example shows how to use the following methods:
This code example shows how to use the following methods:
            // Verify signature 
            if (sig != null && pub != null) {
                try { 
                    byte[] sigRaw = Base64.decode(sig, Base64.DEFAULT);
                    byte[] pubRaw = Base64.decode(stripKey(pub), Base64.DEFAULT);
                    X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(pubRaw);
                    KeyFactory kf = KeyFactory.getInstance("RSA", "BC");
                    Signature s = Signature.getInstance("SHA1withRSA", "BC");
                    s.initVerify(kf.generatePublic(publicKeySpec));
                    s.update(encRaw);
                    ver = s.verify(sigRaw);
                } catch (Exception ex) {
                    Log.i("whistle", "Verification failed: "+ex.getMessage());
                    ver = false;
                } 
            } 
             
            // Split enc into encrypted aes data and remaining enc 
            byte[] encSplit = encRaw;
            byte[] aesRaw = new byte[RSA_BYTES];
6
CodeRank
This code example shows how to use the following methods:
This code example shows how to use the following methods:
This code example shows how to use the following methods:
        { 
            return new PKCS8EncodedKeySpec(key.getEncoded());
        } 
        else if (spec.isAssignableFrom(X509EncodedKeySpec.class) && key.getFormat().equals("X.509"))
        { 
            return new X509EncodedKeySpec(key.getEncoded());
        } 
 
        throw new InvalidKeySpecException("not implemented yet " + key + " " + spec);
    } 
} 
6
CodeRank
This code example shows how to use the following methods:
		return Base64.encodeBytes(merged, Base64.DONT_BREAK_LINES);
	} 
 
	public static PublicKey keyForEncodedBytes(byte[] inBytes) throws InvalidKeySpecException {
 
		X509EncodedKeySpec key_spec = new X509EncodedKeySpec(inBytes);
		KeyFactory keyFactory = null;
		try { 
			keyFactory = KeyFactory.getInstance("RSA");
		} catch (NoSuchAlgorithmException e) {
			System.err.println(e);
			e.printStackTrace();
			return null; 
		} 
 
		return keyFactory.generatePublic(key_spec);
	} 
 
	public static boolean verifySignature(PublicKey key, byte[] nounce, byte[] signature) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, SignatureException {
		Signature sig = Signature.getInstance("SHA1withRSA");
6
CodeRank
This code example shows how to use the following methods:
        { 
            return new PKCS8EncodedKeySpec(key.getEncoded());
        } 
        else if (keySpec.isAssignableFrom(X509EncodedKeySpec.class) && key.getFormat().equals("X.509"))
        { 
            return new X509EncodedKeySpec(key.getEncoded());
        } 
 
        throw new InvalidKeySpecException("not implemented yet " + key + " " + keySpec);
    } 
 
    protected Key engineTranslateKey(Key key)
        throws InvalidKeyException 
    { 
        throw new InvalidKeyException("not implemented yet " + key);
    } 
} 
6
CodeRank
This code example shows how to use the following methods:
     */ 
    public static PublicKey generatePublicKey(String encodedPublicKey) {
        try { 
            byte[] decodedKey = Base64.decode(encodedPublicKey);
            KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM);
            return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeySpecException e) {
            Log.e(TAG, "Invalid key specification.");
            throw new IllegalArgumentException(e);
        } catch (Base64DecoderException e) {
            Log.e(TAG, "Base64 decoding failed.");
            throw new IllegalArgumentException(e);
        } 
    } 
 
    /** 
     * Verifies that the signature from the server matches the computed 
     * signature on the data.  Returns true if the data is correctly signed. 
6
CodeRank
This code example shows how to use the following methods:
     */ 
    public static PublicKey generatePublicKey(String encodedPublicKey) {
        try { 
            byte[] decodedKey = Base64.decode(encodedPublicKey);
            KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM);
            return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeySpecException e) {
            Log.e(TAG, "Invalid key specification.");
            throw new IllegalArgumentException(e);
        } catch (Base64DecoderException e) {
            Log.e(TAG, "Base64 decoding failed.");
            throw new IllegalArgumentException(e);
        } 
    } 
 
    /** 
     * Verifies that the signature from the server matches the computed 
     * signature on the data.  Returns true if the data is correctly signed. 
6
CodeRank
This code example shows how to use the following methods:
This code example shows how to use the following methods:
        throws InvalidKeyException, NoSuchAlgorithmException 
    { 
        try 
        { 
            SubjectPublicKeyInfo keyInfo = this.getSubjectPublicKeyInfo();
            X509EncodedKeySpec xspec = new X509EncodedKeySpec(keyInfo.getEncoded());
            KeyFactory kFact;
 
            try 
            { 
                kFact = helper.createKeyFactory(keyInfo.getAlgorithm().getAlgorithm().getId());
            } 
            catch (NoSuchAlgorithmException e)
            { 
                // 
                // try an alternate 
                // 
                if (keyAlgorithms.get(keyInfo.getAlgorithm().getAlgorithm()) != null)
                { 
                    String  keyAlgorithm = (String)keyAlgorithms.get(keyInfo.getAlgorithm().getAlgorithm());
6
CodeRank

Related Java 8 X509EncodedKeySpec Questions & Answers:

See Code Examples for Java 8 X509EncodedKeySpec Methods: