Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * =============================================================================
   * 
   *   Copyright (c) 2007, The JASYPT team (http://www.jasypt.org)
   * 
   *   Licensed under the Apache License, Version 2.0 (the "License");
   *   you may not use this file except in compliance with the License.
   *   You may obtain a copy of the License at
   * 
  *       http://www.apache.org/licenses/LICENSE-2.0
  * 
  *   Unless required by applicable law or agreed to in writing, software
  *   distributed under the License is distributed on an "AS IS" BASIS,
  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *   See the License for the specific language governing permissions and
  *   limitations under the License.
  * 
  * =============================================================================
  */
 package org.jasypt.encryption.pbe;
 
 

Standard implementation of the PBEBigIntegerEncryptor interface. This class lets the user specify the algorithm to be used for encryption, the password to use, the number of hashing iterations and the salt generator that will be applied for obtaining the encryption key.

Important: The size of the result of encrypting a number, depending on the algorithm, may be much bigger (in bytes) than the size of the encrypted number itself. For example, encrypting a 4-byte integer can result in an encrypted 16-byte number. This can lead the user into problems if the encrypted values are to be stored and not enough room has been provided.

This class is thread-safe.


Configuration

The algorithm, password, key-obtention iterations and salt generator can take values in any of these ways:

  • Using its default values (except for password).
  • Setting a org.jasypt.encryption.pbe.config.PBEConfig object which provides new configuration values.
  • Calling the corresponding setAlgorithm(...), setPassword(...), setKeyObtentionIterations(...) or setSaltGenerator(...) methods.
And the actual values to be used for initialization will be established by applying the following priorities:
  1. First, the default values are considered (except for password).
  2. Then, if a org.jasypt.encryption.pbe.config.PBEConfig object has been set with setConfig(...), the non-null values returned by its getX() methods override the default values.
  3. Finally, if the corresponding setX(...) method has been called on the encryptor itself for any of the configuration parameters, the values set by these calls override all of the above.


Initialization

Before it is ready to encrypt, an object of this class has to be initialized. Initialization happens:

  • When initialize() is called.
  • When encrypt(...) or decrypt(...) are called for the first time, if initialize() has not been called before.
Once an encryptor has been initialized, trying to change its configuration will result in an AlreadyInitializedException being thrown.


Usage

An encryptor may be used for:

  • Encrypting messages, by calling the encrypt(...) method.
  • Decrypting messages, by calling the decrypt(...) method.
If a random salt generator is used, two encryption results for the same message will always be different (except in the case of random salt coincidence). This may enforce security by difficulting brute force attacks on sets of data at a time and forcing attackers to perform a brute force attack on each separate piece of encrypted data.

To learn more about the mechanisms involved in encryption, read PKCS #5: Password-Based Cryptography Standard.

Author(s):
Daniel Fernández Garrido
Since:
1.2
public final class StandardPBEBigIntegerEncryptor 
        implements PBEBigIntegerEncryptor {
    
    // The StandardPBEByteEncryptor that will be internally used.
    private StandardPBEByteEncryptor byteEncryptor = null;

    
    
    
Creates a new instance of StandardPBEBigIntegerEncryptor.
        super();
        this. = new StandardPBEByteEncryptor();
    }
    
    
    

Sets a org.jasypt.encryption.pbe.config.PBEConfig object for the encryptor. If this config object is set, it will be asked values for:

  • Algorithm
  • Password
  • Hashing iterations for obtaining the encryption key
  • Salt generator

The non-null values it returns will override the default ones, and will be overriden by any values specified with a setX method.

Parameters:
config the PBEConfig object to be used as the source for configuration parameters.
    public void setConfig(PBEConfig config) {
        .setConfig(config);
    }

    
    

Sets the algorithm to be used for encryption, like PBEWithMD5AndDES.

This algorithm has to be supported by your Java Virtual Machine, and it must be one of the algorithms registered at org.jasypt.encryption.pbe.algorithms.PBEAlgorithms.

Parameters:
algorithm the name of the algorithm to be used.
See also:
org.jasypt.encryption.pbe.algorithms.PBEAlgorithms
    public void setAlgorithm(String algorithm) {
        .setAlgorithm(algorithm);
    }

    
    

Sets the password to be used.

There is no default value for password, so not setting this parameter either from a org.jasypt.encryption.pbe.config.PBEConfig object or from a call to setPassword will result in an EncryptionInitializationException being thrown during initialization.

Parameters:
password the password to be used.
    public void setPassword(String password) {
        .setPassword(password);
    }
    

    

Set the number of hashing iterations applied to obtain the encryption key.

This mechanism is explained in PKCS #5: Password-Based Cryptography Standard.

Parameters:
keyObtentionIterations the number of iterations
    public void setKeyObtentionIterations(int keyObtentionIterations) {
        .setKeyObtentionIterations(keyObtentionIterations);
    }

    
    

Sets the salt generator to be used. If no salt generator is specified, an instance of org.jasypt.salt.RandomSaltGenerator will be used.

Parameters:
saltGenerator the salt generator to be used.
    public void setSaltGenerator(SaltGenerator saltGenerator) {
        .setSaltGenerator(saltGenerator);
    }


    

Returns true if the encryptor has already been initialized, false if not.
Initialization happens:

  • When initialize is called.
  • When encrypt or decrypt are called for the first time, if initialize has not been called before.

Once an encryptor has been initialized, trying to change its configuration (algorithm, password, key obtention iterations or salt generator) will result in an AlreadyInitializedException being thrown.

Returns:
true if the encryptor has already been initialized, false if not.
    public boolean isInitialized() {
        return .isInitialized();
    }

    
    

Initialize the encryptor.

This operation will consist in determining the actual configuration values to be used, and then initializing the encryptor with them.
These values are decided by applying the following priorities:

  1. First, the default values are considered (except for password).
  2. Then, if a org.jasypt.encryption.pbe.config.PBEConfig object has been set with setConfig, the non-null values returned by its getX methods override the default values.
  3. Finally, if the corresponding setX method has been called on the encryptor itself for any of the configuration parameters, the values set by these calls override all of the above.

Once an encryptor has been initialized, trying to change its configuration (algorithm, password, key obtention iterations or salt generator) will result in an AlreadyInitializedException being thrown.

Throws:
org.jasypt.exceptions.EncryptionInitializationException if initialization could not be correctly done (for example, no password has been set).
    public void initialize() {
        .initialize();
    }
    
    
    

Encrypts a message using the specified configuration.

Important: The size of the result of encrypting a number, depending on the algorithm, may be much bigger (in bytes) than the size of the encrypted number itself. For example, encrypting a 4-byte integer can result in an encrypted 16-byte number. This can lead the user into problems if the encrypted values are to be stored and not enough room has been provided.

The mechanisms applied to perform the encryption operation are described in PKCS #5: Password-Based Cryptography Standard.

This encryptor uses a salt for each encryption operation. The size of the salt depends on the algorithm being used. This salt is used for creating the encryption key and, if generated by a random generator, it is also appended unencrypted at the beginning of the results so that a decryption operation can be performed.

If a random salt generator is used, two encryption results for the same message will always be different (except in the case of random salt coincidence). This may enforce security by difficulting brute force attacks on sets of data at a time and forcing attackers to perform a brute force attack on each separate piece of encrypted data.

Parameters:
message the BigInteger message to be encrypted
Returns:
the result of encryption
Throws:
org.jasypt.exceptions.EncryptionOperationNotPossibleException if the encryption operation fails, ommitting any further information about the cause for security reasons.
org.jasypt.exceptions.EncryptionInitializationException if initialization could not be correctly done (for example, no password has been set).
    public BigInteger encrypt(BigInteger message) {
        
        if (message == null) {
            return null;
        }
        
        try {
            
            // Get the number in binary form
            byte[] messageBytes = message.toByteArray();
            
            // The StandardPBEByteEncryptor does its job.
            byte[] encryptedMessage = .encrypt(messageBytes);
            // The length of the encrypted message will be stored
            // with the result itself so that we can correctly rebuild
            // the complete byte array when decrypting (BigInteger will
            // ignore all "0x0" bytes in the leftmost side, and also "-0x1" 
            // in the leftmost side will be translated as signum).
            byte[] encryptedMessageLengthBytes =
                NumberUtils.byteArrayFromInt(encryptedMessage.length);
            
            // Append the length bytes to the encrypted message
            byte[] encryptionResult = 
                ArrayUtils.addAll(
                        encryptedMessageencryptedMessageLengthBytes);
            // Finally, return a new number built from the encrypted bytes
            return new BigInteger(encryptionResult);
        
        } catch (EncryptionInitializationException e) {
            throw e;
        } catch (EncryptionOperationNotPossibleException e) {
            throw e;
        } catch (Exception e) {
            // If encryption fails, it is more secure not to return any 
            // information about the cause in nested exceptions. Simply fail.
            throw new EncryptionOperationNotPossibleException();
        }
        
    }

    
    

Decrypts a message using the specified configuration.

The mechanisms applied to perform the decryption operation are described in PKCS #5: Password-Based Cryptography Standard.

If a random salt generator is used, this decryption operation will expect to find an unencrypted salt at the beginning of the encrypted input, so that the decryption operation can be correctly performed (there is no other way of knowing it).

Parameters:
encryptedMessage the BigInteger message to be decrypted
Returns:
the result of decryption
Throws:
org.jasypt.exceptions.EncryptionOperationNotPossibleException if the decryption operation fails, ommitting any further information about the cause for security reasons.
org.jasypt.exceptions.EncryptionInitializationException if initialization could not be correctly done (for example, no password has been set).
    public BigInteger decrypt(BigInteger encryptedMessage) {
        
        if (encryptedMessage == null) {
            return null;
        }
        
        try {
            // Get the number in binary form
            byte[] encryptedMessageBytes = encryptedMessage.toByteArray();
            // Process the encrypted byte array (check size, pad if needed...)
            encryptedMessageBytes = 
                NumberUtils.processBigIntegerEncryptedByteArray(
                        encryptedMessageBytesencryptedMessage.signum());
            
            // Let the byte encyptor decrypt
            byte[] message = .decrypt(encryptedMessageBytes);
            // Finally, return a new number built from the decrypted bytes
            return new BigInteger(message);
        
        } catch (EncryptionInitializationException e) {
            throw e;
        } catch (EncryptionOperationNotPossibleException e) {
            throw e;
        } catch (Exception e) {
            // If decryption fails, it is more secure not to return any 
            // information about the cause in nested exceptions. Simply fail.
            throw new EncryptionOperationNotPossibleException();
        }
    }
    
New to GrepCode? Check out our FAQ X