Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  
  package rs.baselib.crypto;
  
 
 
Implements basic crypting decrypting.

Author(s):
ralph
 
 public class DefaultCryptingDelegate implements ICryptingDelegate {
 
 	private static Logger log = LoggerFactory.getLogger(DefaultCryptingDelegate.class);
 
 	private Cipher eCipher;
 	private Cipher dCipher;
 	private boolean blockBased;

Constructor.
 
 	}

Initializes this instance.

Parameters:
factory the factory providing initialization parameters
 
 	public void init(ICryptingDelegateFactory factory) {
 		try {
 			KeyPair keyPair = factory.getKeyPair();
 			String algorithm = factory.getAlgorithm();
 			if (keyPair != null) {
 				// Use key pair algorithm
 				if (algorithm == nullalgorithm = keyPair.getPrivate().getAlgorithm();
 				.debug("Key uses algorithm: "+algorithm);
 				AlgorithmParameterSpec spec = factory.getParamSpec();
 				if (algorithm == nullalgorithm = .;
 				 = Cipher.getInstance(algorithm);
 				 = Cipher.getInstance(algorithm);
 				SecureRandom random = new SecureRandom();
 				if (spec == null) {
 					.init(.keyPair.getPrivate(), random);
 					.init(.keyPair.getPublic(), random);
 				} else {
 					.init(.keyPair.getPrivate(), specrandom);
 					.init(.keyPair.getPublic(), specrandom);
 				}
 				 = true;
 			} else {
 				// Use passphrase algorithm
 				char passphrase[] = factory.getPassphrase();
 				int iterationCount = .;
 				byte salt[] = factory.getSalt();
 				KeySpec keySpec = new PBEKeySpec(passphrasesaltiterationCount);
 				AlgorithmParameterSpec paramSpec = factory.getParamSpec();
 				algorithm = factory.getAlgorithm();
 				if (algorithm == nullalgorithm = key.getAlgorithm();
 				 = Cipher.getInstance(algorithm);
 				 = Cipher.getInstance(algorithm);
 				if (paramSpec != null) {
 					.init(.keyparamSpec);
 					.init(.keyparamSpec);
 				} else {
 				}
 				 = false;
 			}
 			.debug("Cipher block length (encrypt) = "+.getBlockSize());
 			.debug("Cipher block length (decrypt) = "+.getBlockSize());
 		} catch (Exception e) {
 			throw new RuntimeException("Canot create ciphers"e);
 		}
 	}

 
 	public byte[] encrypt(byte[] bytesthrows Exception {
 		if (isBlockBased()) {
 			int blockSize = .getBlockSize();
 			if (blockSize == 0) blockSize = 245;
 			return EncryptionUtils.crypt(bytesblockSize);
		}
		return EncryptionUtils.crypt(bytes);
	}

	public byte[] decrypt(byte[] bytesthrows Exception {
		if (isBlockBased()) {
			int blockSize = .getBlockSize();
			if (blockSize == 0) blockSize = 256;
			return EncryptionUtils.crypt(bytesblockSize);
		}
		return EncryptionUtils.crypt(bytes);
	}

Returns true when the crypting algorithm is block based.

Returns:
true or false
	public boolean isBlockBased() {
		return ;
	}
New to GrepCode? Check out our FAQ X