Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 Mirko Caserta
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this software except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *  http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.springcryptoutils.core.cipher.asymmetric;
 
 import java.util.Map;
 
 
 
The default implementation for performing asymmetric encryption/decryption with base64 encoded strings and keys which are mapped with a logical name.

Author(s):
Mirko Caserta (mirko.caserta@gmail.com)
 
 
 	private String algorithm = "RSA";
 	private String charsetName = "UTF-8";
 	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;
 	}

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 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 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.springcryptoutils.core.cipher.Mode)
	public String encrypt(String keyIdString 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 :
					final byte[] messageAsByteArray = message.getBytes();
					cipher.init(.key);
					return Base64.encodeBase64String(cipher.doFinal(messageAsByteArray));
				case :
					final byte[] encryptedMessage = Base64.decodeBase64(message);
					cipher.init(.key);
					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