Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  
  package rs.baselib.crypto;
  
  import java.security.Key;
 
The class for decrypting strings.

Author(s):
ralph
 
 public class Decrypter {
 
     private Cipher dcipher;
Constructor from secret key.

Parameters:
dCipher the cipher to be used
 
 	public Decrypter(Cipher dCipher) {
 		this. = dCipher;
 	}

Constructor from secret key.

Parameters:
key the secret key to be used
algorithm algorithm (key's algorithm will be used if NULL)
Throws:
javax.crypto.NoSuchPaddingException when decrypting algorithm cannot be generated
java.security.NoSuchAlgorithmException when decrypting algorithm cannot be generated
java.security.InvalidKeyException when decrypting algorithm cannot be generated
java.security.InvalidAlgorithmParameterException when decrypting algorithm cannot be generated
 
 	public Decrypter(Key keyString algorithmthrows DecryptionException {
 		this(keyalgorithmnull);
 	}

Constructor from secret key.

Parameters:
key the secret key to be used
algorithm algorithm (key's algorithm will be used if NULL)
paramSpec parameters to the decrypting algorithm (will be generated if NULL)
Throws:
javax.crypto.NoSuchPaddingException when decrypting algorithm cannot be generated
java.security.NoSuchAlgorithmException when decrypting algorithm cannot be generated
java.security.InvalidKeyException when decrypting algorithm cannot be generated
java.security.InvalidAlgorithmParameterException when decrypting algorithm cannot be generated
 
 	public Decrypter(Key keyString algorithmAlgorithmParameterSpec paramSpecthrows DecryptionException {
 		init(keyalgorithmparamSpec);
 	}

Constructor from secret key.

Parameters:
key the secret key to be used
algorithm algorithm (key's algorithm will be used if NULL)
salt salt to be used
iterationCount number of iterations for decryption
Throws:
javax.crypto.NoSuchPaddingException when decrypting algorithm cannot be generated
java.security.NoSuchAlgorithmException when decrypting algorithm cannot be generated
java.security.InvalidKeyException when decrypting algorithm cannot be generated
java.security.InvalidAlgorithmParameterException when decrypting algorithm cannot be generated
 
 	public Decrypter(Key keyString algorithmbyte salt[], int iterationCountthrows DecryptionException {
         AlgorithmParameterSpec paramSpec = EncryptionUtils.generateParamSpec(saltiterationCount);
 		init(keyalgorithmparamSpec);
 	}

Constructor from bytephrase, salt and iteration spec.

Parameters:
bytephrase bytephrase to be used
salt salt to be used
iterationCount number of iterations for decryption
Throws:
java.security.spec.InvalidKeySpecException when key cannot be generated
javax.crypto.NoSuchPaddingException when decrypting algorithm cannot be generated
java.security.NoSuchAlgorithmException when decrypting algorithm cannot be generated
java.security.InvalidKeyException when decrypting algorithm cannot be generated
java.security.InvalidAlgorithmParameterException when decrypting algorithm cannot be generated
 
 	public Decrypter(byte bytephrase[], byte salt[], int iterationCountthrows DecryptionException {
 		
 		try {
 			if (iterationCount < 1) iterationCount = .;
 			if (salt == nullsalt = EncryptionUtils.generateSalt(0);
 			KeySpec keySpec = new PBEKeySpec(new String(bytephrase"UTF8").toCharArray(), saltiterationCount);
			AlgorithmParameterSpec paramSpec = EncryptionUtils.generateParamSpec(saltiterationCount);
        	throw new DecryptionException("Unsupported encoding: "+e.getMessage(), e);
catch (InvalidKeySpecException e) {
        	throw new DecryptionException("Invalid key specification: "+e.getMessage(), e);
catch (NoSuchAlgorithmException e) {
        	throw new DecryptionException("No such algorithm: "+e.getMessage(), e);
		}
	}

Constructor from passphrase.

Parameters:
passPhrase passphrase to be used
Throws:
DecryptionException when decrypting algorithm cannot be generated
	public Decrypter(char passPhrase[]) throws DecryptionException {
		this(new String(passPhrase));
	}

Constructor from passphrase.

Parameters:
passPhrase passphrase to be used
Throws:
DecryptionException when decrypting algorithm cannot be generated
	public Decrypter(char passPhrase[], byte salt[]) throws DecryptionException {
		this(new String(passPhrase), salt);
	}

Constructor from passphrase.

Parameters:
passPhrase passphrase to be used
Throws:
DecryptionException when decrypting algorithm cannot be generated
	public Decrypter(String passPhrasethrows DecryptionException {
		this(passPhrasenull, 0);
	}

Constructor from passphrase.

Parameters:
passPhrase passphrase to be used
Throws:
DecryptionException when decrypting algorithm cannot be generated
	public Decrypter(String passPhrasebyte salt[]) throws DecryptionException {
		this(passPhrasesalt, 0);
	}

Constructor from passphrase, salt and iteration spec.

Parameters:
passPhrase passphrase to be used
salt salt to be used
iterationCount number of iterations for decryption
Throws:
java.security.spec.InvalidKeySpecException when key cannot be generated
javax.crypto.NoSuchPaddingException when decrypting algorithm cannot be generated
java.security.NoSuchAlgorithmException when decrypting algorithm cannot be generated
java.security.InvalidKeyException when decrypting algorithm cannot be generated
java.security.InvalidAlgorithmParameterException when decrypting algorithm cannot be generated
	public Decrypter(String passPhrasebyte salt[], int iterationCountthrows DecryptionException {
		try {
			if (iterationCount < 1) iterationCount = .;
			if (salt == nullsalt = EncryptionUtils.generateSalt(0);
			KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), saltiterationCount);
			AlgorithmParameterSpec paramSpec = EncryptionUtils.generateParamSpec(saltiterationCount);
catch (InvalidKeySpecException e) {
        	throw new DecryptionException("Invalid key specification: "+e.getMessage(), e);
catch (NoSuchAlgorithmException e) {
        	throw new DecryptionException("No such algorithm: "+e.getMessage(), e);
		}
	}

Initializes this decrypter.

Parameters:
key secret key
algorithm algorithm (key's algorithm will be used if NULL)
paramSpec parameters to the decrypting algorithm (will be generated if NULL)
Throws:
javax.crypto.NoSuchPaddingException when decrypting algorithm cannot be generated
java.security.NoSuchAlgorithmException when decrypting algorithm cannot be generated
java.security.InvalidKeyException when decrypting algorithm cannot be generated
java.security.InvalidAlgorithmParameterException when decrypting algorithm cannot be generated
	private void init(Key keyString algorithmAlgorithmParameterSpec paramSpecthrows DecryptionException {
		try {
			if (algorithm == nullalgorithm = key.getAlgorithm();
			 = Cipher.getInstance(algorithm);
			if (paramSpec != null) {
				.init(.keyparamSpec);
else {
			}
catch (NoSuchPaddingException e) {
			throw new DecryptionException("No such padding: "+e.getMessage(), e);
catch (NoSuchAlgorithmException e) {
        	throw new DecryptionException("No such algorithm: "+e.getMessage(), e);
        	throw new DecryptionException("Invalid algorithm: "+e.getMessage(), e);
catch (InvalidKeyException e) {
        	throw new DecryptionException("Invalid key: "+e.getMessage(), e);
catch (Throwable t) {
        	throw new DecryptionException("Cannot initialize decrypter: "+t.getMessage(), t);
		}
	}

Returns the algorithm.

Returns:
the algorithm
	public String getAlgorithm() {
	}

Returns the algorithmParameterSpec.

Returns:
the algorithmParameterSpec
	}

    
Takes a single string as an argument and returns an decrypted version of that string.

Parameters:
str string to be decrypted
Returns:
string decrypted version of the provided String
    public String decrypt(String strthrows DecryptionException {
        try {
            // Decode base64 to get bytes
            byte[] dec = EncryptionUtils.decodeBase64(str);
            // Decrypt
            byte[] utf8 = .doFinal(dec);
            // Decode using utf-8
            return new String(utf8"UTF8");
        } catch (Throwable t) {
        	throw new DecryptionException("Cannot decrypt string: "+t.getMessage(), t);
        }
    }

    
Decrypt a byte array

Parameters:
bytes bytes to be decrypted
Returns:
byte decrypted version of the provided array
    public byte[] decrypt(byte bytes[]) throws DecryptionException {
        try {
            // Encrypt
            return .doFinal(bytes);
        } catch (Throwable t) {
        	throw new DecryptionException("Cannot decrypt: "+t.getMessage(), t);
        }
    }
New to GrepCode? Check out our FAQ X