Key getEncoded - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Key getEncoded method.

This code example shows how to use the following methods:getClass, getAlgorithm, getEncoded, getFormat
 
        if (keySpec == null) {
            throw new InvalidKeySpecException("keySpec == null");
        } 
 
        if (!"EC".equals(key.getAlgorithm())) {
            throw new InvalidKeySpecException("Key must be an EC key");
        } 
 
        if (key instanceof ECPublicKey && ECPublicKeySpec.class.isAssignableFrom(keySpec)) {
            ECPublicKey ecKey = (ECPublicKey) key;
            return (T) new ECPublicKeySpec(ecKey.getW(), ecKey.getParams());
        } else if (key instanceof PublicKey && ECPublicKeySpec.class.isAssignableFrom(keySpec)) {
            final byte[] encoded = key.getEncoded();
            if (!"X.509".equals(key.getFormat()) || encoded == null) {
                throw new InvalidKeySpecException("Not a valid X.509 encoding");
            } 
            ECPublicKey ecKey = (ECPublicKey) engineGeneratePublic(new X509EncodedKeySpec(encoded));
            return (T) new ECPublicKeySpec(ecKey.getW(), ecKey.getParams());
        } else if (key instanceof ECPrivateKey

7
CodeRank
This code example shows how to use the following methods:getEncoded
  public MasterKeyData(int serialNo, SecretKey secretKey) {
    this.masterKeyRecord = Records.newRecord(MasterKey.class);
    this.masterKeyRecord.setKeyId(serialNo);
    this.generatedSecretKey = secretKey;
    this.masterKeyRecord.setBytes(ByteBuffer.wrap(generatedSecretKey
      .getEncoded()));
  } 
 
  public MasterKeyData(MasterKey masterKeyRecord, SecretKey secretKey) {
    this.masterKeyRecord = masterKeyRecord;
    this.generatedSecretKey = secretKey;
 
  } 
 
  public MasterKey getMasterKey() { 
    return this.masterKeyRecord;
  } 
 
  public SecretKey getSecretKey() {
    return this.generatedSecretKey;
7
CodeRank
This code example shows how to use the following methods:getAlgorithm, getEncoded, getFormat
    private void encodeKey( 
        Key                 key,
        DataOutputStream    dOut)
        throws IOException 
    { 
        byte[]      enc = key.getEncoded();
 
        if (key instanceof PrivateKey)
        { 
            dOut.write(KEY_PRIVATE);
        } 
        else if (key instanceof PublicKey)
        { 
            dOut.write(KEY_PUBLIC);
        } 
        else 
        { 
            dOut.write(KEY_SECRET);
        } 
     

7
CodeRank
This code example shows how to use the following methods:getClass, getAlgorithm, getEncoded, getFormat
 
        if (keySpec == null) {
            throw new InvalidKeySpecException("keySpec == null");
        } 
 
        if (!"RSA".equals(key.getAlgorithm())) {
            throw new InvalidKeySpecException("Key must be a RSA key");
        } 
 
        if (key instanceof RSAPublicKey && RSAPublicKeySpec.class.isAssignableFrom(keySpec)) {
            RSAPublicKey rsaKey = (RSAPublicKey) key;
            return (T) new RSAPublicKeySpec(rsaKey.getModulus(), rsaKey.getPublicExponent());
        } else if (key instanceof PublicKey && RSAPublicKeySpec.class.isAssignableFrom(keySpec)) {
            final byte[] encoded = key.getEncoded();
            if (!"X.509".equals(key.getFormat()) || encoded == null) {
                throw new InvalidKeySpecException("Not a valid X.509 encoding");
            } 
            RSAPublicKey rsaKey =
                    (RSAPublicKey) engineGeneratePublic(new X509EncodedKeySpec(encoded));
            return (T) new RSAPublicKeySpec(rsaKey.getModulus(), rsaKey.getPublicExponent());

7
CodeRank
This code example shows how to use the following methods:getEncoded
        } 
        else 
        { 
            try 
            { 
                byte[]  bytes = publicKey.getEncoded();
 
                publicKey = JDKKeyFactory.createPublicKeyFromDERStream(bytes);
 
                if (publicKey instanceof DSAKey)
                { 
                    param = DSAUtil.generatePublicKeyParameter(publicKey);
                } 
                else 
                { 
                    throw new InvalidKeyException("can't recognise key type in DSA based signer");
                } 
            } 
            catch (Exception e)
            { 
7
CodeRank
This code example shows how to use the following methods:getEncoded
KeyGenerator kgen = KeyGenerator.getInstance("AES");  
	    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");  
	    kgen.init(m_nKeyLenBit, sr);  
	    SecretKey skey = kgen.generateKey();  
	    m_dbKeyData = skey.getEncoded();
	    Logger.I(TAG, "New key is generated");
	    if(DEBUG)
	        Logger.I(TAG, "Key: " + dumpKey(m_dbKeyData));
	} 
 
    private void writeKeyToStorage() 
    { 
        SharedPreferences settings =  ContextFactory.getContext().getSharedPreferences(m_strPrefName, Context.MODE_PRIVATE);		
        SharedPreferences.Editor editor = settings.edit();
        String strKey = Base64.encodeToString(m_dbKeyData, false);//, Base64.DEFAULT);
         
        editor.putString(m_strDBPartition, strKey );
        if (editor.commit()) {
            Logger.I(TAG, "Key is successfully saved to Shared Preferences");
7
CodeRank
This code example shows how to use the following methods:toString, getAlgorithm, getEncoded, getFormat
This code example shows how to use the following methods:getEncoded
    } 
 
    protected int engineGetKeySize( 
        Key     key)
    { 
        return key.getEncoded().length;
    } 
 
    protected int engineGetOutputSize( 
        int     inputLen)
    { 
        return -1; 
    } 
 
    protected AlgorithmParameters engineGetParameters()
    { 
        return null; 
    } 
 
    protected void engineSetMode( 
7
CodeRank
This code example shows how to use the following methods:getEncoded
         
            final KeySpec keySpec = new PBEKeySpec(input);
            final SecretKeyFactory keyFactory = 
                SecretKeyFactory.getInstance(getAlgorithm());
            final SecretKey key = keyFactory.generateSecret(keySpec);
            rawKey = key.getEncoded();
 
            // if initializing, just set it 
            if (init) {
                validator = createValidator(rawKey);
                storeValidator(validator);
                feedLocalKey(key);
            } 
            // if not initializing, check validity before setting 
            else { 
                if (validator == null) {
                    validator = loadValidator();
                } 
                if (checkKeyValid(rawKey, validator)) {
                    feedLocalKey(key);

6
CodeRank
This code example shows how to use the following methods:getEncoded
        } 
        else 
        { 
            try 
            { 
                byte[] bytes = publicKey.getEncoded();
 
                publicKey = JDKKeyFactory.createPublicKeyFromDERStream(bytes);
 
                if (publicKey instanceof ECPublicKey)
                { 
                    param = ECUtil.generatePublicKeyParameter(publicKey);
                } 
                else 
                { 
                    throw new InvalidKeyException("can't recognise key type in ECDSA based signer");
                } 
            } 
            catch (Exception e)
            { 
7
CodeRank

Related Key getEncoded Questions & Answers:

getEncoded Method Overview
Returns the key in its primary encoding format, or null if this key does not support encoding.
See Code Examples for other Java 8 Key Methods: