Java 8 SecretKeyFactory - 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 SecretKeyFactory class.

This code example shows how to use the following methods:generateSecret, getKeySpec, translateKey
 
    private void checkResult(SecretKeyFactory skf) throws InvalidKeyException,
            InvalidKeySpecException { 
        SecretKey sk;
        KeySpec keySpec;        
        sk = skf.generateSecret(null);
        assertNull("generateSecret method must return null", sk);
        sk = skf.translateKey(null);
        assertNull("translateKey method must return null", sk);
        keySpec = skf.getKeySpec(null, null);
        assertNull("getKeySpec method must return null", keySpec);
    } 
    /** 
     * Test for <code>getInstance(String algorithm)</code> method 
     * Assertions: 
     * throws NullPointerException when algorithm is null; 
     * throws NoSuchAlgorithmException when algorithm is incorrect; 
     * returns SecretKeyFactory object 
     */ 
    public void testGetInstance01() throws NoSuchAlgorithmException, 
6
CodeRank
This code example shows how to use the following methods:generateSecret
  { 
    // Create PBE parameter set 
    pbeParamSpec = new PBEParameterSpec(salt,count);
    pbeKeySpec = new PBEKeySpec("saagar".toCharArray());
    keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    pbeKey = keyFac.generateSecret(pbeKeySpec);
  } 
 
  /** 
   * Encrypt the string 
   */ 
  private byte[] crypt(int cipherMode,byte[] text)
    throws  NoSuchPaddingException, 
            NoSuchAlgorithmException, 
            InvalidAlgorithmParameterException, 
            InvalidKeyException, 
            IllegalBlockSizeException, 
            BadPaddingException 
 
  { 
4
CodeRank
This code example shows how to use the following methods:generateSecret
	public String jEncrypt(String unencryptedString) throws EncryptionException {
		if (unencryptedString == null || unencryptedString.trim().length() == 0)
			throw new IllegalArgumentException("unencrypted string was null or empty");
 
		try { 
			SecretKey key = keyFactory.generateSecret(keySpec);
			cipher.init(Cipher.ENCRYPT_MODE, key);
			byte[] cleartext = unencryptedString.getBytes(UNICODE_FORMAT);
			byte[] ciphertext = cipher.doFinal(cleartext);
 
			BASE64Encoder base64encoder = new BASE64Encoder();
			return base64encoder.encode(ciphertext);
		} catch (Exception e) {
			throw new EncryptionException(e);
		} 
	} 
 
	public String jDecrypt(String encryptedString) throws EncryptionException {
		if (encryptedString == null || encryptedString.trim().length() <= 0)
			throw new IllegalArgumentException("encrypted string was null or empty");
3
CodeRank
This code example shows how to use the following methods:generateSecret
			SecretKey secretKey = secretKeyCache.get(key);
			if (secretKey == null) {
				// 为上一密钥创建一个指定的 DESSede key 
				DESedeKeySpec spec = new DESedeKeySpec(key.getBytes());
				// 生成一个 DESede 密钥对象 
				secretKey = keyFactory.generateSecret(spec);
				secretKeyCache.put(key, secretKey);
			} 
			return secretKey;
		} 
 
		private String base64Encode(byte[] bytes) {
			BASE64Encoder b64e = new BASE64Encoder();
			String rs = b64e.encodeBuffer(bytes);
			if (rs != null)
				return rs.replaceAll("\\n|\\r", "");
			return rs;
		} 
 
		private byte[] base64Decode(String data) {
3
CodeRank
This code example shows how to use the following methods:generateSecret
     */ 
    public void setPassword(String pass) {
		password = pass;
		pbeKeySpec = new PBEKeySpec(password.toCharArray());
		try { 
		    pbeKey = keyFac.generateSecret(pbeKeySpec);
		    pbeCipher = Cipher
		    .getInstance(algorithm,"BC");
		} catch (InvalidKeySpecException e) {
		    Log.e(TAG,"setPassword(): "+e.toString());
		} catch (NoSuchAlgorithmException e) {
		    Log.e(TAG,"setPassword(): "+e.toString());
		} catch (NoSuchProviderException e) {
		    Log.e(TAG,"setPassword(): "+e.toString());
		} catch (NoSuchPaddingException e) {
		    Log.e(TAG,"setPassword(): "+e.toString());
		} 
    } 
 
    /** 
4
CodeRank
This code example shows how to use the following methods:generateSecret
	public void setPassword(String pass) {
		if (debug) Log.d(TAG,"setPassword("+pass+")");
		password = pass;
		pbeKeySpec = new PBEKeySpec(password.toCharArray());
		try { 
			pbeKey = keyFac.generateSecret(pbeKeySpec);
			pbeCipher = Cipher
					.getInstance(algorithm,"BC");
		} catch (InvalidKeySpecException e) {
			Log.e(TAG,"setPassword(): "+e.toString());
		} catch (NoSuchAlgorithmException e) {
			Log.e(TAG,"setPassword(): "+e.toString());
		} catch (NoSuchProviderException e) {
			Log.e(TAG,"setPassword(): "+e.toString());
		} catch (NoSuchPaddingException e) {
			Log.e(TAG,"setPassword(): "+e.toString());
		} 
		 
		// Every time we set a new password, also the session key changes: 
		sessionKey = createNewSessionKey();
3
CodeRank
This code example shows how to use the following methods:generateSecret
    public void setPassword(String pass) {
    	if (debug) Log.d(TAG,"setPassword("+pass+")");
		password = pass;
		pbeKeySpec = new PBEKeySpec(password.toCharArray());
		try { 
		    pbeKey = keyFac.generateSecret(pbeKeySpec);
		    pbeCipher = Cipher
		    .getInstance(algorithm,"BC");
		} catch (InvalidKeySpecException e) {
		    Log.e(TAG,"setPassword(): "+e.toString());
		} catch (NoSuchAlgorithmException e) {
		    Log.e(TAG,"setPassword(): "+e.toString());
		} catch (NoSuchProviderException e) {
		    Log.e(TAG,"setPassword(): "+e.toString());
		} catch (NoSuchPaddingException e) {
		    Log.e(TAG,"setPassword(): "+e.toString());
		} 
		 
		// Every time we set a new password, also the session key changes: 
		sessionKey = createNewSessionKey();
2
CodeRank
This code example shows how to use the following methods:generateSecret
			char[] passphrase = pass.toCharArray();
			this.pbeKeySpec = new PBEKeySpec(passphrase); //arbitrary plaintext, but must be the same at the server end
	              //keyFac = SecretKeyFactory.getInstance("PBEWithSHAAndTwoFish-CBC");     //we are specifying public block encryption with MD5 and DES 
			this.keyFac = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC", "BC");     //we are specifying public block encryption with MD5 and DES
		      //keyFac = SecretKeyFactory.getInstance("PBEWithHmacSHA1AndDESede"); 
	              SecretKey pbeKey = this.keyFac.generateSecret(this.pbeKeySpec);
 
	              // Create PBE Cipher 
	              this.pbeCipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC", "BC");
	              //pbeCipher = Cipher.getInstance("PBEWithSHAAndTwoFish-CBC"); 
			//pbeCipher = Cipher.getInstance("PBEWithHmacSHA1AndDESede"); 
	              // Initialize PBE Cipher with key and parameters 
	              this.pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, this.pbeParamSpec);
			} catch(Exception e) {
				logger.error("Error setting up encryption objects: " + e.getMessage(), e);
			} 
			 
		} 
		 
		public   BigInteger getHashOf(String s) {
3
CodeRank
This code example shows how to use the following methods:generateSecret
			String pass = "asdfasdf";
			char[] passphrase = pass.toCharArray();
			this.pbeKeySpec = new PBEKeySpec(passphrase);
			this.keyFac = SecretKeyFactory.getInstance(
					"PBEWithSHAAndTwofish-CBC", "BC"); 
			SecretKey pbeKey = this.keyFac.generateSecret(this.pbeKeySpec);
			// Create PBE Cipher 
			this.pbeCipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC",
					"BC"); 
			this.pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, this.pbeParamSpec);
		} catch (Exception ex) {
			logger.error( "Can't initialize crypt", ex );
		} 
		try { 
			this.out = this.client.getOutputStream();
			this.in = this.client.getInputStream();
		} catch (IOException ex) {
			logger.error( "Can't get socket in/out", ex );
		} 
		this.datain = new DataInputStream(this.in);
2
CodeRank
This code example shows how to use the following methods:generateSecret
            throw new IllegalArgumentException(
                    "unencrypted string was null or empty" ); 
         
        try 
        { 
            SecretKey key = keyFactory.generateSecret( keySpec );
            cipher.init( Cipher.ENCRYPT_MODE, key );
            byte[] cleartext = unencryptedString.getBytes( UNICODE_FORMAT );
            byte[] ciphertext = cipher.doFinal( cleartext );
             
            BASE64Encoder base64encoder = new BASE64Encoder();
            return base64encoder.encode( ciphertext );
        } 
        catch (Exception e)
        { 
            throw new EncryptionException( e );
        } 
    } 
     
    public String decrypt( String encryptedString ) throws EncryptionException
4
CodeRank

Related Java 8 SecretKeyFactory Questions & Answers:

See Code Examples for Java 8 SecretKeyFactory Methods: