Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.springcryptoutils.core.cipher.asymmetric;
 
 import java.util.Map;
 
 
The default implementation for performing asymmetric encryption/decryption with keys which are mapped with a logical name.

Author(s):
Mirko Caserta (mirko.caserta@gmail.com)
	private String algorithm = "RSA";
	private String provider;
	private Mode mode;
	private Map<StringKeykeyMap;

The asymmetric key algorithm. The default is RSA.

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

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;
	}

Sets the map of keys. The map key is a string representing the logical name of the key (the keyId).

Parameters:
keyMap the key map
	public void setKeyMap(Map<StringKeykeyMap) {
		this. = keyMap;
	}

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

Parameters:
keyId the key id
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:
AsymmetricEncryptionException on runtime errors
See also:
setMode(com.springcryptoutils.core.cipher.Mode)
	public byte[] encrypt(String keyIdbyte[] message) {
		final Key key = .get(keyId);
		if (key == null) {
			throw new AsymmetricEncryptionException("key not found: keyId=" + keyId);
		}
		try {
			final Cipher cipher = ((( == null) || (.length() == 0)) ? Cipher.getInstance() : Cipher
			switch () {
				case :
					cipher.init(.key);
					break;
				case :
					cipher.init(.key);
					break;
				default:
					throw new AsymmetricEncryptionException("error encrypting/decrypting message: invalid mode; mode="
);
			}
			return cipher.doFinal(message);
catch (Exception e) {
			throw new AsymmetricEncryptionException("error encrypting/decrypting message; mode=" + e);
		}
	}
New to GrepCode? Check out our FAQ X