Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.google.code.springcryptoutils.core.cipher.asymmetric;
 
 
The default implementation for performing asymmetric encryption/decryption with base64 encoded strings and a static key.

Author(s):
Mirko Caserta (mirko.caserta@gmail.com)
public class Base64EncodedCiphererImpl implements Base64EncodedCipherer {
    private String algorithm = "RSA";
    private String charsetName = "UTF-8";
    private Mode mode;
    private Key key;

    
The asymmetric key algorithm. The default is RSA.

Parameters:
algorithm the asymmetric key algorithm
    public void setAlgorithm(String algorithm) {
        this. = algorithm;
    }

    
The charset used when a message must be converted into a raw byte array. Default is UTF-8.

Parameters:
charsetName the charset name
    public void setCharsetName(String charsetName) {
        this. = charsetName;
    }

    
Sets the encryption/decryption mode.

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

    
Sets the encryption key.

Parameters:
key the encryption key
    public void setKey(Key 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 base64 encoded message to decrypt
Returns:
if in encryption mode, the base64 encoded encrypted message, otherwise the decrypted message
Throws:
AsymmetricEncryptionException on runtime errors
See also:
setMode(com.google.code.springcryptoutils.core.cipher.Mode)
    public String encrypt(String message) {
        try {
            final Cipher cipher = Cipher.getInstance();
            switch () {
                case :
                    final byte[] messageAsByteArray = message.getBytes();
                    cipher.init(.);
                    return Base64.encodeBase64String(cipher.doFinal(messageAsByteArray));
                case :
                    final byte[] encryptedMessage = Base64.decodeBase64(message);
                    cipher.init(.);
                    return new String(cipher.doFinal(encryptedMessage), );
                default:
                    return null;
            }
        } catch (Exception e) {
            throw new AsymmetricEncryptionException("error encrypting/decrypting message; mode=" + e);
        }
    }
New to GrepCode? Check out our FAQ X