use of javax.crypto.Cipher in project hudson-2.x by hudson.
the class Secret method getEncryptedValue.
/**
* Encrypts {@link #value} and returns it in an encoded printable form.
*
* @see #toString()
*/
public String getEncryptedValue() {
try {
Cipher cipher = getCipher("AES");
cipher.init(Cipher.ENCRYPT_MODE, getKey());
// add the magic suffix which works like a check sum.
return new String(Base64.encode(cipher.doFinal((value + MAGIC).getBytes("UTF-8"))));
} catch (GeneralSecurityException e) {
// impossible
throw new Error(e);
} catch (UnsupportedEncodingException e) {
// impossible
throw new Error(e);
}
}
use of javax.crypto.Cipher in project hudson-2.x by hudson.
the class Secret method decrypt.
/**
* Reverse operation of {@link #getEncryptedValue()}. Returns null
* if the given cipher text was invalid.
*/
public static Secret decrypt(String data) {
if (data == null)
return null;
try {
Cipher cipher = getCipher("AES");
cipher.init(Cipher.DECRYPT_MODE, getKey());
String plainText = new String(cipher.doFinal(Base64.decode(data.toCharArray())), "UTF-8");
if (plainText.endsWith(MAGIC))
return new Secret(plainText.substring(0, plainText.length() - MAGIC.length()));
return null;
} catch (GeneralSecurityException e) {
return null;
} catch (UnsupportedEncodingException e) {
// impossible
throw new Error(e);
} catch (IOException e) {
return null;
}
}
use of javax.crypto.Cipher in project Openfire by igniterealtime.
the class AesEncryptor method cipher.
/**
* Symmetric encrypt/decrypt routine.
*
* @param attribute The value to be converted
* @param key The encryption key
* @param mode The cipher mode (encrypt or decrypt)
* @return The converted attribute, or null if conversion fails
*/
private byte[] cipher(byte[] attribute, byte[] key, int mode) {
byte[] result = null;
try {
// Create AES encryption key
Key aesKey = new SecretKeySpec(key, "AES");
// Create AES Cipher
Cipher aesCipher = Cipher.getInstance(ALGORITHM);
// Initialize AES Cipher and convert
aesCipher.init(mode, aesKey, new IvParameterSpec(INIT_PARM));
result = aesCipher.doFinal(attribute);
} catch (Exception e) {
log.error("AES cipher failed", e);
}
return result;
}
use of javax.crypto.Cipher in project Openfire by igniterealtime.
the class Crypto method generateAESKeystream.
public static Byte[] generateAESKeystream(Byte[] key, Integer length, Byte[] counter) {
byte[] output = new byte[length.intValue()];
for (int i = 0; i < output.length; i++) output[i] = 0;
byte[] input = BitAssistant.bytesFromArray(counter);
try {
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
cipher.init(1, new SecretKeySpec(BitAssistant.bytesFromArray(key), "AES"));
for (int i = 0; i < length.intValue(); i += 16) {
cipher.update(input, 0, 16, output, i);
IncrementCounter(input);
}
for (int i = 0; i < counter.length; i++) counter[i] = Byte.valueOf(input[i]);
return BitAssistant.bytesToArray(output);
} catch (Exception e) {
return new Byte[0];
}
}
use of javax.crypto.Cipher in project hudson-2.x by hudson.
the class AnnotatedLargeText method createAnnotator.
private ConsoleAnnotator createAnnotator(StaplerRequest req) throws IOException {
try {
String base64 = req != null ? req.getHeader("X-ConsoleAnnotator") : null;
if (base64 != null) {
Cipher sym = Secret.getCipher("AES");
sym.init(Cipher.DECRYPT_MODE, Hudson.getInstance().getSecretKeyAsAES128());
ObjectInputStream ois = new ObjectInputStreamEx(new GZIPInputStream(new CipherInputStream(new ByteArrayInputStream(Base64.decode(base64.toCharArray())), sym)), Hudson.getInstance().pluginManager.uberClassLoader);
long timestamp = ois.readLong();
if (TimeUnit2.HOURS.toMillis(1) > abs(System.currentTimeMillis() - timestamp))
// don't deserialize something too old to prevent a replay attack
return (ConsoleAnnotator) ois.readObject();
}
} catch (GeneralSecurityException e) {
throw new IOException2(e);
} catch (ClassNotFoundException e) {
throw new IOException2(e);
}
// start from scratch
return ConsoleAnnotator.initial(context == null ? null : context.getClass());
}
Aggregations