Search in sources :

Example 26 with Cipher

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);
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Cipher(javax.crypto.Cipher)

Example 27 with Cipher

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;
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Cipher(javax.crypto.Cipher) IOException(java.io.IOException)

Example 28 with Cipher

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;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) Key(java.security.Key)

Example 29 with Cipher

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];
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher)

Example 30 with Cipher

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());
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) GeneralSecurityException(java.security.GeneralSecurityException) Cipher(javax.crypto.Cipher) ObjectInputStreamEx(hudson.remoting.ObjectInputStreamEx) IOException2(hudson.util.IOException2) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

Cipher (javax.crypto.Cipher)626 SecretKey (javax.crypto.SecretKey)168 SecretKeySpec (javax.crypto.spec.SecretKeySpec)166 IvParameterSpec (javax.crypto.spec.IvParameterSpec)130 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)105 InvalidKeyException (java.security.InvalidKeyException)76 SecretKeyFactory (javax.crypto.SecretKeyFactory)75 IOException (java.io.IOException)72 GeneralSecurityException (java.security.GeneralSecurityException)68 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)60 Key (java.security.Key)54 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)54 BadPaddingException (javax.crypto.BadPaddingException)51 SecureRandom (java.security.SecureRandom)44 UnsupportedEncodingException (java.io.UnsupportedEncodingException)43 KeyGenerator (javax.crypto.KeyGenerator)43 PrivateKey (java.security.PrivateKey)41 PublicKey (java.security.PublicKey)40 PBEKeySpec (javax.crypto.spec.PBEKeySpec)36 KeyFactory (java.security.KeyFactory)35