Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Default implementation for performing symmetric encryption with strings containing base64 encoded versions of raw byte arrays.

Mirko Caserta (
 public class Base64EncodedCiphererImpl implements Base64EncodedCipherer {
     private String keyAlgorithm = "DESede";
     private String cipherAlgorithm = "DESede/CBC/PKCS5Padding";
     private String charsetName = "UTF-8";
     private Mode mode;
     private boolean chunkOutput;

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

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).

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

The charset to use when converting a string into a raw byte array representation. The default is UTF-8.

charsetName the charset name (default: UTF-8)
     public void setCharsetName(String charsetName) {
         this. = charsetName;

Sets the encryption/decryption mode.

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

When outputting long base64 encoded strings, should the output be formatted so it's easier to read? This may not work well with some base64 decoders which don't accept whitespace in the input so the default is false.

chunkOutput should the output be formatted?
     public void setChunkOutput(boolean chunkOutput) {
         this. = chunkOutput;

Encrypts or decrypts a message. The encryption/decryption mode depends on the configuration of the mode parameter.

key a base64 encoded version of the symmetric key
initializationVector a base64 encoded version of the initialization vector
message if in encryption mode, the clear-text message to encrypt, otherwise a base64 encoded version of the message to decrypt
if in encryption mode, returns a base64 encoded version of the encrypted message, otherwise returns the decrypted clear-text message
SymmetricEncryptionException on runtime errors
See also:
     public String encrypt(String keyString initializationVectorString message) {
         try {
             IvParameterSpec initializationVectorSpec = new IvParameterSpec(Base64.decodeBase64(initializationVector));
             final SecretKeySpec keySpec = new SecretKeySpec(Base64.decodeBase64(key), );
             final Cipher cipher = Cipher.getInstance();
             byte[] messageAsByteArray;
             switch () {
                 case :
                     messageAsByteArray = message.getBytes();
                     final byte[] encryptedMessage = cipher.doFinal(messageAsByteArray);
                     return new String(Base64.encodeBase64(encryptedMessage));
                case :
                    messageAsByteArray = Base64.decodeBase64(message);
                    final byte[] decryptedMessage = cipher.doFinal(messageAsByteArray);
                    return new String(decryptedMessage);
                    return null;
        } catch (Exception e) {
            throw new SymmetricEncryptionException("error encrypting/decrypting message; mode=" + e);
New to GrepCode? Check out our FAQ X