use of org.craftercms.commons.crypto.CryptoException in project profile by craftercms.
the class RememberMeManagerImpl method serializeLogin.
protected String serializeLogin(PersistentLogin login) throws RememberMeException {
StringBuilder serializedLogin = new StringBuilder();
serializedLogin.append(login.getId()).append(SERIALIZED_LOGIN_SEPARATOR);
serializedLogin.append(login.getProfileId()).append(SERIALIZED_LOGIN_SEPARATOR);
serializedLogin.append(login.getToken());
try {
return encryptor.encrypt(serializedLogin.toString());
} catch (CryptoException e) {
throw new RememberMeException("Unable to encrypt remember me cookie", e);
}
}
use of org.craftercms.commons.crypto.CryptoException in project commons by craftercms.
the class SecretKeyRepositoryImpl method getKey.
@Override
public SecretKey getKey(String name, boolean create) throws CryptoException {
try {
SecretKey key = (SecretKey) keyStore.getKey(name, keyStorePassword);
if (key == null) {
logger.debug(LOG_KEY_KEY_NOT_FOUND, name);
if (create) {
key = CryptoUtils.generateKey(defaultKeyAlgorithm);
saveKey(name, key);
logger.debug(LOG_KEY_KEY_CREATED, name);
}
} else {
logger.debug(LOG_KEY_KEY_FOUND, name);
}
return key;
} catch (GeneralSecurityException e) {
throw new CryptoException(ERROR_KEY_GET_KEY_ERROR, e);
}
}
use of org.craftercms.commons.crypto.CryptoException in project commons by craftercms.
the class SecretKeyRepositoryImpl method saveKey.
@Override
public void saveKey(String name, SecretKey key) throws CryptoException {
KeyStore.ProtectionParameter protParam = new KeyStore.PasswordProtection(keyStorePassword);
KeyStore.SecretKeyEntry entry = new KeyStore.SecretKeyEntry(key);
try {
keyStore.setEntry(name, entry, protParam);
} catch (GeneralSecurityException e) {
throw new CryptoException(ERROR_KEY_SAVE_KEY_ERROR, e);
}
logger.debug(LOG_KEY_KEY_SAVED, name);
storeKeyStore();
}
use of org.craftercms.commons.crypto.CryptoException in project commons by craftercms.
the class PbkAesTextEncryptor method generateKey.
private static Key generateKey(String password, String salt) throws CryptoException {
try {
KeySpec keySpec = new PBEKeySpec(password.toCharArray(), Base64.decodeBase64(salt), PBK_ITER, PBK_LEN);
SecretKeyFactory factory = SecretKeyFactory.getInstance(PBK_ALGORITHM);
return new SecretKeySpec(factory.generateSecret(keySpec).getEncoded(), CryptoUtils.AES_CIPHER_ALGORITHM);
} catch (GeneralSecurityException e) {
throw new CryptoException("Unable to generate PBK key", e);
}
}
use of org.craftercms.commons.crypto.CryptoException in project commons by craftercms.
the class EncryptionTool method main.
public static final void main(String... args) {
Options options = createOptions();
CommandLineParser parser = new DefaultParser();
try {
CommandLine line = parser.parse(options, args);
if (line.hasOption(HELP_OPTION)) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("java -jar JARNAME [-e|-d|-e64|-d64 text] [-p password] [-s salt]", options);
} else if (line.hasOption(ENC_OPTION)) {
encrypt(line);
} else if (line.hasOption(DEC_OPTION)) {
decrypt(line);
} else if (line.hasOption(ENC_BASE64_OPTION)) {
encodeBase64(line);
} else if (line.hasOption(DEC_BASE64_OPTION)) {
decodeBase64(line);
} else {
throw new MissingOptionException("Either -" + ENC_OPTION + ", -" + DEC_OPTION + ", -" + ENC_BASE64_OPTION + ", -" + DEC_BASE64_OPTION + " or -" + HELP_OPTION + " must be provided");
}
} catch (ParseException e) {
System.err.println("Error parsing command line. Reason:");
e.printStackTrace(System.err);
} catch (CryptoException e) {
System.err.println("Error while executing encryption/decryption. Reason:");
e.printStackTrace(System.err);
}
}
Aggregations