Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.springcryptoutils.core.cipher.symmetric;
 
 
The default implementation for performing symmetric encryption/decryption.

Author(s):
Mirko Caserta (mirko.caserta@gmail.com)
public class CiphererImpl implements Cipherer {
	private String keyAlgorithm = "DESede";
	private String cipherAlgorithm = "DESede/CBC/PKCS5Padding";
	private String provider;
	private Mode mode;

The symmetric key algorithm. The default is DESede (triple DES).

Parameters:
keyAlgorithm the symmetric key algorithm
	public void setKeyAlgorithm(String keyAlgorithm) {
		this. = keyAlgorithm;
	}

The cipher algorithm. The default is DESede/CBC/PKCS5Padding (triple DES with Cipher Block Chaining and PKCS 5 padding).

Parameters:
cipherAlgorithm the cipher algorithm
	public void setCipherAlgorithm(String cipherAlgorithm) {
		this. = cipherAlgorithm;
	}

Sets the provider name of the specific implementation requested (e.g., "BC" for BouncyCastle, "SunJCE" for the default Sun JCE provider).

Parameters:
provider the provider to set
	public void setProvider(String provider) {
		this. = provider;
	}

Sets the encryption/decryption mode.

Parameters:
mode the encryption/decryption mode
	public void setMode(Mode mode) {
		this. = mode;
	}

Encrypts/decrypts a message based on the underlying mode of operation.

Parameters:
key the encryption key
initializationVector the initialization vector
message if in encryption mode, the clear-text message, otherwise the message to decrypt
Returns:
if in encryption mode, the encrypted message, otherwise the decrypted message
Throws:
SymmetricEncryptionException on runtime errors
See also:
setMode(com.springcryptoutils.core.cipher.Mode)
	public byte[] encrypt(byte[] keybyte[] initializationVectorbyte[] message) {
		try {
			IvParameterSpec initializationVectorSpec = new IvParameterSpec(initializationVector);
			final SecretKeySpec skey = new SecretKeySpec(key);
			final Cipher cipher = ((( == null) || (.length() == 0))
			switch () {
				case :
					cipher.init(.skeyinitializationVectorSpec);
					break;
				case :
					cipher.init(.skeyinitializationVectorSpec);
					break;
				default:
					throw new SymmetricEncryptionException("error encrypting/decrypting message: invalid mode; mode=" + );
			}
			return cipher.doFinal(message);
catch (Exception e) {
			throw new SymmetricEncryptionException("error encrypting/decrypting message; mode=" + e);
		}
	}
New to GrepCode? Check out our FAQ X