Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.google.code.springcryptoutils.core.cipher.symmetric;
  
  
The default implementation for performing symmetric encryption/decryption using a static key.

Author(s):
Mirko Caserta (mirko.caserta@gmail.com)
 
 
     private String keyAlgorithm = "DESede";
     private String cipherAlgorithm = "DESede/CBC/PKCS5Padding";
     private Mode mode;
     private String key;
     private SecretKeySpec keySpec;

    
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 encryption/decryption mode.

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

    
A base64 encoded representation of the raw byte array containing the initialization vector.

Parameters:
initializationVector the initialization vector
Throws:
SymmetricEncryptionException on runtime errors
 
     public void setInitializationVector(String initializationVector) {
         try {
             this. = new IvParameterSpec(Base64.decodeBase64(initializationVector.getBytes("UTF-8")));
         } catch (UnsupportedEncodingException e) {
             throw new SymmetricEncryptionException("UTF-8 is an unsupported encoding on this platform"e);
         }
     }

    
A base64 encoded representation of the raw byte array containing the cryptographic key.

Parameters:
key the cryptographic key
 
     public void setKey(String key) {
         this. = key;
     }

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

Parameters:
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.google.code.springcryptoutils.core.cipher.Mode)
 
     public byte[] encrypt(byte[] message) {
         try {
             final Cipher cipher = Cipher.getInstance();
             switch () {
                 case :
                     cipher.init(.);
                     break;
                 case :
                     cipher.init(.);
                     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);
        }
    }
    public void afterPropertiesSet() {
        this. = new SecretKeySpec(Base64.decodeBase64(), );
    }
New to GrepCode? Check out our FAQ X